root / trunk / code / projects / colonet / server / vision / capturetest.c @ 938
History | View | Annotate | Download (2.89 KB)
1 | 936 | rcahoon | #include "vision.h" |
---|---|---|---|
2 | |||
3 | #include <cv.h> |
||
4 | #include <highgui.h> |
||
5 | |||
6 | #include <stdio.h> |
||
7 | |||
8 | 937 | rcahoon | #define MIN_PADDING 70 |
9 | 936 | rcahoon | |
10 | 938 | rcahoon | #define CAPTURE
|
11 | 936 | rcahoon | |
12 | struct CenterP {
|
||
13 | CvPoint center; |
||
14 | int count;
|
||
15 | }; |
||
16 | |||
17 | 937 | rcahoon | #ifdef CAPTURE
|
18 | 936 | rcahoon | static CvCapture *capture;
|
19 | 937 | rcahoon | #endif
|
20 | 936 | rcahoon | |
21 | 937 | rcahoon | int main(int argc, char *argv[]) |
22 | { |
||
23 | #ifdef CAPTURE
|
||
24 | 938 | rcahoon | CvCapture* capture = cvCaptureFromCAM( 0 );
|
25 | 936 | rcahoon | if( !capture ) {
|
26 | fprintf( stderr, "ERROR: capture is NULL \n" );
|
||
27 | return -1; |
||
28 | } |
||
29 | 937 | rcahoon | #endif
|
30 | 936 | rcahoon | |
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 | 937 | rcahoon | do {
|
36 | IplImage *image03; |
||
37 | |||
38 | #ifdef CAPTURE
|
||
39 | 936 | rcahoon | if(!cvGrabFrame(capture)){ // capture a frame |
40 | printf("Could not grab a frame\n\7");
|
||
41 | 937 | rcahoon | return -1; |
42 | 936 | rcahoon | } |
43 | 937 | rcahoon | IplImage *im_capture=cvRetrieveFrame(capture); // retrieve the captured frame
|
44 | 936 | rcahoon | |
45 | image03 = cvCreateImage(cvGetSize(im_capture), im_capture->depth, 1);
|
||
46 | cvCvtColor(im_capture, image03, CV_RGB2GRAY); |
||
47 | 937 | rcahoon | #else
|
48 | // load image and force it to be grayscale
|
||
49 | if ((image03 = cvLoadImage(argv[1], 0)) == 0) { |
||
50 | 936 | rcahoon | fprintf(stderr, "Failed to load image.\n");
|
51 | return -1; |
||
52 | 937 | rcahoon | } |
53 | #endif
|
||
54 | 936 | rcahoon | |
55 | 937 | rcahoon | struct CenterP centers[100] = {0}; |
56 | int index = 0; |
||
57 | 936 | rcahoon | |
58 | 937 | rcahoon | 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 | 936 | rcahoon | |
62 | 937 | rcahoon | 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 | 936 | rcahoon | |
69 | 937 | rcahoon | //cvCircle( image03, center, 3, CV_RGB(255,255,255), -1, 8, 0 );
|
70 | 936 | rcahoon | |
71 | 937 | rcahoon | float dist = MIN_PADDING;
|
72 | int loc = index;
|
||
73 | 936 | rcahoon | |
74 | 937 | rcahoon | 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 | 936 | rcahoon | |
81 | 937 | rcahoon | if (d < dist)
|
82 | { |
||
83 | dist = d; |
||
84 | loc = j; |
||
85 | 936 | rcahoon | } |
86 | } |
||
87 | |||
88 | 937 | rcahoon | 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 | 936 | rcahoon | |
95 | 937 | rcahoon | for (i = 0; i < index; i++) |
96 | { |
||
97 | cvCircle( image03, centers[i].center, 60, CV_RGB(255,255,255), 3, 8, 0 ); |
||
98 | 936 | rcahoon | } |
99 | |||
100 | 937 | rcahoon | cvShowImage( "mywindow1", image03 );
|
101 | cvReleaseMemStorage(&storage); |
||
102 | |||
103 | 936 | rcahoon | 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 | 938 | rcahoon | } while((cvWaitKey(10) & 255) != 27); |
108 | 936 | rcahoon | |
109 | 937 | rcahoon | #ifdef CAPTURE
|
110 | 936 | rcahoon | // Release the capture device housekeeping
|
111 | cvReleaseCapture( &capture ); |
||
112 | 937 | rcahoon | #endif
|
113 | 936 | rcahoon | |
114 | cvDestroyWindow( "mywindow1" );
|
||
115 | return 0; |
||
116 | } |