#include<stdio.h> #include<cv.h> #include<highgui.h> int main(int argc, char** argv){ IplImage* img = cvLoadImage("E:\\1.jpg"); IplImage* red = cvCreateImage(cvGetSize(img), img->depth, 1); IplImage* green = cvCreateImage(cvGetSize(img), img->depth, 1); IplImage* blue = cvCreateImage(cvGetSize(img), img->depth, 1); IplImage* green_image = cvCreateImage(cvGetSize(img), img->depth, img->nChannels); cvSplit(img, red, green, blue, NULL); cvZero(red); cvZero(blue); cvMerge(red, green, blue, NULL, green_image); cvNamedWindow("GreenChannel", CV_WINDOW_AUTOSIZE); cvShowImage("GreenChannel", green_image); //克隆这个绿图两次(分别命名为clone1和clone2) // IplImage* clone1 = cvCloneImage(green); IplImage* clone2 = cvCloneImage(green); //求出这个绿色平面的最大值和最小值 // double min; double max; CvPoint minPtr; CvPoint maxPtr; cvMinMaxLoc(green, &min, &max, &minPtr, &maxPtr); //注意:输入的图象必须是单通道的 printf("Minimum: %lf, Position: (%d, %d) \n", min, minPtr.x, minPtr.y); printf("Maximum: %lf, Position: (%d, %d) \n", max, maxPtr.x, maxPtr.y); //将clone1的所有元素赋值为thresh = (unsigned char)((最大值-最小值)/2.0) // double thresh = (max-min)/2; cvZero(clone1); cvAddS(clone1, cvScalar(thresh), clone1); //将clone2所有元素赋值为0,然后调用函数cvCmp(green_image, clone1, clone2, CV_CMP_GE)。 //现在clone2将是一个标识绿图中值超过thresh的掩码图象。 // //如果green中的元素大于或等于clone1中对应位置的元素,那么clone2对应位置的像素值被设为255 cvCmp(green, clone1, clone2, CV_CMP_GE); //最后,使用cvSubS(green_image, thresh/2, green_image, clone2)函数并显示结果 // cvSubS(green, cvScalar(thresh/2), green, clone2); IplImage* resultImg = cvCreateImage(cvGetSize(img), img->depth, img->nChannels); cvMerge(red, green, blue, NULL, resultImg); cvNamedWindow("FinalResult", CV_WINDOW_AUTOSIZE); cvShowImage("FinalResult", resultImg); cvWaitKey(0); cvReleaseImage(&img); cvReleaseImage(&red); cvReleaseImage(&green); cvReleaseImage(&blue); cvReleaseImage(&green_image); cvReleaseImage(&clone1); cvReleaseImage(&clone2); cvDestroyWindow("GreenChannel"); }