OpenCV学习笔记-自适应阈值化

自适应阈值化的函数为:

AdaptiveThreshold

自适应阈值方法

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 );
src
输入图像.
dst
输出图像.
max_value
使用 CV_THRESH_BINARY 和 CV_THRESH_BINARY_INV 的最大值.
adaptive_method
自适应阈值算法使用:CV_ADAPTIVE_THRESH_MEAN_C 或 CV_ADAPTIVE_THRESH_GAUSSIAN_C (见讨论).
threshold_type
取阈值类型:必须是下者之一
  • CV_THRESH_BINARY,
  • CV_THRESH_BINARY_INV
block_size
用来计算阈值的象素邻域大小: 3, 5, 7, ...
param1
与方法有关的参数。对方法 CV_ADAPTIVE_THRESH_MEAN_C 和 CV_ADAPTIVE_THRESH_GAUSSIAN_C, 它是一个从均值或加权均值提取的常数(见讨论), 尽管它可以是负数。

函数 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 #include #include 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

你可能感兴趣的:(OpenCV学习笔记-自适应阈值化)