图像形态学 - 自适应阈值(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.

#include <highgui.h>
#include <math.h>
#include <cv.h>

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处理

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

 

转自:http://blog.csdn.net/hitwengqi/article/details/6856768

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值