Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / server / vision / fitellipse.c @ 1428

History | View | Annotate | Download (4.03 KB)

1
/**
2
 * fitellipse.c
3
 * based on opencv's sample program fitellipse.c by Denis Burenkov
4
 *
5
 * @author Rich Hong
6
 * @date 12/05/2007
7
 */
8
#ifdef _CH_
9
#pragma package <opencv>
10
#endif
11

    
12
#ifndef _EiC
13
#include "cv.h"
14
#include "highgui.h"
15
#endif
16
#include <stdio.h>
17
#include <stdlib.h>
18

    
19
int slider_pos = 200;
20

    
21
// Load the source image. HighGUI use.
22
IplImage *image02 = 0, *image03 = 0, *image04 = 0;
23

    
24
void process_image(int h);
25

    
26
int main( int argc, char** argv ) {
27
        const char* filename = argc == 2 ? argv[1] : (char*)"colonet.jpg";
28

    
29
        // load image and force it to be grayscale
30
        if( (image03 = cvLoadImage(filename, 0)) == 0 ) return -1;
31

    
32
        // Create the destination images
33
        image02 = cvCloneImage( image03 );
34
        image04 = cvCloneImage( image03 );
35

    
36
        // Create windows.
37
        cvNamedWindow("Source", 1);
38
        // Show the image.
39
        cvShowImage("Source", image03);
40
        cvNamedWindow("Result", 1);
41
        // Create toolbars. HighGUI use.
42
        cvCreateTrackbar( "Threshold", "Result", &slider_pos, 255, process_image );
43
        process_image(0);
44
                
45
        // Wait for a key stroke; the same function arranges events processing
46
        cvWaitKey(0);
47

    
48
        cvReleaseImage(&image02);
49
        cvReleaseImage(&image03);
50

    
51
        cvDestroyWindow("Source");
52
        cvDestroyWindow("Result");
53
        return 0;
54
}
55

    
56
// Define trackbar callback functon. This function find contours,
57
// draw it and approximate it by ellipses.
58
void process_image(int h)
59
{
60
    CvMemStorage* stor;
61
    CvSeq* cont;
62
    CvBox2D32f* box;
63
    CvPoint* PointArray;
64
    CvPoint2D32f* PointArray2D32f;
65
    
66
    // Create dynamic structure and sequence.
67
    stor = cvCreateMemStorage(0);
68
    cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
69
    
70
    // Threshold the source image. This needful for cvFindContours().
71
    cvThreshold( image03, image02, slider_pos, 255, CV_THRESH_BINARY );
72
    
73
    // Find all contours.
74
    cvFindContours( image02, stor, &cont, sizeof(CvContour), 
75
                    CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
76
    
77
    // Clear images. IPL use.
78
    cvZero(image02);
79
    cvZero(image04);
80
    
81
    // This cycle draw all contours and approximate it by ellipses.
82
    for(;cont;cont = cont->h_next)
83
    {   
84
        int i; // Indicator of cycle.
85
        int count = cont->total; // This is number point in contour
86
        CvPoint center;
87
        CvSize size;
88
        
89
        // Number point must be more than or equal to 6 (for cvFitEllipse_32f).        
90
        if( count < 6 )
91
            continue;
92
        
93
        // Alloc memory for contour point set.    
94
        PointArray = (CvPoint*)malloc( count*sizeof(CvPoint) );
95
        PointArray2D32f= (CvPoint2D32f*)malloc( count*sizeof(CvPoint2D32f) );
96
        
97
        // Alloc memory for ellipse data.
98
        box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
99
        
100
        // Get contour point set.
101
        cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);
102
        
103
        // Convert CvPoint set to CvBox2D32f set.
104
        for(i=0; i<count; i++)
105
        {
106
            PointArray2D32f[i].x = (float)PointArray[i].x;
107
            PointArray2D32f[i].y = (float)PointArray[i].y;
108
        }
109
        
110
        // Fits ellipse to current contour.
111
        cvFitEllipse(PointArray2D32f, count, box);
112
        
113
        // Draw current contour.
114
        cvDrawContours(image04,cont,CV_RGB(255,255,255),CV_RGB(255,255,255),0,1,8,cvPoint(0,0));
115
        
116
        // Convert ellipse data from float to integer representation.
117
        center.x = cvRound(box->center.x);
118
        center.y = cvRound(box->center.y);
119
        size.width = cvRound(box->size.width*0.5);
120
        size.height = cvRound(box->size.height*0.5);
121
        box->angle = -box->angle;
122

    
123
        if (size.width>10&&size.height>10&&size.width<20&&size.height<20){
124
                //printf("%d %d %d %d\n",center.x,center.y,size.width,size.height);
125
        
126
                // Draw ellipse.
127
                       cvEllipse(image04, center, size,
128
                box->angle, 0, 360,
129
                CV_RGB(0,0,255), 1, CV_AA, 0);
130
        }
131
        // Free memory.          
132
        free(PointArray);
133
        free(PointArray2D32f);
134
        free(box);
135
    }
136
    
137
    // Show image. HighGUI use.
138
    cvShowImage( "Result", image04 );
139
}
140

    
141
#ifdef _EiC
142
main(1,"fitellipse.c");
143
#endif
144