OpenCV二值化方法

来源:互联网 发布:软件开发实例 编辑:程序博客网 时间:2024/05/20 23:40

cvThreshold是opencv库中的一个函数

  作用:函数 cvThreshold 对单通道数组应用固定阈值操作。该函数的典型应用是对灰度图像进行阈值操作得到二值图像。(cvCmpS 也可以达到此目的) 或者是去掉噪声,例如过滤很小或很大象素值的图像点。本函数支持的对图像取阈值的方法由 threshold_type 确定。

  形式:void cvThreshold( const CvArr* src, CvArr* dst, double threshold, double max_value, int threshold_type );

  src:原始数组 (单通道 , 8-bit of 32-bit 浮点数)。dst:输出数组,必须与 src 的类型一致,或者为 8-bit。

  threshold:阈值

  max_value:使用 CV_THRESH_BINARY 和 CV_THRESH_BINARY_INV 的最大值。

 

本函数支持的对图像取阈值的方法由 threshold_type 确定:

threshold_type=CV_THRESH_BINARY:

dst(x,y) = max_value, if src(x,y)>threshold 0, otherwise.

threshold_type=CV_THRESH_BINARY_INV:

dst(x,y) = 0, if src(x,y)>threshold; dst(x,y) = max_value, otherwise.

threshold_type=CV_THRESH_TRUNC:

dst(x,y) = threshold, if src(x,y)>threshold;   dst(x,y) = src(x,y), otherwise.

threshold_type=CV_THRESH_TOZERO:

dst(x,y) = src(x,y), if (x,y)>threshold ;  dst(x,y) = 0, otherwise.

threshold_type=CV_THRESH_TOZERO_INV:

dst(x,y) = 0, if src(x,y)>threshold ;  dst(x,y) = src(x,y), otherwise.


值得一说的是threshold_type可以使用CV_THRESH_OTSU类型,这样该函数就会使用大律法OTSU得到的全局自适应阈值来进行二值化图片,而参数中的threshold不再起        作用。比如:cvThreshold( dst, dst,300 , 255,   CV_THRESH_OTSU | CV_THRESH_BINARY_INV);这种方法对于灰度直方图呈现二峰特征的图片处理起来效果很好。当然你也可以使用已有的OTSU算法来计算该阈值。如下:

[cpp] view plaincopy
  1. int otsu(const IplImage *src_image) //大津法求阈值  
  2. {  
  3.     double sum = 0.0;  
  4.     double w0 = 0.0;  
  5.     double w1 = 0.0;  
  6.     double u0_temp = 0.0;  
  7.     double u1_temp = 0.0;  
  8.     double u0 = 0.0;  
  9.     double u1 = 0.0;  
  10.     double delta_temp = 0.0;  
  11.     double delta_max = 0.0;  
  12.   
  13.     //src_image灰度级  
  14.     int pixel_count[256]={0};  
  15.     float pixel_pro[256]={0};  
  16.     int threshold = 0;  
  17.     uchar* data = (uchar*)src_image->imageData;  
  18.     //统计每个灰度级中像素的个数  
  19.     for(int i = 0; i < src_image->height; i++)  
  20.     {  
  21.         for(int j = 0;j < src_image->width;j++)  
  22.         {  
  23.             pixel_count[(int)data[i * src_image->width + j]]++;  
  24.             sum += (int)data[i * src_image->width + j];  
  25.         }  
  26.     }  
  27.     cout<<"平均灰度:"<<sum / ( src_image->height * src_image->width )<<endl;  
  28.     //计算每个灰度级的像素数目占整幅图像的比例  
  29.     for(int i = 0; i < 256; i++)  
  30.     {  
  31.     pixel_pro[i] = (float)pixel_count[i] / ( src_image->height * src_image->width );  
  32.     }  
  33.     //遍历灰度级[0,255],寻找合适的threshold  
  34.     for(int i = 0; i < 256; i++)  
  35.     {  
  36.         w0 = w1 = u0_temp = u1_temp = u0 = u1 = delta_temp = 0;  
  37.         for(int j = 0; j < 256; j++)  
  38.         {  
  39.             if(j <= i)   //背景部分  
  40.             {  
  41.                 w0 += pixel_pro[j];  
  42.                 u0_temp += j * pixel_pro[j];  
  43.             }  
  44.             else   //前景部分  
  45.             {  
  46.                 w1 += pixel_pro[j];  
  47.                 u1_temp += j * pixel_pro[j];  
  48.             }  
  49.         }  
  50.         u0 = u0_temp / w0;  
  51.         u1 = u1_temp / w1;  
  52.         delta_temp = (float)(w0 *w1* pow((u0 - u1), 2)) ;  
  53.         if(delta_temp > delta_max)  
  54.         {  
  55.             delta_max = delta_temp;  
  56.             threshold = i;  
  57.         }  
  58.     }  
  59.     return threshold;  
  60. }  
原创粉丝点击