root / trunk / code / projects / colonet / server / vision / capturetest.c @ 1428
History | View | Annotate | Download (2.89 KB)
1 |
#include "vision.h" |
---|---|
2 |
|
3 |
#include <cv.h> |
4 |
#include <highgui.h> |
5 |
|
6 |
#include <stdio.h> |
7 |
|
8 |
#define MIN_PADDING 70 |
9 |
|
10 |
#define CAPTURE
|
11 |
|
12 |
struct CenterP {
|
13 |
CvPoint center; |
14 |
int count;
|
15 |
}; |
16 |
|
17 |
#ifdef CAPTURE
|
18 |
static CvCapture *capture;
|
19 |
#endif
|
20 |
|
21 |
int main(int argc, char *argv[]) |
22 |
{ |
23 |
#ifdef CAPTURE
|
24 |
CvCapture* capture = cvCaptureFromCAM( 0 );
|
25 |
if( !capture ) {
|
26 |
fprintf( stderr, "ERROR: capture is NULL \n" );
|
27 |
return -1; |
28 |
} |
29 |
#endif
|
30 |
|
31 |
// Create a window in which the captured images will be presented
|
32 |
cvNamedWindow( "mywindow1", CV_WINDOW_AUTOSIZE );
|
33 |
|
34 |
// Show the image captured from the camera in the window and repeat
|
35 |
do {
|
36 |
IplImage *image03; |
37 |
|
38 |
#ifdef CAPTURE
|
39 |
if(!cvGrabFrame(capture)){ // capture a frame |
40 |
printf("Could not grab a frame\n\7");
|
41 |
return -1; |
42 |
} |
43 |
IplImage *im_capture=cvRetrieveFrame(capture); // retrieve the captured frame
|
44 |
|
45 |
image03 = cvCreateImage(cvGetSize(im_capture), im_capture->depth, 1);
|
46 |
cvCvtColor(im_capture, image03, CV_RGB2GRAY); |
47 |
#else
|
48 |
// load image and force it to be grayscale
|
49 |
if ((image03 = cvLoadImage(argv[1], 0)) == 0) { |
50 |
fprintf(stderr, "Failed to load image.\n");
|
51 |
return -1; |
52 |
} |
53 |
#endif
|
54 |
|
55 |
struct CenterP centers[100] = {0}; |
56 |
int index = 0; |
57 |
|
58 |
cvSmooth( image03, image03, CV_GAUSSIAN, 9, 9 ); // smooth it, otherwise a lot of false circles may be detected |
59 |
CvMemStorage* storage = cvCreateMemStorage(0);
|
60 |
CvSeq* circles = cvHoughCircles( image03, storage, CV_HOUGH_GRADIENT, 1, 1, 100, 43 ); |
61 |
|
62 |
int i;
|
63 |
for( i = 0; i < circles->total && index < 100; i++ ) |
64 |
{ |
65 |
float* p = (float*)cvGetSeqElem( circles, i ); |
66 |
CvPoint center = cvPoint(cvRound(p[0]), cvRound(p[1])); |
67 |
int r = cvRound(p[2]); |
68 |
|
69 |
//cvCircle( image03, center, 3, CV_RGB(255,255,255), -1, 8, 0 );
|
70 |
|
71 |
float dist = MIN_PADDING;
|
72 |
int loc = index;
|
73 |
|
74 |
int j;
|
75 |
for (j = 0; j < index; j++) |
76 |
{ |
77 |
int dx = centers[j].center.x-center.x;
|
78 |
int dy = centers[j].center.y-center.y;
|
79 |
float d = cvSqrt(dx*dx + dy*dy);
|
80 |
|
81 |
if (d < dist)
|
82 |
{ |
83 |
dist = d; |
84 |
loc = j; |
85 |
} |
86 |
} |
87 |
|
88 |
int count = centers[loc].count;
|
89 |
centers[loc].center.x = (centers[loc].center.x * count + center.x) / (count + 1);
|
90 |
centers[loc].center.y = (centers[loc].center.y * count + center.y) / (count + 1);
|
91 |
centers[loc].count++; |
92 |
if (loc == index) index++;
|
93 |
} |
94 |
|
95 |
for (i = 0; i < index; i++) |
96 |
{ |
97 |
cvCircle( image03, centers[i].center, 60, CV_RGB(255,255,255), 3, 8, 0 ); |
98 |
} |
99 |
|
100 |
cvShowImage( "mywindow1", image03 );
|
101 |
cvReleaseMemStorage(&storage); |
102 |
|
103 |
cvReleaseImage(&image03); |
104 |
|
105 |
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
|
106 |
//remove higher bits using AND operator
|
107 |
} while((cvWaitKey(10) & 255) != 27); |
108 |
|
109 |
#ifdef CAPTURE
|
110 |
// Release the capture device housekeeping
|
111 |
cvReleaseCapture( &capture ); |
112 |
#endif
|
113 |
|
114 |
cvDestroyWindow( "mywindow1" );
|
115 |
return 0; |
116 |
} |