题目要求 |
程序代码 |
结果图片 |
要言妙道 |
通过设置cvCanny中好的低阈值和高阈值,使用三种不同的高低阈值比,分别为1.5:1 、2.75:1、4:1
a、设置高阈值小于50
b、设置50<高阈值<100
c、设置100<高阈值<150
e、设置200<高阈值<250
1 // OpenCVExerciseTesting.cpp : 定义控制台应用程序的入口点。 2 // 3 //D:\\Work\\Work_Programming\\Source\\Image\\lena.jpg 4 5 6 #include "stdafx.h" 7 #include <cv.h> 8 #include <highgui.h> 9 #include <iostream> 10 11 #include <opencv2/legacy/legacy.hpp> 12 //#pragma comment(lib, "opencv_legacy2411.lib") 13 14 using namespace cv; 15 using namespace std; 16 17 //函数声明-->--->-->--->-->--->-->--->// 18 19 20 //<--<--<--<--<--<--<--<--<--函数声明// 21 22 int _tmain(int argc, _TCHAR* argv[]) 23 { 24 const char * soutceFile = "D:\\Work\\Work_Programming\\Source\\Image\\OpenCVExerciseImage\\第6章\\建筑.jpg"; 25 IplImage * image_Resource = cvLoadImage(soutceFile, CV_LOAD_IMAGE_GRAYSCALE); 26 assert(image_Resource); 27 28 cvNamedWindow("原始图像", CV_WINDOW_NORMAL); 29 cvNamedWindow("题目_a_1", CV_WINDOW_NORMAL); 30 cvNamedWindow("题目_a_2", CV_WINDOW_NORMAL); 31 cvNamedWindow("题目_a_3", CV_WINDOW_NORMAL); 32 cvNamedWindow("题目_b_1", CV_WINDOW_NORMAL); 33 cvNamedWindow("题目_b_2", CV_WINDOW_NORMAL); 34 cvNamedWindow("题目_b_3", CV_WINDOW_NORMAL); 35 cvNamedWindow("题目_c_1", CV_WINDOW_NORMAL); 36 cvNamedWindow("题目_c_2", CV_WINDOW_NORMAL); 37 cvNamedWindow("题目_c_3", CV_WINDOW_NORMAL); 38 //cvNamedWindow("题目_d_1", CV_WINDOW_NORMAL); 39 //cvNamedWindow("题目_d_2", CV_WINDOW_NORMAL); 40 //cvNamedWindow("题目_d_3", CV_WINDOW_NORMAL); 41 cvNamedWindow("题目_e_1", CV_WINDOW_NORMAL); 42 cvNamedWindow("题目_e_2", CV_WINDOW_NORMAL); 43 cvNamedWindow("题目_e_3", CV_WINDOW_NORMAL); 44 45 cvShowImage("原始图像", image_Resource); 46 47 //---------------------------a:开始--------------------------------// 48 49 IplImage * image_Result_a = cvCloneImage(image_Resource); 50 cvZero(image_Result_a); 51 52 double highThresh = 10; 53 double lowThresh = highThresh / 1.5; 54 55 cvCanny(image_Resource, image_Result_a, highThresh, lowThresh, 3); 56 cvShowImage("题目_a_1", image_Result_a); 57 58 lowThresh = highThresh / 2.75; 59 cvZero(image_Result_a); 60 61 cvCanny(image_Resource, image_Result_a, highThresh, lowThresh, 3); 62 cvShowImage("题目_a_2", image_Result_a); 63 64 lowThresh = highThresh / 4.0; 65 cvZero(image_Result_a); 66 67 cvCanny(image_Resource, image_Result_a, highThresh, lowThresh, 3); 68 cvShowImage("题目_a_3", image_Result_a); 69 70 //---------------------------a:结束--------------------------------// 71 72 //---------------------------b:开始--------------------------------// 73 74 IplImage * image_Result_b = cvCloneImage(image_Resource); 75 cvZero(image_Result_b); 76 77 highThresh = 80; 78 lowThresh = highThresh / 1.5; 79 80 cvCanny(image_Resource, image_Result_b, highThresh, lowThresh, 3); 81 cvShowImage("题目_b_1", image_Result_b); 82 83 lowThresh = highThresh / 2.75; 84 cvZero(image_Result_b); 85 86 cvCanny(image_Resource, image_Result_b, highThresh, lowThresh, 3); 87 cvShowImage("题目_b_2", image_Result_b); 88 89 lowThresh = highThresh / 4.0; 90 cvZero(image_Result_b); 91 92 cvCanny(image_Resource, image_Result_b, highThresh, lowThresh, 3); 93 cvShowImage("题目_b_3", image_Result_b); 94 95 //---------------------------b:结束--------------------------------// 96 97 //---------------------------c:开始--------------------------------// 98 99 IplImage * image_Result_c = cvCloneImage(image_Resource); 100 cvZero(image_Result_c); 101 102 highThresh = 130; 103 lowThresh = highThresh / 1.5; 104 105 cvCanny(image_Resource, image_Result_c, highThresh, lowThresh, 3); 106 cvShowImage("题目_c_1", image_Result_c); 107 108 lowThresh = highThresh / 2.75; 109 cvZero(image_Result_c); 110 111 cvCanny(image_Resource, image_Result_c, highThresh, lowThresh, 3); 112 cvShowImage("题目_c_2", image_Result_c); 113 114 lowThresh = highThresh / 4.0; 115 cvZero(image_Result_c); 116 117 cvCanny(image_Resource, image_Result_c, highThresh, lowThresh, 3); 118 cvShowImage("题目_c_3", image_Result_c); 119 120 //---------------------------c:结束--------------------------------// 121 122 //---------------------------e:开始--------------------------------// 123 124 IplImage * image_Result_e = cvCloneImage(image_Resource); 125 cvZero(image_Result_e); 126 127 highThresh = 245; 128 lowThresh = highThresh / 1.5; 129 130 cvCanny(image_Resource, image_Result_e, highThresh, lowThresh, 3); 131 cvShowImage("题目_e_1", image_Result_e); 132 133 lowThresh = highThresh / 2.75; 134 cvZero(image_Result_e); 135 136 cvCanny(image_Resource, image_Result_e, highThresh, lowThresh, 3); 137 cvShowImage("题目_e_2", image_Result_e); 138 139 lowThresh = highThresh / 4.0; 140 cvZero(image_Result_e); 141 142 cvCanny(image_Resource, image_Result_e, highThresh, lowThresh, 3); 143 cvShowImage("题目_e_3", image_Result_e); 144 145 //---------------------------e:结束--------------------------------// 146 147 cvWaitKey(0); 148 149 cvReleaseImage(&image_Resource); 150 cvReleaseImage(&image_Result_a); 151 cvReleaseImage(&image_Result_b); 152 cvReleaseImage(&image_Result_c); 153 cvReleaseImage(&image_Result_e); 154 155 cvDestroyAllWindows(); 156 157 return 0; 158 }
①Canny推荐的上下限阈值比为2:1到3:1之间