#include "stdafx.h"
#include "highgui.h"
#include "cv.h"
int main()
{
IplImage* str1, str2, str3;
str1 = cvLoadImage("H:\\3.jpg"); // load image
IplImage* img_B = cvCreateImage(cvGetSize(str1),IPL_DEPTH_8U,1); //single channle images
IplImage* img_G = cvCreateImage(cvGetSize(str1), IPL_DEPTH_8U, 1);
IplImage* img_R = cvCreateImage(cvGetSize(str1), IPL_DEPTH_8U, 1);
cvSplit(str1,img_B ,img_G, img_R, 0); //split the load image
//plImage* clone1 = img; 这是我原先写的错误代码,我之前理解的是clone1和cloen2其实就是复制img,所以便直接将
//IplImage* cloen2 = img; img赋值给cloen1和clone2,但是在运行时发现了明显的错误,这样的结果是G通道图像变成 了空。在这里cloen2 = img 将两个图像产生了联系,将clone2置零,img也将被置零。
IplImage* clone1 = cvCloneImage (img_G); //clone the G channle iamge
IplImage* clone2 = cvCloneImage (img_G);
double min = 0; double max = 255; //find the min and max points
CvPoint minpoint; CvPoint maxpoint;
cvMinMaxLoc(img_G, &min, &max, &minpoint, &maxpoint, 0);
printf("The min point in (%d,%d)\n", minpoint.x,minpoint.y);
printf("The max point in (%d,%d)\n", maxpoint.x,maxpoint.y);
double thresh = ((max - min) / 2.0); printf("thresh is %f", thresh);
cvSet(clone1, CvScalar(thresh,0,0), 0);
cvZero(clone2); // zero clone2
cvCmp(img_G, clone1, clone2, CV_CMP_GE);
cvSubS(img_G, CvScalar(thresh / 2), img_G, clone2);
cvShowImage("掩码图像", clone2);
cvShowImage("origin", str1);
cvShowImage("G", img_G);
cvWaitKey(0);
return 0;
}