Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (3.63 KB)

1
/**
2
 * Robot Detection
3
 * based on opencv's sample program fitellipse.c by Denis Burenkov
4
 *
5
 * @author Rich Hong
6
 * @author Ryan Cahoon
7
 * @date 11/18/2007
8
 */
9
 
10
//TODO: Add camera stabilization
11

    
12
#include "vision.h"
13

    
14
#include <cv.h>
15
#include <highgui.h>
16

    
17
#include <stdio.h>
18
#include <stdlib.h>
19

    
20
#define MIN_PADDING 70
21

    
22
#define DEBUG 1 //Debug to find threshold level
23

    
24
int write_JPEG_to_mem (unsigned char *storage, int length, IplImage *img, int quality);
25

    
26
struct CenterP {
27
  CvPoint center;
28
  int count;
29
};
30

    
31
/*static char* filename;*/
32

    
33
static CvCapture *capture;
34

    
35
int vision_init(/*char* filename_*/) {
36
  /*filename = filename_;*/
37
  
38
  printf("Vision init. %s\n", DEBUG ? "Debugging on" : "");
39

    
40
  capture = cvCaptureFromCAM(0);
41
  if( !capture ) {
42
    fprintf( stderr, "ERROR: capture is NULL \n" );
43
    return -1;
44
  }
45

    
46
  if (DEBUG)
47
  {
48
    printf("Vision debugging start\n");
49
    cvNamedWindow("mywindow1", CV_WINDOW_AUTOSIZE);
50
  }
51
  
52
  return 0;
53
}
54

    
55
void vision_close()
56
{
57
  if (DEBUG)
58
    cvDestroyWindow("mywindow1");
59

    
60
  cvReleaseCapture(&capture);
61
  
62
  printf("Vision closed.\n");
63
}
64

    
65
int getImageBytes(unsigned char *buffer, int length)
66
{
67
  IplImage* im_capture;
68

    
69
  if(!cvGrabFrame(capture)){              // capture a frame 
70
    printf("Could not grab a frame\n\7");
71
    exit(0);
72
  }
73
  im_capture=cvRetrieveFrame(capture);           // retrieve the captured frame
74
  
75
  return write_JPEG_to_mem (buffer, length, im_capture, 30);
76
}
77

    
78
int vision_get_robot_positions(VisionPosition** positions) {
79
  IplImage *image03;
80

    
81
  if(!cvGrabFrame(capture)){              // capture a frame 
82
    printf("Could not grab a frame\n\7");
83
    return -1;
84
  }
85
  IplImage *im_capture=cvRetrieveFrame(capture);           // retrieve the captured frame
86
    
87
  image03 = cvCreateImage(cvGetSize(im_capture), im_capture->depth, 1);
88
  cvCvtColor(im_capture, image03, CV_RGB2GRAY);
89

    
90
  struct CenterP centers[100] = {0};
91
  int count = 0;
92

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

    
97
  int i;
98
  for( i = 0; i < circles->total && count < 100; i++ )
99
  {
100
    //TODO: Augment with Harr cascade second level detecting orbs + identify robot with orb color
101

    
102
    float* p = (float*)cvGetSeqElem( circles, i );
103
    CvPoint center = cvPoint(cvRound(p[0]), cvRound(p[1]));
104
    int r = cvRound(p[2]);
105

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

    
108
    int dist2 = MIN_PADDING;
109
    int loc = count;
110

    
111
    int j;
112
    for (j = 0; j < count; j++)
113
    {
114
      int dx = centers[j].center.x-center.x;
115
      int dy = centers[j].center.y-center.y;
116
      int d2 = dx*dx + dy*dy;
117

    
118
      if (d2 < dist2)
119
      {
120
        dist2 = d2;
121
        loc = j;
122
      }
123
    }
124

    
125
    int c = centers[loc].count;
126
    centers[loc].center.x = (centers[loc].center.x * c + center.x) / (c + 1);
127
    centers[loc].center.y = (centers[loc].center.y * c + center.y) / (c + 1);
128
    centers[loc].count++;
129
    if (loc == count) count++;
130
  }
131

    
132
  if (DEBUG)
133
  {
134
    for (i = 0; i < count; i++)
135
    {
136
      cvCircle( image03, centers[i].center, 60, CV_RGB(255,255,255), 3, 8, 0 );
137
    }
138

    
139
    cvShowImage( "mywindow1", image03 );
140
  }
141

    
142
  cvReleaseMemStorage(&storage);
143

    
144
  cvReleaseImage(&image03);
145

    
146
  VisionPosition* pos_array = (VisionPosition*)malloc(sizeof(VisionPosition) * count);
147
  if (pos_array == NULL) {
148
    fprintf(stderr, "malloc failed\n");
149
    return -1;
150
  }
151

    
152
  int c = 0;
153
  for (i = 0; i < count; i++) {
154
    pos_array[c].x = centers[i].center.x;
155
    pos_array[c].y = centers[i].center.y;
156
    c++;
157
  }
158

    
159
  *positions = pos_array;
160
  return count;
161
}