opit1.c

philip milev, 2010-03-26 11:55 am

Download (3.6 kB)

 
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
/*void SetCaptureProperties(CvCapture* capture)
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
    //SetCaptureProperties(capture);
48
    
49
  // cvNamedWindow( "CamSub 1",1);
50
   //        cvCreateTrackbar("Threshold","CamSub 1",&tr,255,NULL); //creates trackbar 
51
  //  cvNamedWindow( "CamMorph",1);
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
                //If is the first frame
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
            //If no lastImage clone actual image;
73
                        lastImage=cvCloneImage(image);
74
                }
75
                 if(!diffImage)
76
                       {
77
                                        //Create image header same as frame but with 1 channel to gray
78
                                          diffImage=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
79
                            }
80
                
81
                
82
       //treshold image and store it into bitImage
83
            
84
        cvThreshold(image,bitImage,tr,255,CV_THRESH_BINARY_INV);
85
86
// create opening kernel
87
                
88
        IplConvKernel* kernel;
89
        kernel = cvCreateStructuringElementEx(38, 38, 1, 1, CV_SHAPE_ELLIPSE, NULL);
90
91
//Morphological opening of bitImage using a circular kernel and saving it into diffImage
92
93
                cvMorphologyEx(bitImage, diffImage, NULL, kernel, CV_MOP_OPEN, 1);
94
        
95
//showing the videos in the windows created
96
 
97
 //   cvShowImage("CamSub 1",bitImage);
98
 //  cvShowImage("CamMorph",diffImage);
99
      
100
//Calculations of the centers of gravity of the blobs            
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
// TBD check that m00 != 0
107
108
 float center_x = m10/m00;
109
 float center_y = m01/m00;
110
p1 = (int)center_x;
111
p2 = (int)center_y;
112
//cast the centers into int numbers
113
114
115
  IplImage* image_rgb = cvCreateImage( cvSize(diffImage->width, diffImage->height), diffImage->depth, 3 );
116
     //cvCopy(diffImage,image_rgb,NULL);
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
   // cvDestroyWindow("Map");
132
   // cvDestroyWindow("CamMorph");
133
   
134
    return 0;
135
   
136
}