1 |
|
2 | #include "cv.h"
|
3 | #include "highgui.h"
|
4 | #include <stdio.h>
|
5 | #include <ctype.h>
|
6 |
|
7 | IplImage* image;
|
8 | IplImage* lastImage;
|
9 | IplImage* diffImage;
|
10 | IplImage* bitImage;
|
11 | double m00, m10, m01;
|
12 | int p1,p2,c;
|
13 | int tr=40;
|
14 |
|
15 |
|
16 | |
17 | {
|
18 | cvSetCaptureProperty(capture, CV_CAP_PROP_FPS,24);
|
19 | cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH,640);
|
20 | cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT,480);
|
21 | }
|
22 | */
|
23 |
|
24 |
|
25 | void showCaptureProperties(CvCapture* capture)
|
26 | {
|
27 | int w,h;
|
28 | double f;
|
29 | w=(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH);
|
30 | h=(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT);
|
31 | f=(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
|
32 | printf("Capture properties (widthxheight - frames): %dx%d - %lf\n",w,h,f);
|
33 | }
|
34 |
|
35 | int main( int argc, char** argv )
|
36 |
|
37 | {
|
38 |
|
39 | CvCapture* capture = 0;
|
40 |
|
41 |
|
42 | capture = cvCaptureFromCAM(0);
|
43 | cvSetCaptureProperty(capture, CV_CAP_PROP_FOURCC,'Y411');
|
44 | cvSetCaptureProperty(capture, CV_CAP_PROP_FPS,-1);
|
45 | cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH,640.0);
|
46 | cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT,480.0);
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | cvQueryFrame(capture);
|
53 | showCaptureProperties(capture);
|
54 |
|
55 | for(;;)
|
56 |
|
57 | {
|
58 | IplImage* frame = 0;
|
59 | frame = cvQueryFrame( capture );
|
60 |
|
61 | if( !frame )
|
62 | break;
|
63 |
|
64 | if(!image){
|
65 | image=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
|
66 | bitImage=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
|
67 | }
|
68 | cvCvtColor(frame,image,CV_BGR2GRAY);
|
69 |
|
70 | if(!lastImage)
|
71 | {
|
72 |
|
73 | lastImage=cvCloneImage(image);
|
74 | }
|
75 | if(!diffImage)
|
76 | {
|
77 |
|
78 | diffImage=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | cvThreshold(image,bitImage,tr,255,CV_THRESH_BINARY_INV);
|
85 |
|
86 |
|
87 |
|
88 | IplConvKernel* kernel;
|
89 | kernel = cvCreateStructuringElementEx(38, 38, 1, 1, CV_SHAPE_ELLIPSE, NULL);
|
90 |
|
91 |
|
92 |
|
93 | cvMorphologyEx(bitImage, diffImage, NULL, kernel, CV_MOP_OPEN, 1);
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | CvMoments moments;
|
102 | cvMoments(diffImage, &moments, 1);
|
103 | m00 = cvGetSpatialMoment(&moments, 0,0);
|
104 | m10 = cvGetSpatialMoment(&moments, 1,0);
|
105 | m01 = cvGetSpatialMoment(&moments, 0,1);
|
106 |
|
107 |
|
108 | float center_x = m10/m00;
|
109 | float center_y = m01/m00;
|
110 | p1 = (int)center_x;
|
111 | p2 = (int)center_y;
|
112 |
|
113 |
|
114 |
|
115 | IplImage* image_rgb = cvCreateImage( cvSize(diffImage->width, diffImage->height), diffImage->depth, 3 );
|
116 |
|
117 | cvCircle(image_rgb ,cvPoint(p1,p2), 2, CV_RGB(5,255,255),3,8,0);
|
118 | cvShowImage("Map",image_rgb);
|
119 | printf("center x %.1f , center y %.1f\n ",center_x,center_y);
|
120 |
|
121 |
|
122 |
|
123 | c = cvWaitKey(10);
|
124 | if( (char) c == 27 )
|
125 | break;
|
126 | }
|
127 |
|
128 |
|
129 | cvReleaseCapture( &capture );
|
130 | cvDestroyWindow("CamSub1");
|
131 |
|
132 |
|
133 |
|
134 | return 0;
|
135 |
|
136 | }
|