Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / server / vision / vision.c @ 937

History | View | Annotate | Download (3.5 KB)

1
/**
2
 * Robot Detection
3
 * based on opencv's sample program fitellipse.c by Denis Burenkov
4
 *
5
 * @author Rich Hong
6
 * @date 11/18/2007
7
 */
8

    
9
#include "vision.h"
10

    
11
#include <cv.h>
12
#include <highgui.h>
13

    
14
#include <stdio.h>
15
#include <stdlib.h>
16

    
17
#define MIN_PADDING 70
18

    
19
#define DEBUG 1 //Debug to find threshold level
20

    
21
int write_JPEG_to_mem (unsigned char *storage, int length, IplImage *img, int quality);
22

    
23
struct CenterP {
24
  CvPoint center;
25
  int count;
26
};
27

    
28
/*static char* filename;*/
29

    
30
static CvCapture *capture;
31

    
32
int vision_init(/*char* filename_*/) {
33
  /*filename = filename_;*/
34
  
35
  //TODO: replace with 1394 capture so we don't have to use Coriander
36
  capture = cvCreateCameraCapture(-1);
37
  if( !capture ) {
38
    fprintf( stderr, "ERROR: capture is NULL \n" );
39
    return -1;
40
  }
41

    
42
  if (DEBUG)
43
    cvNamedWindow("Result", CV_WINDOW_AUTOSIZE);
44
  
45
  return 0;
46
}
47

    
48
void vision_close()
49
{
50
  if (DEBUG)
51
    cvDestroyWindow("Result");
52

    
53
  cvReleaseCapture(&capture);
54
}
55

    
56
int getImageBytes(unsigned char *buffer, int length)
57
{
58
  IplImage* im_capture;
59

    
60
  if(!cvGrabFrame(capture)){              // capture a frame 
61
    printf("Could not grab a frame\n\7");
62
    exit(0);
63
  }
64
  im_capture=cvRetrieveFrame(capture);           // retrieve the captured frame
65
  
66
  return write_JPEG_to_mem (buffer, length, im_capture, 30);
67
}
68

    
69
int vision_get_robot_positions(VisionPosition** positions) {
70
  IplImage *image03;
71

    
72
  if(!cvGrabFrame(capture)){              // capture a frame 
73
    printf("Could not grab a frame\n\7");
74
    return -1;
75
  }
76
  IplImage *im_capture=cvRetrieveFrame(capture);           // retrieve the captured frame
77
    
78
  image03 = cvCreateImage(cvGetSize(im_capture), im_capture->depth, 1);
79
  cvCvtColor(im_capture, image03, CV_RGB2GRAY);
80

    
81
  struct CenterP centers[100] = {0};
82
  int count = 0;
83

    
84
  cvSmooth( image03, image03, CV_GAUSSIAN, 9, 9 ); // smooth it, otherwise a lot of false circles may be detected
85
  CvMemStorage* storage = cvCreateMemStorage(0);
86
  CvSeq* circles = cvHoughCircles( image03, storage, CV_HOUGH_GRADIENT, 1, 1, 100, 43 );
87

    
88
  int i;
89
  for( i = 0; i < circles->total && count < 100; i++ )
90
  {
91
    //TODO: Augment with Harr cascade second level detecting orbs + identify robot with orb color
92

    
93
    float* p = (float*)cvGetSeqElem( circles, i );
94
    CvPoint center = cvPoint(cvRound(p[0]), cvRound(p[1]));
95
    int r = cvRound(p[2]);
96

    
97
    if (DEBUG) cvCircle( image03, center, 3, CV_RGB(255,255,255), -1, 8, 0 );
98

    
99
    float dist = MIN_PADDING;
100
    int loc = count;
101

    
102
    int j;
103
    for (j = 0; j < count; j++)
104
    {
105
      int dx = centers[j].center.x-center.x;
106
      int dy = centers[j].center.y-center.y;
107
      float d = cvSqrt(dx*dx + dy*dy);
108

    
109
      if (d < dist)
110
      {
111
        dist = d;
112
        loc = j;
113
      }
114
    }
115

    
116
    int c = centers[loc].count;
117
    centers[loc].center.x = (centers[loc].center.x * c + center.x) / (c + 1);
118
    centers[loc].center.y = (centers[loc].center.y * c + center.y) / (c + 1);
119
    centers[loc].count++;
120
    if (loc == count) count++;
121
  }
122

    
123
  if (DEBUG)
124
  {
125
    for (i = 0; i < count; i++)
126
    {
127
      cvCircle( image03, centers[i].center, 60, CV_RGB(255,255,255), 3, 8, 0 );
128
    }
129

    
130
    cvShowImage( "Result", image03 );
131
  }
132

    
133
  cvReleaseMemStorage(&storage);
134

    
135
  cvReleaseImage(&image03);
136

    
137
  VisionPosition* pos_array = (VisionPosition*)malloc(sizeof(VisionPosition) * count);
138
  if (pos_array == NULL) {
139
    fprintf(stderr, "malloc failed\n");
140
    return -1;
141
  }
142

    
143
  int c = 0;
144
  for (i = 0; i < count; i++) {
145
    pos_array[c].x = centers[i].center.x;
146
    pos_array[c].y = centers[i].center.y;
147
    c++;
148
  }
149

    
150
  *positions = pos_array;
151
  return count;
152
}