cvAdaptiveThreshold用法(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 <cv.h>
#include <highgui.h>
#include <math.h>
IplImage* Igray = 0;
IplImage* It = 0;
IplImage* Iat;
IplImage* Ibt;
void main()
{
	Igray = cvLoadImage("f:\\1.jpg", 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);
	Ibt = cvCreateImage(cvSize(Igray->width, Igray->height),IPL_DEPTH_8U, 1);
	cvThreshold(Igray, It, 50, 255,CV_THRESH_BINARY);
	cvAdaptiveThreshold(Igray, Iat, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 7, 5);
	cvAdaptiveThreshold(Igray, Ibt, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, 7, 5);
	cvNamedWindow("orignal", 1);
	cvNamedWindow("threshold", 1);
	cvNamedWindow("adaptiveThresh", 1);
	cvNamedWindow("adaptiveThresh1", 1);
	cvShowImage("orignal", Igray);
	cvShowImage("threshold", It);
	cvShowImage("adaptiveThresh", Iat);
	cvShowImage("adaptiveThresh1", Ibt);
	cvWaitKey(0);
	cvReleaseImage(&Igray);
	cvReleaseImage(&It);
	cvReleaseImage(&Iat);
	cvReleaseImage(&Ibt);
	cvDestroyWindow("orignal");
	cvDestroyWindow("threshold");
	cvDestroyWindow("adaptiveThresh");
	cvDestroyWindow("adaptiveThresh1");
}


内容主要借鉴自openCV第五章/图像处理 -- 阈值化(自适应) - cartoonface的专栏 - 博客频道 - CSDN.NET
http://blog.csdn.net/cartoonface/article/details/6011334

阅读更多
个人分类: OpenCV
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭