Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame^] | 1 | #include <math.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <vector> |
| 5 | |
| 6 | #include "../CameraProcessor.h" |
| 7 | #include "../SensorProcessor.h" |
| 8 | |
| 9 | #include "opencv2/highgui/highgui.hpp" |
| 10 | |
| 11 | const int num_names = 39; |
| 12 | |
| 13 | |
| 14 | static const bool USE_ROTATED = true; |
| 15 | static const int use_width = 320; |
| 16 | static const int use_height = 240; |
| 17 | const char * image_names[num_names] = { |
| 18 | "NASA_bmp/img26_series_b_side_204_e65.jpg", |
| 19 | "NASA_bmp/img19_series_b_side_110_e65.jpg", |
| 20 | "NASA_bmp/img12_series_b_center_224_e65.jpg", |
| 21 | "NASA_bmp/img23_series_b_side_101_e65.jpg", |
| 22 | "NASA_bmp/img15_series_b_side_230_e65.jpg", |
| 23 | "NASA_bmp/img10_series_b_center_203_e65.jpg", |
| 24 | "NASA_bmp/img11_series_b_center_203_e65.jpg", |
| 25 | "NASA_bmp/img13_series_b_center_260_e65.jpg", |
| 26 | "NASA_bmp/img14_series_b_center_251_e65.jpg", |
| 27 | "NASA_bmp/img16_series_b_side_196_e65.jpg", |
| 28 | "NASA_bmp/img17_series_b_side_160_e65.jpg", |
| 29 | "NASA_bmp/img18_series_b_side_140_e65.jpg", |
| 30 | "NASA_bmp/img1_center_200_e65.jpg", |
| 31 | "NASA_bmp/img20_series_b_side_114_e65.jpg", |
| 32 | "NASA_bmp/img21_series_b_side_137_e65.jpg", |
| 33 | "NASA_bmp/img22_center field_e10.jpg", |
| 34 | "NASA_bmp/img22_dog Center Field_e10.jpg", |
| 35 | "NASA_bmp/img22_series_b_side_150_e65.jpg", |
| 36 | "NASA_bmp/img23_center field_e10.jpg", |
| 37 | "NASA_bmp/img24_center field_e10.jpg", |
| 38 | "NASA_bmp/img24_series_b_side_104_e65.jpg", |
| 39 | "NASA_bmp/img25_series_b_side_195_e65.jpg", |
| 40 | "NASA_bmp/img27_series_b_side_192_e65.jpg", |
| 41 | "NASA_bmp/img28_series_b_side_192_e65.jpg", |
| 42 | "NASA_bmp/img29_series_b_side_186_e65.jpg", |
| 43 | "NASA_bmp/img2_center_207_e65.jpg", |
| 44 | "NASA_bmp/img30_series_b_side_177_e65.jpg", |
| 45 | "NASA_bmp/img31_series_b_side_176_e65.jpg", |
| 46 | "NASA_bmp/img32_series_b_side_212_e65.jpg", |
| 47 | "NASA_bmp/img33_series_b_side_251_e65.jpg", |
| 48 | "NASA_bmp/img34_series_b_side_272_e65.jpg", |
| 49 | "NASA_bmp/img35_series_b_side_23+219_e65.jpg", |
| 50 | "NASA_bmp/img3_center_180_e65.jpg", |
| 51 | "NASA_bmp/img4_series_b_center_106_e65.jpg", |
| 52 | "NASA_bmp/img5_series_b_center_122_e65.jpg", |
| 53 | "NASA_bmp/img6_series_b_center_145_e65.jpg", |
| 54 | "NASA_bmp/img7_series_b_center_174_e65.jpg", |
| 55 | "NASA_bmp/img8_series_b_center_196_e65.jpg", |
| 56 | "NASA_bmp/img9_series_b_center_201_e65.jpg"}; |
| 57 | |
| 58 | const char * WINDOW_NAME = "Treshhold Window"; |
| 59 | const char * WINDOW_NAME2 = "Target Window"; |
| 60 | |
| 61 | |
| 62 | static void onMouse( int event, int x, int y, int, void* userData ) { |
| 63 | if( event != CV_EVENT_LBUTTONDOWN ) return; |
| 64 | ProcessorData *proc = (ProcessorData *) userData; |
| 65 | IplImage *image = proc->src_header_image; |
| 66 | uchar b = *((uchar*) (image->imageData + y*image->widthStep + 3*x)); |
| 67 | uchar g = *((uchar*) (image->imageData + y*image->widthStep + 3*(x+1))); |
| 68 | uchar r = *((uchar*) (image->imageData + y*image->widthStep + 3*(x+2))); |
| 69 | |
| 70 | uchar h=0; |
| 71 | uchar s=0; |
| 72 | uchar v=0; |
| 73 | proc->RGBtoHSV(r, g, b, &h, &s, &v); |
| 74 | |
| 75 | |
| 76 | *((uchar*) (image->imageData + y*image->widthStep + 3*x)) = 128; |
| 77 | *((uchar*) (image->imageData + y*image->widthStep + 3*(x+1))) = 128; |
| 78 | *((uchar*) (image->imageData + y*image->widthStep + 3*(x+2))) = 255; |
| 79 | |
| 80 | cv::Mat src(image); |
| 81 | //cv::imshow("test", src); |
| 82 | |
| 83 | printf("got click (%d,%d)= <%d,%d,%d> -- [%d,%d,%d]\n", |
| 84 | x, y, r, g, b, h, s, v); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | int main( int argc, char *argv[] ){ |
| 89 | ProcessorData processor(use_width, use_height, USE_ROTATED); |
| 90 | int img_cycle = 0; |
| 91 | int thresh = 100; |
| 92 | |
| 93 | cvStartWindowThread(); |
| 94 | |
| 95 | cvNamedWindow ("cnt", CV_WINDOW_AUTOSIZE); |
| 96 | cvNamedWindow ("GLOBAL", CV_WINDOW_AUTOSIZE); |
| 97 | //cvNamedWindow ("Grey Img", CV_WINDOW_AUTOSIZE); |
| 98 | //cvNamedWindow ("test", CV_WINDOW_AUTOSIZE); |
| 99 | cvNamedWindow (WINDOW_NAME2, CV_WINDOW_AUTOSIZE); |
| 100 | cvNamedWindow (WINDOW_NAME, CV_WINDOW_AUTOSIZE); |
| 101 | |
| 102 | cvMoveWindow(WINDOW_NAME,0,0); |
| 103 | cvMoveWindow("GLOBAL",325,0); |
| 104 | cvMoveWindow(WINDOW_NAME2,650,0); |
| 105 | //cvMoveWindow("Grey Img", 0, 275); |
| 106 | //cvMoveWindow("test", 325, 275); |
| 107 | cvMoveWindow("cnt",1100,100); |
| 108 | //Creating the trackbars |
| 109 | cvCreateTrackbar("H1","cnt",&processor.h1,360,0); |
| 110 | cvCreateTrackbar("H2","cnt",&processor.h2,360,0); |
| 111 | cvCreateTrackbar("S1","cnt",&processor.s1,255,0); |
| 112 | cvCreateTrackbar("S2","cnt",&processor.s2,255,0); |
| 113 | cvCreateTrackbar("V1","cnt",&processor.v1,255,0); |
| 114 | cvCreateTrackbar("V2","cnt",&processor.v2,255,0); |
| 115 | |
| 116 | while (img_cycle >= 0) { |
| 117 | processor.clear(); |
| 118 | printf("%d = %s\n", img_cycle, image_names[img_cycle]); |
| 119 | processor.src_header_image = cvLoadImage(image_names[img_cycle]); |
| 120 | cvCopy(processor.src_header_image, processor.global_display); |
| 121 | |
| 122 | cv::setMouseCallback( WINDOW_NAME2, onMouse, |
| 123 | (void *)&processor ); |
| 124 | |
| 125 | cv::Mat global_mat(processor.global_display); |
| 126 | cv::Mat src_mat(processor.src_header_image); |
| 127 | |
| 128 | // These lines are the vision processing, the rest of main |
| 129 | // is just fluff |
| 130 | processor.threshold((uchar *) |
| 131 | processor.src_header_image->imageData); |
| 132 | processor.getContours(); |
| 133 | processor.filterToTargets(); |
| 134 | |
| 135 | if(!processor.target_list.empty()){ |
| 136 | std::vector<std::vector<cv::Point> > target_contours; |
| 137 | std::vector<std::vector<cv::Point> > best_contours; |
| 138 | std::vector<std::vector<cv::Point> > raw_contours; |
| 139 | std::vector<Target>::iterator target_it; |
| 140 | Target *best_target = NULL; |
| 141 | int i = 0; |
| 142 | for(target_it = processor.target_list.begin(); |
| 143 | target_it != processor.target_list.end(); target_it++){ |
| 144 | target_contours.push_back(target_it->this_contour); |
| 145 | raw_contours.push_back(target_it->raw_contour); |
| 146 | printf("%d: h=%.1f, interp=%.1f, <x,y>=<%.1f,%.1f>\n", |
| 147 | i++, target_it->height, |
| 148 | interpolate(4, &pixel_to_dist[0], target_it->rect.centroid.x), |
| 149 | target_it->rect.centroid.x, target_it->rect.centroid.y); |
| 150 | if (best_target == NULL) { |
| 151 | best_target = &*target_it; |
| 152 | } else { |
| 153 | if (target_it->height > best_target->height) { |
| 154 | best_target = &*target_it; |
| 155 | } |
| 156 | /* if (processor.is_90) { |
| 157 | if (target_it->rect.centroid.x > best_target->rect.centroid.x) { |
| 158 | best_target = &*target_it; |
| 159 | } |
| 160 | } else { |
| 161 | if (target_it->rect.centroid.y < best_target->rect.centroid.y) { |
| 162 | best_target = &*target_it; |
| 163 | } |
| 164 | }*/ |
| 165 | } |
| 166 | } |
| 167 | best_contours.push_back(best_target->this_contour); |
| 168 | //drawContours(global_mat,target_contours,-1,color,CV_FILLED); |
| 169 | cv::imshow(WINDOW_NAME, src_mat); |
| 170 | //cv::imshow("Grey Img", *processor.grey_mat); |
| 171 | cv::Scalar color(0,0,255); |
| 172 | cv::drawContours( src_mat, target_contours, -1, color, CV_FILLED ); |
| 173 | cv::Scalar color2(128,0,255); |
| 174 | cv::drawContours( src_mat, best_contours, -1, color2, CV_FILLED ); |
| 175 | cv::Scalar color3(0,255,0); |
| 176 | cv::drawContours( src_mat, raw_contours, -1, color3, 1 ); |
| 177 | } |
| 178 | //cv::Mat grey_mat(grey_image); |
| 179 | //cv::imshow(WINDOW_NAME2, grey_mat); |
| 180 | cv::imshow("GLOBAL", global_mat); |
| 181 | cv::imshow(WINDOW_NAME2, src_mat); |
| 182 | char key = cvWaitKey(3000); |
| 183 | switch (key) { |
| 184 | case ' ': |
| 185 | img_cycle++; |
| 186 | img_cycle = img_cycle % num_names; |
| 187 | printf("%c %d= %s\n", key, img_cycle, image_names[img_cycle]); |
| 188 | break; |
| 189 | case 'g': |
| 190 | thresh++; |
| 191 | thresh = (thresh % 255); |
| 192 | printf("+ thresh= %d\n", thresh); |
| 193 | break; |
| 194 | case 'G': |
| 195 | thresh--; |
| 196 | thresh = (thresh % 255); |
| 197 | printf("- thresh= %d\n", thresh); |
| 198 | break; |
| 199 | case 'q': |
| 200 | img_cycle = -1; |
| 201 | break; |
| 202 | default: |
| 203 | break; |
| 204 | } |
| 205 | //redraw image cuz we drew all over it |
| 206 | } |
| 207 | |
| 208 | cvDestroyWindow(WINDOW_NAME); |
| 209 | } |
| 210 | |