OpenCV二值化方法

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算法來計算該閾值。如下:

int otsu(const IplImage *src_image) //大津法求閾值
{
	double sum = 0.0;
	double w0 = 0.0;
	double w1 = 0.0;
	double u0_temp = 0.0;
	double u1_temp = 0.0;
	double u0 = 0.0;
	double u1 = 0.0;
	double delta_temp = 0.0;
	double delta_max = 0.0;

	//src_image灰度級
	int pixel_count[256]={0};
	float pixel_pro[256]={0};
	int threshold = 0;
	uchar* data = (uchar*)src_image->imageData;
	//統計每個灰度級中像素的個數
	for(int i = 0; i < src_image->height; i++)
	{
		for(int j = 0;j < src_image->width;j++)
		{
			pixel_count[(int)data[i * src_image->width + j]]++;
			sum += (int)data[i * src_image->width + j];
		}
	}
    cout<<"平均灰度:"<<sum / ( src_image->height * src_image->width )<<endl;
	//計算每個灰度級的像素數目佔整幅圖像的比例
	for(int i = 0; i < 256; i++)
	{
	pixel_pro[i] = (float)pixel_count[i] / ( src_image->height * src_image->width );
	}
	//遍歷灰度級[0,255],尋找合適的threshold
	for(int i = 0; i < 256; i++)
	{
		w0 = w1 = u0_temp = u1_temp = u0 = u1 = delta_temp = 0;
		for(int j = 0; j < 256; j++)
		{
			if(j <= i)   //背景部分
			{
				w0 += pixel_pro[j];
				u0_temp += j * pixel_pro[j];
			}
			else   //前景部分
			{
				w1 += pixel_pro[j];
				u1_temp += j * pixel_pro[j];
			}
		}
		u0 = u0_temp / w0;
		u1 = u1_temp / w1;
		delta_temp = (float)(w0 *w1* pow((u0 - u1), 2)) ;
		if(delta_temp > delta_max)
		{
			delta_max = delta_temp;
			threshold = i;
		}
	}
	return threshold;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章