1 #include "opencv_libs.h" 2 #include <highgui.h> 3 #include <cv.h> 4 #include <stdio.h> 5 6 #pragma comment (lib,"opencv_calib3d231d.lib") 7 #pragma comment (lib,"opencv_contrib231d.lib") 8 #pragma comment (lib,"opencv_core231d.lib") 9 #pragma comment (lib,"opencv_features2d231d.lib") 10 #pragma comment (lib,"opencv_flann231d.lib") 11 #pragma comment (lib,"opencv_gpu231d.lib") 12 #pragma comment (lib,"opencv_haartraining_engined.lib") 13 #pragma comment (lib,"opencv_highgui231d.lib") 14 #pragma comment (lib,"opencv_imgproc231d.lib") 15 #pragma comment (lib,"opencv_legacy231d.lib") 16 #pragma comment (lib,"opencv_ml231d.lib") 17 #pragma comment (lib,"opencv_objdetect231d.lib") 18 #pragma comment (lib,"opencv_ts231d.lib") 19 #pragma comment (lib,"opencv_video231d.lib") 20 21 /* 22 *《学习OpenCV》第三章第五题 23 * 完成时间:0:41 3/3 星期日 2013 24 */ 25 26 int main() 27 { 28 // 加载一个三通道的彩色图像 29 IplImage * srcImage = cvLoadImage("lena.bmp", 1); 30 31 // 分割成R, G, B三个单通道图像 32 IplImage* red_plane = cvCreateImage(cvGetSize(srcImage), srcImage->depth, 1); 33 IplImage* green_plane = cvCreateImage(cvGetSize(srcImage), srcImage->depth, 1); 34 IplImage* blue_plane = cvCreateImage(cvGetSize(srcImage), srcImage->depth, 1); 35 36 // 分离RGB图像的顺序为 B, G, R 37 cvSplit(srcImage, blue_plane, green_plane, red_plane, NULL); 38 39 // 克隆绿图两次 40 IplImage* clone1 = cvCloneImage(green_plane); 41 IplImage* clone2 = cvCloneImage(green_plane); 42 43 // 找到绿色平面的最大值和最小值 44 double dMaxValue, dMinValue; 45 cvMinMaxLoc(green_plane, &dMinValue, &dMaxValue, NULL, NULL, NULL); 46 printf("Max: %f\tMin: %f\n", dMaxValue, dMinValue); 47 48 // 将clone1的所有元素赋值为thresh = (unsigned char)((max - min)/2)) 49 unsigned char thresh = (dMaxValue - dMinValue)/2; 50 cvSet(clone1, cvScalarAll(thresh), 0); 51 // 将clone2的所有元素赋零 52 cvSet(clone2, cvScalarAll(0), 0); 53 54 // 生成掩码图像 55 cvCmp(green_plane, clone1, clone2, CV_CMP_GE); 56 57 // 显示结果 58 cvSubS(green_plane, cvScalarAll(thresh/2), green_plane, clone2 ); 59 60 cvShowImage("clone1", clone1); 61 cvShowImage("clone2", clone2); 62 63 cvShowImage("红", red_plane); 64 cvShowImage("绿", green_plane); 65 cvShowImage("蓝", blue_plane); 66 cvShowImage("原始图像", srcImage); 67 68 cvvWaitKey(0); 69 70 cvReleaseImage(&srcImage); 71 cvReleaseImage(&red_plane); 72 cvReleaseImage(&green_plane); 73 cvReleaseImage(&blue_plane); 74 cvReleaseImage(&clone1); 75 cvReleaseImage(&clone2); 76 cvDestroyAllWindows(); 77 return 0; 78 }
运行结果: