第五章 - 图像形态学 - 自适应阈值(cvAdaptiveThreshold)

自适应阈值:

是一种改进了的阈值技术,其中阈值本身是一个变量,自适应阈值T(x,y)的每个像素点都不同,通过计算像素点周围的b*b区域的加权平均,然后减去一个常数来得到自适应阈值。


 cvAdaptiveThreshold方法:

Provides adaptive thresholding binary image.

void cvAdaptiveThreshold( IplImage* src, IplImage* dst, double max,
CvAdaptiveThreshMethod method, CvThreshType type, double* parameters);
src                          Source image.
dst                          Destination image.
max                        Max parameter, used with the types CV_THRESH_BINARY and CV_THRESH_BINARY_INV only.
method                  Method for the adaptive threshold definition; now CV_STDDEF_ADAPTIVE_THRESH only.
type                        Thresholding type; must be one of
                                  • CV_THRESH_BINARY, 
                                  • CV_THRESH_BINARY_INV, 
                                  • CV_THRESH_TOZERO, 
                                  • CV_THRESH_TOZERO_INV, 
parameters         Pointer to the list of method-specific input parameters. For the method CV_STDDEF_ADAPTIVE_THRESH the value parameters[0] is the size of the                                                     neighborhood: 1-(3x3), 2-(5x5), or 3-(7x7), and parameters[1] is the value of the minimum variance.



/*code*/

#include 
#include 
#include 

IplImage *Igray = 0, *It = 0, *Iat;

int main( int argc, char** argv )
{
	if( argc != 7 )
	{
		return -1;
	}
	//输入命令行
	double threshold = (double)atof( argv[1] ); //convert string to double
	int threshold_type = atoi( argv[2] ) ? CV_THRESH_BINARY : CV_THRESH_BINARY_INV;
	int adaptive_method = atoi( argv[3] ) ? CV_ADAPTIVE_THRESH_MEAN_C : CV_ADAPTIVE_THRESH_GAUSSIAN_C;
	int block_size = atoi( argv[4] );
	double offset = (double)atof( argv[5] );
	//加载灰度图
	if( ( Igray = cvLoadImage( argv[6], CV_LOAD_IMAGE_GRAYSCALE ) ) == 0 )
	{
		return -1;
	}
	//创建同样大小8位灰度图用于输出
	It = cvCreateImage( cvSize( Igray -> width, Igray -> height ), IPL_DEPTH_8U, 1 ); //单通道8位灰度图
	Iat = cvCreateImage( cvSize( Igray -> width, Igray -> height ), IPL_DEPTH_8U, 1 );
	//阈值化
	cvThreshold( Igray, It, threshold, 255, threshold_type );
	cvAdaptiveThreshold( Igray, Iat, 255, adaptive_method, threshold_type, block_size, offset );
	//命名窗体输出
	cvNamedWindow( "Raw", 1 );
	cvNamedWindow( "Threshold", 1 );
	cvNamedWindow( "Adaptive Threshold", 1 );
	cvShowImage( "Raw", Igray );
	cvShowImage( "Threshold", It );
	cvShowImage( "Adaptive Threshold", Iat );
	cvWaitKey(0);
	//回收内存
	cvReleaseImage( &Igray );
	cvReleaseImage( &It );
	cvReleaseImage( &Iat );
	cvDestroyWindow( "Raw" );
	cvDestroyWindow( "Threshold" );
	cvDestroyWindow( "Adaptive Threshold" );

	return 0;
}

/*input*/

在cmd debug目录下输入chapter_5_example_4.exe 15 1 1 71 15 fruits.jpg

 

其中:

【0】=chapter_5_example_4.ex

【1】=15,Threshold

【2】=1,Type

【3】=1,Method

【4】=71,Block Size

【5】=15,Offset 

【6】=原始图像名

/*result*/

raw gray image


cvThreshold处理


cvAdaptiveThreshold处理



结论,自适应阈值效果比较好,可以自动找到图像特征目标的轮廓。

你可能感兴趣的:(CV/PR/opencv,parameters,input,types,image,string,dst)