zoukankan      html  css  js  c++  java
  • 12. 对一幅灰度图像用最大类间方差法求阈值,并对其进行二值化。

    #include <cv.h>
    #include <highgui.h> 
    #define GrayScale 256   
    int mytsu( IplImage *frame);
    int main()
    {
        int a=0;
         IplImage * test;
        IplImage * test_1;
        test = cvLoadImage("6013202130.bmp", 0);//图片路径是 ConsoleApplication4 文件夹里,同时实验要求转为灰度图片
        test_1 = cvCreateImage(cvSize((test->width), (test->height)), IPL_DEPTH_8U, 1); //创建图像,给指针赋值
        a= mytsu(test);
        CvScalar s;
    
        for (int i = 0; i < test->height; i++)
        {
    
            for (int j = 0; j < test->width; j++)
            {
                s = cvGet2D(test, i, j);
                if (s.val[0] >a)
                    s.val[0] = 255;
                else
                    s.val[0] = 0;
                cvSet2D(test_1, i, j, s);
            }
        }
    
        cvNamedWindow("原图—6013202130", CV_WINDOW_AUTOSIZE);
        cvShowImage("原图—6013202130", test);
        cvNamedWindow("对数变换—6013202130", CV_WINDOW_AUTOSIZE);
        cvShowImage("对数变换—6013202130", test_1);
        cvWaitKey(0);//等待按键
        cvDestroyWindow("原图—6013202130");
        cvDestroyWindow("对数变换—6013202130");
        cvReleaseImage(&test);//释放内存。 
        cvReleaseImage(&test_1);
        return 0;
    }
    
    int mytsu( IplImage *frame)   
    {
    
        int width = frame->width;
        int height = frame->height;
        int pixelCount[GrayScale] = { 0 };
        float pixelPro[GrayScale] = { 0 };
        int i, j, pixelSum = width * height, threshold = 0;
        uchar* data = (uchar*)frame->imageData;
    
        //统计每个灰度级中像素的个数   
        for (i = 0; i < height; i++)
        {
            for (j = 0; j < width; j++)
            {
                pixelCount[(int)data[i * width + j]]++;
            }
        }
    
        //计算每个灰度级的像素数目占整幅图像的比例   
        for (i = 0; i < GrayScale; i++)
        {
            pixelPro[i] = (float)pixelCount[i] / pixelSum;
        }
    
        //遍历灰度级[0,255],寻找合适的threshold   
        float w0, w1, u0tmp, u1tmp, u0, u1, deltaTmp, deltaMax = 0;
        for (i = 0; i < GrayScale; i++)
        {
            w0 = w1 = u0tmp = u1tmp = u0 = u1 = deltaTmp = 0;
            for (j = 0; j < GrayScale; j++)
            {
                if (j <= i)   //背景部分   
                {
                    w0 += pixelPro[j];
                    u0tmp += j * pixelPro[j];
                }
                else   //前景部分   
                {
                    w1 += pixelPro[j];
                    u1tmp += j * pixelPro[j];
                }
            }
            u0 = u0tmp / w0;
            u1 = u1tmp / w1;
            deltaTmp = (float)(w0 *w1* pow((u0 - u1), 2));
            if (deltaTmp > deltaMax)
            {
                deltaMax = deltaTmp;
                threshold = i;
            }
        }
        return threshold;
    }

  • 相关阅读:
    【软件构造】Lab1基本流程指导及重难点分析
    【软件构造】关于java中List和Set数据结构不同实现方式和不同遍历方式时间效率的探讨与分析
    程序人生-Hello’s P2P
    WinterCamp2017吃饭睡觉记
    bzoj 3144 [Hnoi2013]切糕
    bzoj 1565 [NOI2009]植物大战僵尸
    bzoj 1061 [Noi2008]志愿者招募
    序列
    Philosopher
    时机成熟之时
  • 原文地址:https://www.cnblogs.com/zhangfeionline/p/5465233.html
Copyright © 2011-2022 走看看