PageRenderTime 127ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/Vision/trunk/ObjectDetectionUtil/ObjectDetectionUtil/src/test_main.cpp

https://github.com/sdp-2011/sdp-12
C++ | 310 lines | 270 code | 33 blank | 7 comment | 37 complexity | 33bbe758126c1b3c52bcbd46c35b70e6 MD5 | raw file
  1. #include <cvaux.h>
  2. #include <highgui.h>
  3. #include <cxcore.h>
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <math.h>
  11. #include <cstring>
  12. #include <float.h>
  13. #include <limits.h>
  14. #include <time.h>
  15. #include <ctype.h>
  16. #include <iostream>
  17. #include "../../../ObjectDetection/src/objdetection.h"
  18. #include "objdetectionutil.h"
  19. #include <sstream>
  20. // MIN values for yellow robot
  21. int ty_min_1 = 0;
  22. int ty_min_2 = 8;
  23. int ty_min_3 = 75;
  24. // MAX values for yellow robot
  25. int ty_max_1 = 20;
  26. int ty_max_2 = 255;
  27. int ty_max_3 = 255;
  28. CvScalar o_hsv_min_TY = cvScalar(ty_min_1, ty_min_2, ty_min_3);
  29. CvScalar o_hsv_max_TY = cvScalar(ty_max_1, ty_max_2, ty_max_3);
  30. void launch(config conf)
  31. {
  32. CvMemStorage* storage=cvCreateMemStorage(0);
  33. IplImage* frame;
  34. IplImage* back_img;
  35. CvCapture* capture;
  36. bool back=false;
  37. ofstream* writer;
  38. if(conf.outputToText)
  39. writer=new ofstream(conf.outputfile);
  40. if(conf.camera)
  41. {
  42. std::cout<<"Going for Camera"<<std::endl;
  43. capture=cvCaptureFromCAM(0);
  44. }
  45. if(conf.image_file)
  46. {
  47. conf.i_base.current=conf.i_base.image_start;
  48. }
  49. while(true)
  50. {
  51. if(conf.camera)
  52. {
  53. if(conf.camera)
  54. {
  55. if(!back)
  56. {
  57. frame=cvLoadImage("bg.jpg");
  58. if(frame)
  59. {
  60. std::cout<<"background loaded"<<std::endl;
  61. }
  62. else
  63. {
  64. std::cout<<"Setup for Background image"<<std::endl;
  65. std::getchar();
  66. std::cout<<"grabbing frame"<<std::endl;
  67. if( !cvGrabFrame( capture ))
  68. {
  69. std::cout<<"Camera Stopped working, Closing"<<std::endl;
  70. break;
  71. }
  72. frame = cvRetrieveFrame( capture );
  73. std::cout<<"frame grabbed"<<std::endl;
  74. }
  75. }
  76. else
  77. {
  78. if( !cvGrabFrame( capture ))
  79. {
  80. std::cout<<"Camera Stopped working, Closing"<<std::endl;
  81. break;
  82. }
  83. frame = cvRetrieveFrame( capture );
  84. }
  85. }
  86. }
  87. if(conf.image_file)
  88. {
  89. std::stringstream ss;
  90. ss<<conf.i_base.basefile<<conf.i_base.current<<".jpg";
  91. std::string currentFile=ss.str();
  92. frame =cvLoadImage(currentFile.c_str(),1);
  93. std::cout<<"image: "<<conf.i_base.current<<std::endl;
  94. }
  95. if(!frame)
  96. {
  97. std::cout<<"Error: No Image, Closing"<<std::endl;
  98. break;
  99. }
  100. int w=540;
  101. int h=290;
  102. IplImage* buffer_frame = cvCreateImage(cvSize(frame->width,frame->height),frame->depth,3);
  103. IplImage* current_frame = cvCreateImage(cvSize(w,h), frame->depth,3);
  104. cvCopy(frame,buffer_frame);
  105. cvSetImageROI(buffer_frame,cvRect(80,110,w,h));
  106. cvCopy(buffer_frame,current_frame);
  107. cvResetImageROI(buffer_frame);
  108. IplImage* current_frame_pro_TY=NULL;
  109. cvReleaseImage(&buffer_frame);
  110. if(!back)
  111. {
  112. goto after_release;
  113. }
  114. //std::cout<<"Show: "<<<<conf.show<<std::endl;
  115. current_frame_pro_TY= objDetection::preprocess_to_single_channel(current_frame,back_img,o_hsv_min_TY,o_hsv_max_TY);
  116. cvShowImage("Thresholds",current_frame_pro_TY);
  117. //objDetection::utilities::colorPicker(current_frame_pro_TY);
  118. after_release:
  119. if(conf.show)
  120. cvShowImage( "Camera", current_frame );
  121. if(!back)
  122. {
  123. back=true;
  124. back_img=current_frame;
  125. }
  126. else
  127. {
  128. cvReleaseImage(&current_frame);
  129. }
  130. if( (cvWaitKey(50) & 255) == 27 ) break;
  131. if(conf.image_file)
  132. {
  133. cvReleaseImage(&frame);
  134. if(conf.i_base.current==conf.i_base.image_end)
  135. break;
  136. conf.i_base.current++;
  137. }
  138. cvClearMemStorage(storage);
  139. }
  140. if(conf.outputfile)
  141. writer->close();
  142. cvReleaseMemStorage(&storage);
  143. }
  144. config get_Config(int argc, char* argv[])
  145. {
  146. config res;
  147. res.camera=false;
  148. res.image_file=false;
  149. res.rankedArea=false;
  150. res.show=false;
  151. res.outputToConsole=false;
  152. res.outputToText=false;
  153. res.closeObjects=false;
  154. res.hsv_max_TY=o_hsv_max_TY;
  155. res.hsv_min_TY=o_hsv_min_TY;
  156. int currentIndex=1;
  157. if(argv[currentIndex][0]=='c')
  158. {
  159. res.camera=true;
  160. }
  161. if(argv[currentIndex][0]=='i')
  162. {
  163. res.image_file=true;
  164. res.i_base.current=-1;
  165. //go for image file
  166. }
  167. currentIndex++;
  168. if(res.image_file)
  169. {
  170. res.i_base.basefile=argv[currentIndex];
  171. currentIndex++;
  172. }
  173. if(res.image_file)
  174. {
  175. res.i_base.image_start=atoi(argv[currentIndex]);
  176. currentIndex++;
  177. }
  178. if(res.image_file)
  179. {
  180. res.i_base.step=atoi(argv[currentIndex]);
  181. currentIndex++;
  182. }
  183. if(res.image_file)
  184. {
  185. res.i_base.image_end=atoi(argv[currentIndex]);
  186. currentIndex++;
  187. }
  188. //currentIndex++;
  189. if(currentIndex==argc)
  190. {
  191. return res;
  192. }
  193. if(!strcmp(argv[currentIndex],"show"))
  194. res.show=true;
  195. if(!strcmp(argv[currentIndex],"outputToText"))
  196. {
  197. res.outputToText=true;
  198. currentIndex++;
  199. res.outputfile=argv[currentIndex];
  200. }
  201. if(!strcmp(argv[currentIndex],"outputToConsole"))
  202. res.outputToConsole=true;
  203. currentIndex++;
  204. if(currentIndex==argc)
  205. {
  206. return res;
  207. }
  208. if(!strcmp(argv[currentIndex],"show"))
  209. {
  210. res.show=true;
  211. }
  212. if(!strcmp(argv[currentIndex],"outputToText"))
  213. {
  214. res.outputToText=true;
  215. currentIndex++;
  216. res.outputfile=argv[currentIndex];
  217. }
  218. if(!strcmp(argv[currentIndex],"outputToConsole"))
  219. {
  220. res.outputToConsole=true;
  221. }
  222. currentIndex++;
  223. if(currentIndex==argc)
  224. {
  225. return res;
  226. }
  227. if(!strcmp(argv[currentIndex],"show"))
  228. {
  229. res.show=true;
  230. }
  231. if(!strcmp(argv[currentIndex],"outputToText"))
  232. {
  233. res.outputToText=true;
  234. currentIndex++;
  235. res.outputfile=argv[currentIndex];
  236. }
  237. if(!strcmp(argv[currentIndex],"outputToConsole"))
  238. {
  239. res.outputToConsole=true;
  240. }
  241. return res;
  242. }
  243. // Yellow robot trackbar setting
  244. void change_ty_min_1(int position){
  245. o_hsv_min_TY.val[0] = (double) position;
  246. }
  247. void change_ty_max_1(int position){
  248. o_hsv_max_TY.val[0] = (double) position;
  249. }
  250. void change_ty_min_2(int position){
  251. o_hsv_min_TY.val[1] = (double) position;
  252. }
  253. void change_ty_max_2(int position){
  254. o_hsv_max_TY.val[1] = (double) position;
  255. }
  256. void change_ty_min_3(int position){
  257. o_hsv_min_TY.val[2] = (double) position;
  258. }
  259. void change_ty_max_3(int position){
  260. o_hsv_max_TY.val[2] = (double) position;
  261. }
  262. int main(int argc, char* argv[])
  263. {
  264. cvNamedWindow( "Camera", CV_WINDOW_AUTOSIZE );
  265. cvNamedWindow("YellowThreshold",0);
  266. cvMoveWindow("YellowThreshold", 400, 600);
  267. cvCreateTrackbar( "Yellow_Min_Hue", "YellowThreshold", &ty_min_1, 255, &change_ty_min_1);
  268. cvCreateTrackbar( "Yellow_Max_Hue", "YellowThreshold", &ty_max_1, 255, &change_ty_max_1);
  269. cvCreateTrackbar( "Yellow_Min_Sat", "YellowThreshold", &ty_min_2, 255, &change_ty_min_2);
  270. cvCreateTrackbar( "Yellow_Max_Sat", "YellowThreshold", &ty_max_2, 255, &change_ty_max_2);
  271. cvCreateTrackbar( "Yellow_Min_Light", "YellowThreshold", &ty_min_3, 255, &change_ty_min_3);
  272. cvCreateTrackbar( "Yellow_Max_Light", "YellowThreshold", &ty_max_3, 255, &change_ty_max_3);
  273. config conf=get_Config(argc,argv);
  274. launch(conf);
  275. cvWaitKey(0);
  276. return 0;
  277. }