使用cvThreshold函数阈值化
示例:
#include <stdio.h> #include <cv.h> #include <highgui.h> void sum_rgb( IplImage* src, IplImage* dst ) { IplImage* r = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); IplImage* g = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); IplImage* b = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); /* * 函数原型: * CVAPI(void) cvSplit( const CvArr* src, CvArr* dst0, CvArr* dst1, * CvArr* dst2, CvArr* dst3 ); * dst0,dst1,dst2,dst3分别为r,g,b和alpha * 复制src各个通道rgb到单通道图像r、g和b */ cvSplit(src, r, g, b, NULL); IplImage* s = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); /* * 函数原型: * CVAPI(void) cvAddWeighted( const CvArr* src1, double alpha, * const CvArr* src2, double beta, * double gamma, CvArr* dst ); * dst(I)=src1(I)*alpha+src2(I)*beta+gamma */ cvAddWeighted( r, 1. / 3., g, 1. / 3., 0.0, s ); cvAddWeighted( s, 2. / 3., b, 1. / 3., 0.0, s ); /* * 上面最终求解为s(I)=r(I)*2/9+g(I)*2/9+b(I)*1/3 */ /* * 函数原型: * CVAPI(double) cvThreshold( const CvArr* src, CvArr* dst, * double threshold, double max_value, * int threshold_type ); * 图像阈值化 * src:输入图像 * dst:输出图像 * threshold:阈值 * max_value:最大值 * threshold_type:可选值有: * enum * { * CV_THRESH_BINARY =0, // dst(I) = src(I) > threshold ? max_value : 0 * CV_THRESH_BINARY_INV =1, // dst(I) = src(I) > threshold ? 0 : max_value * CV_THRESH_TRUNC =2, // dst(I) = src(I) > threshold ? max_value : src(I) * CV_THRESH_TOZERO =3, // dst(I) = src(I) > threshold ? src(I) : 0 * CV_THRESH_TOZERO_INV =4, // dst(I) = src(I) > threshold ? 0 : src(I) * CV_THRESH_MASK =7, * CV_THRESH_OTSU =8 // use Otsu algorithm to choose the optimal threshold value; * combine the flag with one of the above CV_THRESH_* values * }; */ cvThreshold(s, dst, 100, 100, CV_THRESH_TRUNC); cvReleaseImage(&r); cvReleaseImage(&g); cvReleaseImage(&b); cvReleaseImage(&s); } int main() { cvNamedWindow( "cvThreshold" ); IplImage* src = cvLoadImage( "E:\\Res\\Hydrangeas.jpg" ); IplImage* dst = cvCreateImage( cvGetSize( src ), src->depth, 1 ); sum_rgb( src, dst ); cvShowImage( "cvThreshold" , dst); cvWaitKey(0); cvDestroyWindow( "cvThreshold" ); cvReleaseImage(&src); cvReleaseImage(&dst); return 0; }
使用自适应阈值AdaptiveThreshold函数:
#include <stdio.h> #include <cv.h> #include <highgui.h> int main() { cvNamedWindow( "cvThreshold" ); IplImage* src = cvLoadImage( "E:\\Res\\Hydrangeas.jpg" ); IplImage* gray = cvCreateImage( cvGetSize( src ), src->depth, 1 ); cvCvtColor( src, gray, CV_BGR2GRAY ); IplImage* dst = cvCreateImage( cvGetSize( gray ), gray->depth, 1 ); /* * 函数原型: * CVAPI(void) cvAdaptiveThreshold( const CvArr* src, CvArr* dst, double max_value, * int adaptive_method CV_DEFAULT(CV_ADAPTIVE_THRESH_MEAN_C), * int threshold_type CV_DEFAULT(CV_THRESH_BINARY), * int block_size CV_DEFAULT(3), * double param1 CV_DEFAULT(5)); * src、dst:输入和输出图像 * adaptive_method:自适应阈值方法,有以下取值: * enum * { * CV_ADAPTIVE_THRESH_MEAN_C =0,//区域所有像素平均加权 * CV_ADAPTIVE_THRESH_GAUSSIAN_C =1//根据高斯函数按照区域所有像素离中心距离进行加权计算 * }; * threshold_type:阈值选取方式 * block_size:区域大小 * param1:常数 * 自适应阈值等于区域加权平均减去一个常数param1 */ cvAdaptiveThreshold(gray, dst, 255); cvShowImage( "cvThreshold" , dst); cvWaitKey(0); cvDestroyWindow( "cvThreshold" ); cvReleaseImage(&src); cvReleaseImage(&dst); return 0; }