morphology2.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "opencv2/imgproc.hpp"
  2. #include "opencv2/imgcodecs.hpp"
  3. #include "opencv2/highgui.hpp"
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string>
  7. using namespace cv;
  8. static void help(char** argv)
  9. {
  10. printf("\nShow off image morphology: erosion, dialation, open and close\n"
  11. "Call:\n %s [image]\n"
  12. "This program also shows use of rect, ellipse and cross kernels\n\n", argv[0]);
  13. printf( "Hot keys: \n"
  14. "\tESC - quit the program\n"
  15. "\tr - use rectangle structuring element\n"
  16. "\te - use elliptic structuring element\n"
  17. "\tc - use cross-shaped structuring element\n"
  18. "\tSPACE - loop through all the options\n" );
  19. }
  20. Mat src, dst;
  21. int element_shape = MORPH_RECT;
  22. //the address of variable which receives trackbar position update
  23. int max_iters = 10;
  24. int open_close_pos = 0;
  25. int erode_dilate_pos = 0;
  26. // callback function for open/close trackbar
  27. static void OpenClose(int, void*)
  28. {
  29. int n = open_close_pos;
  30. int an = abs(n);
  31. Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an) );
  32. if( n < 0 )
  33. morphologyEx(src, dst, MORPH_OPEN, element);
  34. else
  35. morphologyEx(src, dst, MORPH_CLOSE, element);
  36. imshow("Open/Close",dst);
  37. }
  38. // callback function for erode/dilate trackbar
  39. static void ErodeDilate(int, void*)
  40. {
  41. int n = erode_dilate_pos;
  42. int an = abs(n);
  43. Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an) );
  44. if( n < 0 )
  45. erode(src, dst, element);
  46. else
  47. dilate(src, dst, element);
  48. imshow("Erode/Dilate",dst);
  49. }
  50. int main( int argc, char** argv )
  51. {
  52. cv::CommandLineParser parser(argc, argv, "{help h||}{ @image | baboon.jpg | }");
  53. if (parser.has("help"))
  54. {
  55. help(argv);
  56. return 0;
  57. }
  58. std::string filename = samples::findFile(parser.get<std::string>("@image"));
  59. if( (src = imread(filename,IMREAD_COLOR)).empty() )
  60. {
  61. help(argv);
  62. return -1;
  63. }
  64. //create windows for output images
  65. namedWindow("Open/Close",1);
  66. namedWindow("Erode/Dilate",1);
  67. open_close_pos = erode_dilate_pos = max_iters;
  68. createTrackbar("iterations", "Open/Close",&open_close_pos,max_iters*2+1,OpenClose);
  69. setTrackbarMin("iterations", "Open/Close", -max_iters);
  70. setTrackbarMax("iterations", "Open/Close", max_iters);
  71. setTrackbarPos("iterations", "Open/Close", 0);
  72. createTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate);
  73. setTrackbarMin("iterations", "Erode/Dilate", -max_iters);
  74. setTrackbarMax("iterations", "Erode/Dilate", max_iters);
  75. setTrackbarPos("iterations", "Erode/Dilate", 0);
  76. for(;;)
  77. {
  78. OpenClose(open_close_pos, 0);
  79. ErodeDilate(erode_dilate_pos, 0);
  80. char c = (char)waitKey(0);
  81. if( c == 27 )
  82. break;
  83. if( c == 'e' )
  84. element_shape = MORPH_ELLIPSE;
  85. else if( c == 'r' )
  86. element_shape = MORPH_RECT;
  87. else if( c == 'c' )
  88. element_shape = MORPH_CROSS;
  89. else if( c == ' ' )
  90. element_shape = (element_shape + 1) % 3;
  91. }
  92. return 0;
  93. }