run_length_morphology_demo.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include <iostream>
  2. #include "opencv2/imgproc.hpp"
  3. #include "opencv2/ximgproc.hpp"
  4. #include "opencv2/imgcodecs.hpp"
  5. #include "opencv2/highgui.hpp"
  6. using namespace std;
  7. using namespace cv;
  8. using namespace cv::ximgproc;
  9. // Adapted from cv_timer in cv_utilities
  10. class Timer
  11. {
  12. public:
  13. Timer() : start_(0), time_(0) {}
  14. void start()
  15. {
  16. start_ = cv::getTickCount();
  17. }
  18. void stop()
  19. {
  20. CV_Assert(start_ != 0);
  21. int64 end = cv::getTickCount();
  22. time_ += end - start_;
  23. start_ = 0;
  24. }
  25. double time()
  26. {
  27. double ret = time_ / cv::getTickFrequency();
  28. time_ = 0;
  29. return ret;
  30. }
  31. private:
  32. int64 start_, time_;
  33. };
  34. static void help()
  35. {
  36. printf("\nAllows to estimate the efficiency of the morphology operations implemented\n"
  37. "in ximgproc/run_length_morphology.cpp\n"
  38. "Call:\n example_ximgproc_run_length_morphology_demo [image] -u=factor_upscaling image\n"
  39. "Similar to the morphology2 sample of the main opencv library it shows the use\n"
  40. "of rect, ellipse and cross kernels\n\n"
  41. "As rectangular and cross-shaped structuring elements are highly optimized in opencv_imgproc module,\n"
  42. "only with elliptical structuring elements a speedup is possible (e.g. for larger circles).\n"
  43. "Run-length morphology has advantages for larger images.\n"
  44. "You can verify this by upscaling your input with e.g. -u=2\n");
  45. printf( "Hot keys: \n"
  46. "\tESC - quit the program\n"
  47. "\tr - use rectangle structuring element\n"
  48. "\te - use elliptic structuring element\n"
  49. "\tc - use cross-shaped structuring element\n"
  50. "\tSPACE - loop through all the options\n" );
  51. }
  52. static void print_introduction()
  53. {
  54. printf("\nFirst select a threshold for binarization.\n"
  55. "Then move the sliders for erosion/dilation or open/close operation\n\n"
  56. "The ratio between the time of the execution from opencv_imgproc\n"
  57. "and the code using run-length encoding will be displayed in the console\n\n");
  58. }
  59. Mat src, dst;
  60. int element_shape = MORPH_ELLIPSE;
  61. //the address of variable which receives trackbar position update
  62. int max_size = 40;
  63. int open_close_pos = 0;
  64. int erode_dilate_pos = 0;
  65. int nThreshold = 100;
  66. cv::Mat binaryImage;
  67. cv::Mat binaryRLE, dstRLE;
  68. cv::Mat rlePainted;
  69. static void PaintRLEToImage(cv::Mat& rleImage, cv::Mat& res, unsigned char uValue)
  70. {
  71. res = cv::Scalar(0);
  72. rl::paint(res, rleImage, Scalar((double) uValue));
  73. }
  74. static bool AreImagesIdentical(cv::Mat& image1, cv::Mat& image2)
  75. {
  76. cv::Mat diff;
  77. cv::absdiff(image1, image2, diff);
  78. int nDiff = cv::countNonZero(diff);
  79. return (nDiff == 0);
  80. }
  81. // callback function for open/close trackbar
  82. static void OpenClose(int, void*)
  83. {
  84. int n = open_close_pos - max_size;
  85. int an = n > 0 ? n : -n;
  86. Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an) );
  87. Timer timer;
  88. timer.start();
  89. if( n < 0 )
  90. morphologyEx(binaryImage, dst, MORPH_OPEN, element);
  91. else
  92. morphologyEx(binaryImage, dst, MORPH_CLOSE, element);
  93. timer.stop();
  94. double imgproc_duration = timer.time();
  95. element = rl::getStructuringElement(element_shape, Size(an * 2 + 1, an * 2 + 1));
  96. Timer timer2;
  97. timer2.start();
  98. if (n < 0)
  99. rl::morphologyEx(binaryRLE, dstRLE, MORPH_OPEN, element, true);
  100. else
  101. rl::morphologyEx(binaryRLE, dstRLE, MORPH_CLOSE, element, true);
  102. timer2.stop();
  103. double rl_duration = timer2.time();
  104. cout << "ratio open/close duration: " << rl_duration / imgproc_duration << " (run-length: "
  105. << rl_duration << ", pixelwise: " << imgproc_duration << " )" << std::endl;
  106. PaintRLEToImage(dstRLE, rlePainted, (unsigned char)255);
  107. if (!AreImagesIdentical(dst, rlePainted))
  108. {
  109. cout << "error result image are not identical" << endl;
  110. }
  111. imshow("Open/Close", rlePainted);
  112. }
  113. // callback function for erode/dilate trackbar
  114. static void ErodeDilate(int, void*)
  115. {
  116. int n = erode_dilate_pos - max_size;
  117. int an = n > 0 ? n : -n;
  118. Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an) );
  119. Timer timer;
  120. timer.start();
  121. if( n < 0 )
  122. erode(binaryImage, dst, element);
  123. else
  124. dilate(binaryImage, dst, element);
  125. timer.stop();
  126. double imgproc_duration = timer.time();
  127. element = rl::getStructuringElement(element_shape, Size(an*2+1, an*2+1));
  128. Timer timer2;
  129. timer2.start();
  130. if( n < 0 )
  131. rl::erode(binaryRLE, dstRLE, element, true);
  132. else
  133. rl::dilate(binaryRLE, dstRLE, element);
  134. timer2.stop();
  135. double rl_duration = timer2.time();
  136. PaintRLEToImage(dstRLE, rlePainted, (unsigned char)255);
  137. cout << "ratio erode/dilate duration: " << rl_duration / imgproc_duration <<
  138. " (run-length: " << rl_duration << ", pixelwise: " << imgproc_duration << " )" << std::endl;
  139. if (!AreImagesIdentical(dst, rlePainted))
  140. {
  141. cout << "error result image are not identical" << endl;
  142. }
  143. imshow("Erode/Dilate", rlePainted);
  144. }
  145. static void OnChangeThreshold(int, void*)
  146. {
  147. threshold(src, binaryImage, (double) nThreshold, 255.0, THRESH_BINARY );
  148. rl::threshold(src, binaryRLE, (double) nThreshold, THRESH_BINARY);
  149. imshow("Threshold", binaryImage);
  150. }
  151. int main( int argc, char** argv )
  152. {
  153. cv::CommandLineParser parser(argc, argv, "{help h||}{ @image | ../data/aloeL.jpg | }{u| |}");
  154. if (parser.has("help"))
  155. {
  156. help();
  157. return 0;
  158. }
  159. std::string filename = parser.get<std::string>("@image");
  160. cv::Mat srcIn;
  161. if( (srcIn = imread(filename,IMREAD_GRAYSCALE)).empty() )
  162. {
  163. help();
  164. return -1;
  165. }
  166. int nScale = 1;
  167. if (parser.has("u"))
  168. {
  169. int theScale = parser.get<int>("u");
  170. if (theScale > 1)
  171. nScale = theScale;
  172. }
  173. if (nScale == 1)
  174. src = srcIn;
  175. else
  176. cv::resize(srcIn, src, cv::Size(srcIn.rows * nScale, srcIn.cols * nScale));
  177. cout << "scale factor read " << nScale << endl;
  178. print_introduction();
  179. //create windows for output images
  180. namedWindow("Open/Close",1);
  181. namedWindow("Erode/Dilate",1);
  182. namedWindow("Threshold",1);
  183. open_close_pos = erode_dilate_pos = max_size - 10;
  184. createTrackbar("size s.e.", "Open/Close",&open_close_pos,max_size*2+1,OpenClose);
  185. createTrackbar("size s.e.", "Erode/Dilate",&erode_dilate_pos,max_size*2+1,ErodeDilate);
  186. createTrackbar("threshold", "Threshold",&nThreshold,255, OnChangeThreshold);
  187. OnChangeThreshold(0, 0);
  188. rlePainted.create(cv::Size(src.cols, src.rows), CV_8UC1);
  189. for(;;)
  190. {
  191. OpenClose(open_close_pos, 0);
  192. ErodeDilate(erode_dilate_pos, 0);
  193. char c = (char)waitKey(0);
  194. if( c == 27 )
  195. break;
  196. if( c == 'e' )
  197. element_shape = MORPH_ELLIPSE;
  198. else if( c == 'r' )
  199. element_shape = MORPH_RECT;
  200. else if( c == 'c' )
  201. element_shape = MORPH_CROSS;
  202. else if( c == ' ' )
  203. element_shape = (element_shape + 1) % 3;
  204. }
  205. return 0;
  206. }