自适应阈值化的函数为:
自适应阈值方法
void cvAdaptiveThreshold( const CvArr* src, CvArr* dst, double max_value, int adaptive_method=CV_ADAPTIVE_THRESH_MEAN_C, int threshold_type=CV_THRESH_BINARY, int block_size=3, double param1=5 );
函数 cvAdaptiveThreshold 将灰度图像变换到二值图像,采用下面公式:
threshold_type=CV_THRESH_BINARY: dst(x,y) = max_value, if src(x,y)>T(x,y) 0, otherwise threshold_type=CV_THRESH_BINARY_INV: dst(x,y) = 0, if src(x,y)>T(x,y) max_value, otherwise
其中 TI 是为每一个象素点单独计算的阈值
对方法 CV_ADAPTIVE_THRESH_MEAN_C,先求出块中的均值,再减掉param1。
对方法 CV_ADAPTIVE_THRESH_GAUSSIAN_C ,先求出块中的加权和(gaussian), 再减掉param1。
================================================================================================================
下面的例题对阈值化和自适应阈值化进行了比较:
#include "StdAfx.h" #include <cv.h> #include <highgui.h> #include <math.h> IplImage* Igray = 0; IplImage* It = 0; IplImage* Iat; void main() { Igray = cvLoadImage("lena.png", CV_LOAD_IMAGE_GRAYSCALE); It = cvCreateImage(cvSize(Igray->width, Igray->height),IPL_DEPTH_8U, 1); Iat = cvCreateImage(cvSize(Igray->width, Igray->height),IPL_DEPTH_8U, 1); cvThreshold(Igray, It, 150, 255,CV_THRESH_BINARY); cvAdaptiveThreshold(Igray, Iat, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 3, 5); cvNamedWindow("orignal", 1); cvNamedWindow("threshold", 1); cvNamedWindow("adaptiveThresh", 1); cvShowImage("orignal", Igray); cvShowImage("threshold", It); cvShowImage("adaptiveThresh", Iat); cvWaitKey(0); cvReleaseImage(&Igray); cvReleaseImage(&It); cvReleaseImage(&Iat); cvDestroyWindow("orignal"); cvDestroyWindow("threshold"); cvDestroyWindow("adaptiveThresh"); }
参考文献:
1.学习OpenCV,于仕祺,刘瑞祯,清华大学出版社,pp.159-161
2.http://blog.csdn.net/cartoonface/article/details/6011334
3.http://www.opencv.org.cn/index.php/Cv%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86#AdaptiveThreshold