PageRenderTime 79ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/saliency/src/label.cpp

https://github.com/jgrogers/Door-Sign-Detection
C++ | 204 lines | 184 code | 18 blank | 2 comment | 33 complexity | 74831eb84ff5899ded8244beba8ba0c8 MD5 | raw file
  1. #include <saliency.h>
  2. #include <boost/program_options.hpp>
  3. #include <boost/filesystem.hpp>
  4. #include <iostream>
  5. #include <Blob.h>
  6. #include <BlobResult.h>
  7. #include <highgui.h>
  8. #include <cv.h>
  9. #include <cvaux.h>
  10. #include <gethog.h>
  11. #include <svm.h>
  12. #include <image.h>
  13. namespace po = boost::program_options;
  14. struct mousedata {
  15. int cntr ;
  16. CvPoint p1;
  17. CvPoint p2;
  18. int num_pts;
  19. bool neg;
  20. } pp ={0,{},{},0,false};
  21. void on_mouse(int event, int x, int y, int flags, void* param) {
  22. switch (event) {
  23. case CV_EVENT_LBUTTONDOWN:
  24. {
  25. pp.p1 = cvPoint(x,y);
  26. pp.p2 = cvPoint(x,y);
  27. pp.num_pts = 1;
  28. pp.neg =false;
  29. break;
  30. }
  31. case CV_EVENT_LBUTTONUP:
  32. {
  33. pp.p2 = cvPoint(x,y);
  34. pp.num_pts = 2;
  35. break;
  36. }
  37. case CV_EVENT_RBUTTONDOWN:
  38. {
  39. pp.p1 = cvPoint(x,y);
  40. pp.p2 = cvPoint(x,y);
  41. pp.num_pts = 1;
  42. pp.neg = true;
  43. break;
  44. }
  45. case CV_EVENT_RBUTTONUP:
  46. {
  47. pp.p2 = cvPoint(x,y);
  48. pp.num_pts = 2;
  49. break;
  50. }
  51. case CV_EVENT_MOUSEMOVE:
  52. {
  53. if (flags & CV_EVENT_FLAG_LBUTTON ||
  54. flags & CV_EVENT_FLAG_RBUTTON) {
  55. pp.p2 = cvPoint(x,y);
  56. }
  57. break;
  58. }
  59. }
  60. }
  61. //returns bw image resized, also resizes source image
  62. void DrawCurrentRects(IplImage* img, const std::vector<CvRect>& pos,
  63. const std::vector<CvRect>& neg) {
  64. for (std::vector<CvRect>::const_iterator itr = pos.begin();
  65. itr != pos.end();
  66. itr++) {
  67. CvPoint p1 = cvPoint(itr->x,itr->y);
  68. CvPoint p2 = cvPoint(itr->x+itr->width,
  69. itr->y+itr->height);
  70. cvRectangle(img, p1, p2, CV_RGB(0,255,0), 1);
  71. }
  72. for (std::vector<CvRect>::const_iterator itr = neg.begin();
  73. itr != neg.end();
  74. itr++) {
  75. CvPoint p1 = cvPoint(itr->x,itr->y);
  76. CvPoint p2 = cvPoint(itr->x+itr->width,
  77. itr->y+itr->height);
  78. cvRectangle(img, p1, p2, CV_RGB(255,0,0), 1);
  79. }
  80. }
  81. void Save(FILE* fp, const std::string& img_name,
  82. const std::vector<CvRect>& pos,
  83. const std::vector<CvRect>& neg) {
  84. if (fp == NULL) return;
  85. for (std::vector<CvRect>::const_iterator itr = pos.begin();
  86. itr != pos.end();
  87. itr++) {
  88. fprintf(fp, "%s : P %u %u %u %u\n",
  89. img_name.c_str(),
  90. itr->x, itr->y, itr->width, itr->height);
  91. }
  92. for (std::vector<CvRect>::const_iterator itr = neg.begin();
  93. itr != neg.end();
  94. itr++) {
  95. fprintf(fp, "%s : N %u %u %u %u\n",
  96. img_name.c_str(),
  97. itr->x, itr->y, itr->width, itr->height);
  98. }
  99. }
  100. int main(int argc, char** argv) {
  101. unsigned int uint_opt;
  102. double double_opt;
  103. po::options_description desc("Allowed options");
  104. desc.add_options()
  105. ("help", "produce help message")
  106. ("img", po::value<std::string>(), "Label only this image")
  107. ("dir", po::value<std::string>(), "Label all images in directory")
  108. ("load", po::value<std::string>(), "clicks to load")
  109. ("save", po::value<std::string>(), "file to save the clicks in")
  110. ;
  111. po::variables_map vm;
  112. po::store(po::parse_command_line(argc, argv, desc),vm);
  113. po::notify(vm);
  114. if (vm.count("help")) {
  115. std::cout <<desc<<"\n";
  116. return 1;
  117. }
  118. FILE* save_fp = NULL;
  119. if (vm.count("save")) {
  120. save_fp = fopen(vm["save"].as<std::string>().c_str(), "a");
  121. }
  122. std::vector< CvRect> pos;
  123. std::vector< CvRect> neg;
  124. cvNamedWindow("Input",1);
  125. cvSetMouseCallback("Input", on_mouse);
  126. if (vm.count("dir")) {
  127. std::string thepath = vm["dir"].as<std::string>();
  128. if (!boost::filesystem::is_directory(thepath)) {
  129. printf("Give me a better path, not %s\n",vm["dir"].as<std::string>().c_str());
  130. return 1;
  131. }
  132. unsigned int key = -1;
  133. for (boost::filesystem::directory_iterator itr(thepath);
  134. itr != boost::filesystem::directory_iterator();
  135. ++itr) {
  136. if (!boost::filesystem::is_regular_file(itr->status())){
  137. continue;
  138. }
  139. std::string full_name = thepath + itr->path().filename();
  140. printf ("Loading %s\n",
  141. full_name.c_str());
  142. IplImage* img_in = cvLoadImage(full_name.c_str());
  143. IplImage* img_bw = TrainPrepImage(img_in);
  144. cvShowImage("Input", img_in);
  145. do {
  146. IplImage* img = cvCloneImage(img_in);
  147. DrawCurrentRects(img, pos,neg);
  148. if (pp.num_pts != 0) {
  149. if (pp.neg) cvRectangle(img, pp.p1, pp.p2, CV_RGB(255,0,0), 1);
  150. else cvRectangle(img, pp.p1, pp.p2, CV_RGB(0,255,0), 1);
  151. }
  152. if (pp.num_pts == 2) {
  153. pp.num_pts = 0;
  154. CvPoint UL = cvPoint(MIN(pp.p1.x, pp.p2.x),
  155. MIN(pp.p1.y, pp.p2.y));
  156. CvSize signsize = cvSize(abs(pp.p2.x-pp.p1.x),
  157. abs(pp.p2.y-pp.p1.y));
  158. if (!signsize.width ||
  159. !signsize.height) {
  160. printf("too small!\n");
  161. continue;
  162. }
  163. CvRect newrect = cvRect(UL.x,UL.y,signsize.width,signsize.height);
  164. if (pp.neg) neg.push_back(newrect);
  165. else pos.push_back(newrect);
  166. }
  167. cvShowImage("Input", img);
  168. key = cvWaitKey(30);
  169. cvReleaseImage(&img);
  170. if ((char)key == 'u') {
  171. pos.clear();
  172. neg.clear();
  173. }
  174. }while ((char) key != ' ' &&
  175. (char) key != 'd');
  176. //next image?
  177. Save(save_fp ,full_name,
  178. pos,neg);
  179. pos.clear();
  180. neg.clear();
  181. cvReleaseImage(&img_in);
  182. cvReleaseImage(&img_bw);
  183. }
  184. }
  185. return -1;
  186. }