Canny边缘检测源码与图像结果(OpenCV2.0)

View Code
 1 #include "stdafx.h"
2 #include "cv.h"
3 #include "cxcore.h"
4 #include "highgui.h"
5 #ifdef _CH_
6 #pragma package <opencv>
7 #endif
8 #ifndef _EiC
9 #include "cv.h"
10 #include "highgui.h"
11 #endif
12 char wndname[] = "Edge";
13 char tbarname[]= "Threshold";
14 IplImage * image=0,*cedge=0,*gray=0,*edge=0;// define a trackbar callbackvoid
15 on_trackbar(int h){ cvSmooth(gray,edge,CV_BLUR,3,3,0,0);
16 cvNot(gray,edge);
17 cvCanny(gray,edge,(float)h,(float)h*3,3);
18 cvZero(cedge);
19 cvCopy(image,cedge,edge);
20 cvShowImage(wndname,cedge);
21 };
22 int _tmain(int argc, _TCHAR* argv[]){
23 char* filename=argc==2?argv[1]:(char*)"fruit.jpg";
24 int edge_thresh=1;
25 //将图像文件加载至内存。通过文件名确定被加载的文件的格式并且自动分配图像数据结构所需的内存
26 //cvLoadImage函数可以读取图像格式:BMP,DIB,JPEG,PNG,PBM,PGM,PPM,SR,RAS和TIFF。
27 if((image=cvLoadImage(filename,1))==0)
28 return -1;
29 cedge = cvCreateImage(cvSize(image->width,image->height),IPL_DEPTH_8U,3);
30 gray=cvCreateImage(cvSize(image->width,image->height),IPL_DEPTH_8U,1);
31 edge=cvCreateImage(cvSize(image->width,image->height),IPL_DEPTH_8U,1);
32 cvCvtColor(image,gray,CV_RGB2GRAY);
33 // Create a window
34 cvNamedWindow(wndname,1);
35 // create a toolbar
36 cvCreateTrackbar(tbarname,wndname,&edge_thresh,100,on_trackbar);
37 // Show the image
38 on_trackbar(0);
39 // Wait for a key stroke; the same function arranges events processing
40 cvWaitKey(0);
41 cvReleaseImage(&image);
42 cvReleaseImage(&gray);
43 cvReleaseImage(&edge);
44 cvDestroyWindow(wndname);
45 return 0;
46 }
47 #ifdef _EiC
48 main(1,"edge.c");
49 #endif

环境为OpenCV2.0,VS2008

原文地址:https://www.cnblogs.com/slysky/p/2195142.html