码迷,mamicode.com
首页 > 其他好文 > 详细

3.1.1 OTSU阈值化

时间:2018-08-12 17:32:57      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:https   最大   ring   3.1.1   parameter   灰度   The   main   背景   

  1 ////Source Code: https://blog.csdn.net/silent_gods/article/details/81046919
  2 
  3 #include "opencv2/opencv.hpp"
  4 #include "opencv2/highgui/highgui.hpp"
  5 
  6 #include <string>
  7 #include <stdio.h>
  8 
  9 using namespace std;
 10 using namespace cv;
 11 
 12 //#define Gamma 3
 13 
 14 //OTSU 函数实现
 15 int OTSU(Mat srcImage) 
 16 {
 17     int nCols = srcImage.cols;
 18     int nRows = srcImage.rows;
 19     int threshold = 0;
 20     //init the parameters
 21     int nSumPix[256];
 22     float nProDis[256];
 23     for (int i = 0; i < 256; i++)
 24     {
 25         nSumPix[i] = 0;
 26         nProDis[i] = 0;
 27     }
 28 
 29     //统计灰度集中每个像素在整幅图像中的个数
 30     for (int i = 0; i < nRows; i++)
 31     {
 32         for (int j = 0; j < nCols; j++)
 33         {
 34             nSumPix[(int)srcImage.at<uchar>(i, j)]++;
 35         }
 36     }
 37 
 38     //计算每个灰度级占图像中的概率分布
 39     for (int i = 0; i < 256; i++)
 40     {
 41         nProDis[i] = (float)nSumPix[i] / (nCols*nRows);
 42     }
 43 
 44     //遍历灰度级【0,255】,计算出最大类间方差下的阈值
 45 
 46     float w0, w1, u0_temp, u1_temp, u0, u1, delta_temp;
 47     double delta_max = 0.0;
 48     for (int i = 0; i < 256; i++)
 49     {
 50         //初始化相关参数
 51         w0 = w1 = u0 = u1 = u0_temp = u1_temp = delta_temp = 0;
 52         for (int j = 0; j < 256; j++)
 53         {
 54             //背景部分
 55             if (j <= i)
 56             {
 57                 w0 += nProDis[j];
 58                 u0_temp += j*nProDis[j];
 59             }
 60             //前景部分
 61             else
 62             {
 63                 w1 += nProDis[j];
 64                 u1_temp += j*nProDis[j];
 65             }
 66         }
 67         //计算两个分类的平均灰度
 68         u0 = u0_temp / w0;
 69         u1 = u1_temp / w1;
 70         //依次找到最大类间方差下的阈值
 71         delta_temp = (float)(w0*w1*pow((u0 - u1), 2)); //前景与背景之间的方差(类间方差)
 72         if (delta_temp > delta_max)
 73         {
 74             delta_max = delta_temp;
 75             threshold = i;
 76         }
 77     }
 78     return threshold;
 79 }
 80 
 81 
 82 int  main()
 83 {
 84 
 85     namedWindow("srcGray", 0);
 86     cvResizeWindow("srcGray", 640, 480);
 87     namedWindow("otsuResultImage", 0);
 88     cvResizeWindow("otsuResultImage", 640, 480);
 89     namedWindow("dst", 0);
 90     cvResizeWindow("dst", 640, 480);
 91     //图像读取及判断
 92     Mat srcImage;
 93     srcImage = imread("D:\\0604.png");
 94     if (!srcImage.data)
 95     {
 96         return -1;
 97     }
 98     imshow("srcImage", srcImage);
 99     Mat srcGray;
100     cvtColor(srcImage, srcGray, CV_RGB2GRAY);
101     imshow("srcGray", srcGray);
102 
103     //调用otsu算法得到图像
104     int otsuThreshold = OTSU(srcGray);
105     cout << otsuThreshold << endl;
106     //定义输出结果图像
107     Mat otsuResultImage = Mat::zeros(srcGray.rows, srcGray.cols, CV_8UC1);
108 
109     //利用得到的阈值进行二值化操作
110     for (int i = 0; i < srcGray.rows; i++)
111     {
112         for (int j = 0; j < srcGray.cols; j++)
113         {
114             //cout << (int)srcGray.at<uchar>(i, j) << endl;
115             //高像素阈值判断
116             if (srcGray.at<uchar>(i, j) > otsuThreshold)
117             {
118                 otsuResultImage.at<uchar>(i, j) = 255;
119             }
120             else
121             {
122                 otsuResultImage.at<uchar>(i, j) = 0;
123             }
124             //cout <<(int)otsuResultImage.at<uchar>(i, j) << endl;
125         }
126     }
127     imshow("otsuResultImage", otsuResultImage);
128     waitKey(0);
129     return 0;
130 }

运行效果:

技术分享图片

技术分享图片技术分享图片

 

3.1.1 OTSU阈值化

标签:https   最大   ring   3.1.1   parameter   灰度   The   main   背景   

原文地址:https://www.cnblogs.com/thebreakofdawn/p/9463200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!