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;
    }

  • 相关阅读:
    Window 下配置ChromeDriver(简单4步完成)[转]
    selenium之 chromedriver与chrome版本映射表(更新至v2.46)[转]
    学习网站
    如何理解python中的类和方法(转)
    面试题整理20191127
    mysql 慢查询
    python学习--代码组织实例
    ubuntu下安装Matlab
    SkinPP for VC
    C++中的4个类型转换关键字
  • 原文地址:https://www.cnblogs.com/zhangfeionline/p/5465233.html
Copyright © 2011-2022 走看看