-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshape detection.cpp
92 lines (67 loc) · 1.97 KB
/
shape detection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**************************************************
* 轮廓检测
* 主要函数:
* cvFindContours
* cvDrawContours
**************************************************/
/***********************************************************************
* OpenCV example
* By Shiqi Yu 2006
***********************************************************************/
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
int main( int argc, char** argv )
{
//声明IplImage指针
IplImage* pImg = NULL;
IplImage* pContourImg = NULL;
CvMemStorage * storage = cvCreateMemStorage(0);
CvSeq * contour = 0;
int mode = CV_RETR_EXTERNAL;
if( argc == 3)
if(strcmp(argv[2], "all") == 0)
mode = CV_RETR_CCOMP; //内外轮廓都检测
//创建窗口
cvNamedWindow("src", 1);
cvNamedWindow("contour",1);
//载入图像,强制转化为Gray
if( argc >= 2 &&
(pImg = cvLoadImage( argv[1], 0)) != 0 )
{
cvShowImage( "src", pImg );
//为轮廓显示图像申请空间
//3通道图像,以便用彩色显示
pContourImg = cvCreateImage(cvGetSize(pImg),
IPL_DEPTH_8U,
3);
//copy source image and convert it to BGR image
cvCvtColor(pImg, pContourImg, CV_GRAY2BGR);
//查找contour
cvFindContours( pImg, storage, &contour, sizeof(CvContour),
mode, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
}
else
{
//销毁窗口
cvDestroyWindow( "src" );
cvDestroyWindow( "contour" );
cvReleaseMemStorage(&storage);
return -1;
}
//将轮廓画出
cvDrawContours(pContourImg, contour,
CV_RGB(0,0,255), CV_RGB(255, 0, 0),
2, 2, 8, cvPoint(0,0));
//显示图像
cvShowImage( "contour", pContourImg );
cvWaitKey(0);
//销毁窗口
cvDestroyWindow( "src" );
cvDestroyWindow( "contour" );
//释放图像
cvReleaseImage( &pImg );
cvReleaseImage( &pContourImg );
cvReleaseMemStorage(&storage);
return 0;
}