Opencv里面的阈值化做起来比较简单,只需要一个函数即可:
/* Applies fixed-level threshold to grayscale image. This is a basic operation applied before retrieving contours */ CVAPI(double) cvThreshold( const CvArr* src, CvArr* dst, double threshold, double max_value, int threshold_type );
这里是根据threadshould来决定处理源图像的阈值,使用threshold_type 来决定如何处理。
这里有5种选择,详见:
http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/threshold/threshold.html
下面来实践一下:
#include <cv.h> #include <highgui.h> #include <stdio.h> /* CV_IMPL void cvAddWeighted( const CvArr* srcarr1, double alpha, const CvArr* srcarr2, double beta, double gamma, CvArr* dstarr ) { cv::Mat src1 = cv::cvarrToMat(srcarr1), src2 = cv::cvarrToMat(srcarr2), dst = cv::cvarrToMat(dstarr); CV_Assert( src1.size == dst.size && src1.channels() == dst.channels() ); cv::addWeighted( src1, alpha, src2, beta, gamma, dst, dst.type() ); } void cv::addWeighted( InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype ) { double scalars[] = {alpha, beta, gamma}; arithm_op(src1, src2, dst, noArray(), dtype, getAddWeightedTab(), true, scalars); } */ void sum_rgb(IplImage* src, IplImage *dst, int type) { 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); //split the image to three color planes cvSplit(src, r, g, b, NULL); IplImage *s = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); /* void cvAddWeighted(const CvArr* src1, double alpha, const CvArr* src2, double beta, double gamma, CvArr* dst) dst = src1 * alpha + src2 * beta + gamma */ cvAddWeighted(r, 1.0/3.0, g, 1.0/3.0, 0.0, s); cvAddWeighted(s, 1.0/1.0, b, 1.0/3.0, 0.0, s); cvThreshold(s, dst, 100, 255, type); cvReleaseImage(&r); cvReleaseImage(&g); cvReleaseImage(&b); cvReleaseImage(&s); } int main(int argc, char **argv) { cvNamedWindow("HI", 1); IplImage *src = cvLoadImage(argv[1]); IplImage *dst = cvCreateImage(cvGetSize(src), src->depth, 1); const int methods[5] = {CV_THRESH_BINARY, CV_THRESH_BINARY_INV, CV_THRESH_TRUNC, CV_THRESH_TOZERO_INV, CV_THRESH_TOZERO}; const char* methods_str[5] = {"CV_THRESH_BINARY", "CV_THRESH_BINARY_INV", "CV_THRESH_TRUNC", "CV_THRESH_TOZERO_INV", "CV_THRESH_TOZERO"}; for(int i = 0; i < 5; i++) { sum_rgb(src, dst, methods[i]); cvShowImage(methods_str[i], dst); } while(1) { if(cvWaitKey(10) & 0x7f == 27) break; } cvDestroyWindow("HI"); cvReleaseImage(&src); cvReleaseImage(&dst); }
这里的关键函数是:
cvThreshold(s, dst, 100, 255, type);
效果如下: