root / trunk / code / projects / colonet / server / vision / vision.c @ 1428
History | View | Annotate | Download (3.1 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 | #include "shmimgserve.h" |
| 21 | |
| 22 | #define MIN_PADDING 70 |
| 23 | |
| 24 | #define DEBUG 0 //Debug to find threshold level |
| 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 | openShM(); |
| 47 | |
| 48 | if (DEBUG)
|
| 49 | {
|
| 50 | cvNamedWindow("mywindow1", CV_WINDOW_AUTOSIZE);
|
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | void vision_close()
|
| 57 | {
|
| 58 | if (DEBUG)
|
| 59 | cvDestroyWindow("mywindow1");
|
| 60 | |
| 61 | releaseShM(); |
| 62 | |
| 63 | cvReleaseCapture(&capture); |
| 64 | |
| 65 | printf("Vision closed.\n");
|
| 66 | } |
| 67 | |
| 68 | int vision_get_robot_positions(VisionPosition** positions) {
|
| 69 | IplImage *image03; |
| 70 | |
| 71 | if(!cvGrabFrame(capture)){ // capture a frame |
| 72 | printf("Could not grab a frame\n");
|
| 73 | return -1; |
| 74 | } |
| 75 | IplImage *im_capture=cvRetrieveFrame(capture); // retrieve the captured frame
|
| 76 | |
| 77 | sendImage(im_capture); |
| 78 | |
| 79 | image03 = cvCreateImage(cvGetSize(im_capture), im_capture->depth, 1);
|
| 80 | cvCvtColor(im_capture, image03, CV_RGB2GRAY); |
| 81 | |
| 82 | struct CenterP centers[100] = {0}; |
| 83 | int count = 0; |
| 84 | |
| 85 | cvSmooth( image03, image03, CV_GAUSSIAN, 9, 9 ); // smooth it, otherwise a lot of false circles may be detected |
| 86 | CvMemStorage* storage = cvCreateMemStorage(0);
|
| 87 | CvSeq* circles = cvHoughCircles( image03, storage, CV_HOUGH_GRADIENT, 1, 50, 100, 43 ); |
| 88 | |
| 89 | int i;
|
| 90 | for( i = 0; i < circles->total && count < 100; i++ ) |
| 91 | {
|
| 92 | //TODO: Augment with Harr cascade second level detecting orbs + identify robot with orb color
|
| 93 | |
| 94 | float* p = (float*)cvGetSeqElem( circles, i ); |
| 95 | CvPoint center = cvPoint(cvRound(p[0]), cvRound(p[1])); |
| 96 | int r = cvRound(p[2]); |
| 97 | |
| 98 | if (DEBUG) cvCircle( image03, center, 3, CV_RGB(255,255,255), 3, 8, 0 ); |
| 99 | |
| 100 | int dist2 = MIN_PADDING;
|
| 101 | int loc = count;
|
| 102 | |
| 103 | int j;
|
| 104 | for (j = 0; j < count; j++) |
| 105 | {
|
| 106 | int dx = centers[j].center.x-center.x;
|
| 107 | int dy = centers[j].center.y-center.y;
|
| 108 | int d2 = dx*dx + dy*dy;
|
| 109 | |
| 110 | if (d2 < dist2)
|
| 111 | {
|
| 112 | dist2 = d2; |
| 113 | loc = j; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | int c = centers[loc].count;
|
| 118 | centers[loc].center.x = (centers[loc].center.x * c + center.x) / (c + 1);
|
| 119 | centers[loc].center.y = (centers[loc].center.y * c + center.y) / (c + 1);
|
| 120 | centers[loc].count++; |
| 121 | if (loc == count) count++;
|
| 122 | } |
| 123 | |
| 124 | if (DEBUG) cvShowImage( "mywindow1", image03 ); |
| 125 | |
| 126 | cvReleaseMemStorage(&storage); |
| 127 | |
| 128 | cvReleaseImage(&image03); |
| 129 | |
| 130 | VisionPosition* pos_array = (VisionPosition*)malloc(sizeof(VisionPosition) * count);
|
| 131 | if (pos_array == NULL) { |
| 132 | fprintf(stderr, "malloc failed\n");
|
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | int c = 0; |
| 137 | for (i = 0; i < count; i++) { |
| 138 | pos_array[c].x = centers[i].center.x; |
| 139 | pos_array[c].y = centers[i].center.y; |
| 140 | c++; |
| 141 | } |
| 142 | |
| 143 | *positions = pos_array; |
| 144 | return count;
|
| 145 | } |