#-------------------------------------------------
#
# Project created by QtCreator 2015-11-23T20:46:54
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = exercise3-7
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
CONFIG += c++11
INCLUDEPATH+=D:\opencv\opencv\build\include\opencv\
D:\opencv\opencv\build\include\opencv2\
D:\opencv\opencv\build\include
LIBS+=D:\opencv\opencv\build\x86\mingw\lib\libopencv_calib3d248.dll.a\
D:\opencv\opencv\build\x86\mingw\lib\libopencv_contrib248.dll.a\
D:\opencv\opencv\build\x86\mingw\lib\libopencv_core248.dll.a\
D:\opencv\opencv\build\x86\mingw\lib\libopencv_features2d248.dll.a\
D:\opencv\opencv\build\x86\mingw\lib\libopencv_flann248.dll.a\
D:\opencv\opencv\build\x86\mingw\lib\libopencv_gpu248.dll.a\
D:\opencv\opencv\build\x86\mingw\lib\libopencv_highgui248.dll.a\
D:\opencv\opencv\build\x86\mingw\lib\libopencv_imgproc248.dll.a\
D:\opencv\opencv\build\x86\mingw\lib\libopencv_legacy248.dll.a\
D:\opencv\opencv\build\x86\mingw\lib\libopencv_ml248.dll.a\
D:\opencv\opencv\build\x86\mingw\lib\libopencv_objdetect248.dll.a\
D:\opencv\opencv\build\x86\mingw\lib\libopencv_video248.dll.a\
以下是main.cpp
#include <highgui.h>
#include <cv.h>
#include <stdio.h>
int main()
{
// 加载一个三通道的彩色图像
IplImage * srcImage = cvLoadImage("D:\\OpencvforQT\\picture\\lena.jpg", 1);
// 分割成R, G, B三个单通道图像
IplImage* red_plane = cvCreateImage(cvGetSize(srcImage), srcImage->depth, 1);
IplImage* green_plane = cvCreateImage(cvGetSize(srcImage), srcImage->depth, 1);
IplImage* blue_plane = cvCreateImage(cvGetSize(srcImage), srcImage->depth, 1);
// 分离RGB图像的顺序为 B, G, R
cvSplit(srcImage, blue_plane, green_plane, red_plane, NULL);
// 克隆绿图两次
IplImage* clone1 = cvCloneImage(green_plane);
IplImage* clone2 = cvCloneImage(green_plane);
// 找到绿色平面的最大值和最小值
double dMaxValue, dMinValue;
cvMinMaxLoc(green_plane, &dMinValue, &dMaxValue, NULL, NULL, NULL);
printf("Max: %f\tMin: %f\n", dMaxValue, dMinValue);
// 将clone1的所有元素赋值为thresh = (unsigned char)((max - min)/2))
unsigned char thresh = (dMaxValue - dMinValue)/2;
cvSet(clone1, cvScalarAll(thresh), 0);
// 将clone2的所有元素赋零
cvSet(clone2, cvScalarAll(0), 0);
// 生成掩码图像
cvCmp(green_plane, clone1, clone2, CV_CMP_GE);
// 显示结果
cvSubS(green_plane, cvScalarAll(thresh/2), green_plane, clone2 );
cvShowImage("clone1", clone1); //窗口名要用英文
cvShowImage("clone2", clone2);
cvShowImage("Red", red_plane);
cvShowImage("Green", green_plane);
cvShowImage("Blue", blue_plane);
cvShowImage("Soure", srcImage); //原始图片
cvvWaitKey(0);
cvReleaseImage(&srcImage);
cvReleaseImage(&red_plane);
cvReleaseImage(&green_plane);
cvReleaseImage(&blue_plane);
cvReleaseImage(&clone1);
cvReleaseImage(&clone2);
cvDestroyAllWindows();
return 0;
}