转自:https://www.cnblogs.com/tingshuixuan2012/p/4472406.html
代码:
// OpenCVExerciseTesting.cpp : 定义控制台应用程序的入口点。
//
//D:\\Work\\Work_Programming\\Source\\Image\\lena.jpg
#include "stdafx.h"
#include
#include
#include
#include
//#pragma comment(lib, "opencv_legacy2411.lib")
using namespace cv;
using namespace std;
//函数声明-->--->-->--->-->--->-->--->//
//<--<--<--<--<--<--<--<--<--函数声明//
int _tmain(int argc, _TCHAR* argv[])
{
const char * soutceFile = "D:\\Work\\Work_Programming\\Source\\Image\\OpenCVExerciseImage\\第6章\\建筑.jpg";
IplImage * image_Resource = cvLoadImage(soutceFile, CV_LOAD_IMAGE_GRAYSCALE);
assert(image_Resource);
cvNamedWindow("原始图像", CV_WINDOW_NORMAL);
cvNamedWindow("题目_a_1", CV_WINDOW_NORMAL);
cvNamedWindow("题目_a_2", CV_WINDOW_NORMAL);
cvNamedWindow("题目_a_3", CV_WINDOW_NORMAL);
cvNamedWindow("题目_b_1", CV_WINDOW_NORMAL);
cvNamedWindow("题目_b_2", CV_WINDOW_NORMAL);
cvNamedWindow("题目_b_3", CV_WINDOW_NORMAL);
cvNamedWindow("题目_c_1", CV_WINDOW_NORMAL);
cvNamedWindow("题目_c_2", CV_WINDOW_NORMAL);
cvNamedWindow("题目_c_3", CV_WINDOW_NORMAL);
//cvNamedWindow("题目_d_1", CV_WINDOW_NORMAL);
//cvNamedWindow("题目_d_2", CV_WINDOW_NORMAL);
//cvNamedWindow("题目_d_3", CV_WINDOW_NORMAL);
cvNamedWindow("题目_e_1", CV_WINDOW_NORMAL);
cvNamedWindow("题目_e_2", CV_WINDOW_NORMAL);
cvNamedWindow("题目_e_3", CV_WINDOW_NORMAL);
cvShowImage("原始图像", image_Resource);
//---------------------------a:开始--------------------------------//
IplImage * image_Result_a = cvCloneImage(image_Resource);
cvZero(image_Result_a);
double highThresh = 10;
double lowThresh = highThresh / 1.5;
cvCanny(image_Resource, image_Result_a, highThresh, lowThresh, 3);
cvShowImage("题目_a_1", image_Result_a);
lowThresh = highThresh / 2.75;
cvZero(image_Result_a);
cvCanny(image_Resource, image_Result_a, highThresh, lowThresh, 3);
cvShowImage("题目_a_2", image_Result_a);
lowThresh = highThresh / 4.0;
cvZero(image_Result_a);
cvCanny(image_Resource, image_Result_a, highThresh, lowThresh, 3);
cvShowImage("题目_a_3", image_Result_a);
//---------------------------a:结束--------------------------------//
//---------------------------b:开始--------------------------------//
IplImage * image_Result_b = cvCloneImage(image_Resource);
cvZero(image_Result_b);
highThresh = 80;
lowThresh = highThresh / 1.5;
cvCanny(image_Resource, image_Result_b, highThresh, lowThresh, 3);
cvShowImage("题目_b_1", image_Result_b);
lowThresh = highThresh / 2.75;
cvZero(image_Result_b);
cvCanny(image_Resource, image_Result_b, highThresh, lowThresh, 3);
cvShowImage("题目_b_2", image_Result_b);
lowThresh = highThresh / 4.0;
cvZero(image_Result_b);
cvCanny(image_Resource, image_Result_b, highThresh, lowThresh, 3);
cvShowImage("题目_b_3", image_Result_b);
//---------------------------b:结束--------------------------------//
//---------------------------c:开始--------------------------------//
IplImage * image_Result_c = cvCloneImage(image_Resource);
cvZero(image_Result_c);
highThresh = 130;
lowThresh = highThresh / 1.5;
cvCanny(image_Resource, image_Result_c, highThresh, lowThresh, 3);
cvShowImage("题目_c_1", image_Result_c);
lowThresh = highThresh / 2.75;
cvZero(image_Result_c);
cvCanny(image_Resource, image_Result_c, highThresh, lowThresh, 3);
cvShowImage("题目_c_2", image_Result_c);
lowThresh = highThresh / 4.0;
cvZero(image_Result_c);
cvCanny(image_Resource, image_Result_c, highThresh, lowThresh, 3);
cvShowImage("题目_c_3", image_Result_c);
//---------------------------c:结束--------------------------------//
//---------------------------e:开始--------------------------------//
IplImage * image_Result_e = cvCloneImage(image_Resource);
cvZero(image_Result_e);
highThresh = 245;
lowThresh = highThresh / 1.5;
cvCanny(image_Resource, image_Result_e, highThresh, lowThresh, 3);
cvShowImage("题目_e_1", image_Result_e);
lowThresh = highThresh / 2.75;
cvZero(image_Result_e);
cvCanny(image_Resource, image_Result_e, highThresh, lowThresh, 3);
cvShowImage("题目_e_2", image_Result_e);
lowThresh = highThresh / 4.0;
cvZero(image_Result_e);
cvCanny(image_Resource, image_Result_e, highThresh, lowThresh, 3);
cvShowImage("题目_e_3", image_Result_e);
//---------------------------e:结束--------------------------------//
cvWaitKey(0);
cvReleaseImage(&image_Resource);
cvReleaseImage(&image_Result_a);
cvReleaseImage(&image_Result_b);
cvReleaseImage(&image_Result_c);
cvReleaseImage(&image_Result_e);
cvDestroyAllWindows();
return 0;
}
总结: