OpenCV__Canny边缘检测和缩放(译)
View Code
1 // opencvdemo.cpp : Defines the entry point for the console application.
2 //
3 #include "stdafx.h"
4
5 #ifdef _CH_
6 #pragma package <opencv>
7 #endif
8
9 #ifndef _EiC
10 #include "cv.h"
11 #include "highgui.h"
12 #endif
13
14 IplImage * in;
15
16 //缩放函数
17 IplImage* doPyrDown(IplImage* in,int filter=IPL_GAUSSIAN_5x5)
18 {
19 assert(in->width%2==0 && in->height%2==0);
20 IplImage* out = cvCreateImage(cvSize(in->width/2,in->height/2),in->depth,in->nChannels);
21 cvPyrDown(in,out);
22 return (out);
23 };
24
25 //Canny检测函数
26 IplImage* doCanny(
27 IplImage* in,
28 double lowThresh,
29 double highThresh,
30 double aperture)
31 {
32 if(in->nChannels!=1)
33 return (0); //canny 只能处理灰度图片
34 IplImage* out = cvCreateImage(cvGetSize(in),IPL_DEPTH_8U,1);
35 cvCanny(in,out,lowThresh,highThresh,aperture);
36 return (out);
37 }
38
39 int _tmain(int argc, _TCHAR* argv[])
40 {
41 char* filename=argc==2?argv[1]:(char*)"fruit.jpg";
42 int edge_thresh=1;
43 //将图像文件加载至内存。通过文件名确定被加载的文件的格式并且自动分配图像数据结构所需的内存
44 //cvLoadImage函数可以读取图像格式:BMP,DIB,JPEG,PNG,PBM,PGM,PPM,SR,RAS和TIFF。
45 if((in=cvLoadImage(filename,1))==0)
46 return -1;
47 IplImage* out;
48 out=doPyrDown(in,IPL_GAUSSIAN_5x5);
49 out=doPyrDown(out,IPL_GAUSSIAN_5x5);
50
51 cvNamedWindow("两次缩放处理",1);
52
53 cvShowImage("两次缩放处理",out);
54
55 out=doCanny(out,140,150,3);
56 cvNamedWindow("再进行Canny边缘检测",1);
57 cvShowImage("再进行Canny边缘检测",out);
58
59 cvReleaseImage(&in);
60 cvReleaseImage(&out);
61 cvWaitKey(0);
62 cvDestroyWindow("两次缩放处理");
63 cvDestroyWindow("再进行Canny边缘检测");
64
65 }
其中需要注意,在OpenCV中,我们必须确认被释放的空间必须是我们显示分配的。
本程序的主要功能是缩放图像两次,然后在缩放后的图像中寻找边缘。