Morphology_1.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file Morphology_1.cpp
  3. * @brief Erosion and Dilation sample code
  4. * @author OpenCV team
  5. */
  6. #include "opencv2/imgproc.hpp"
  7. #include "opencv2/highgui.hpp"
  8. #include <iostream>
  9. using namespace cv;
  10. using namespace std;
  11. /// Global variables
  12. Mat src, erosion_dst, dilation_dst;
  13. int erosion_elem = 0;
  14. int erosion_size = 0;
  15. int dilation_elem = 0;
  16. int dilation_size = 0;
  17. int const max_elem = 2;
  18. int const max_kernel_size = 21;
  19. /** Function Headers */
  20. void Erosion( int, void* );
  21. void Dilation( int, void* );
  22. //![main]
  23. /**
  24. * @function main
  25. */
  26. int main( int argc, char** argv )
  27. {
  28. /// Load an image
  29. CommandLineParser parser( argc, argv, "{@input | LinuxLogo.jpg | input image}" );
  30. src = imread( samples::findFile( parser.get<String>( "@input" ) ), IMREAD_COLOR );
  31. if( src.empty() )
  32. {
  33. cout << "Could not open or find the image!\n" << endl;
  34. cout << "Usage: " << argv[0] << " <Input image>" << endl;
  35. return -1;
  36. }
  37. /// Create windows
  38. namedWindow( "Erosion Demo", WINDOW_AUTOSIZE );
  39. namedWindow( "Dilation Demo", WINDOW_AUTOSIZE );
  40. moveWindow( "Dilation Demo", src.cols, 0 );
  41. /// Create Erosion Trackbar
  42. createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion Demo",
  43. &erosion_elem, max_elem,
  44. Erosion );
  45. createTrackbar( "Kernel size:\n 2n +1", "Erosion Demo",
  46. &erosion_size, max_kernel_size,
  47. Erosion );
  48. /// Create Dilation Trackbar
  49. createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Dilation Demo",
  50. &dilation_elem, max_elem,
  51. Dilation );
  52. createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo",
  53. &dilation_size, max_kernel_size,
  54. Dilation );
  55. /// Default start
  56. Erosion( 0, 0 );
  57. Dilation( 0, 0 );
  58. waitKey(0);
  59. return 0;
  60. }
  61. //![main]
  62. //![erosion]
  63. /**
  64. * @function Erosion
  65. */
  66. void Erosion( int, void* )
  67. {
  68. int erosion_type = 0;
  69. if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
  70. else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
  71. else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
  72. //![kernel]
  73. Mat element = getStructuringElement( erosion_type,
  74. Size( 2*erosion_size + 1, 2*erosion_size+1 ),
  75. Point( erosion_size, erosion_size ) );
  76. //![kernel]
  77. /// Apply the erosion operation
  78. erode( src, erosion_dst, element );
  79. imshow( "Erosion Demo", erosion_dst );
  80. }
  81. //![erosion]
  82. //![dilation]
  83. /**
  84. * @function Dilation
  85. */
  86. void Dilation( int, void* )
  87. {
  88. int dilation_type = 0;
  89. if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
  90. else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
  91. else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
  92. Mat element = getStructuringElement( dilation_type,
  93. Size( 2*dilation_size + 1, 2*dilation_size+1 ),
  94. Point( dilation_size, dilation_size ) );
  95. /// Apply the dilation operation
  96. dilate( src, dilation_dst, element );
  97. imshow( "Dilation Demo", dilation_dst );
  98. }
  99. //![dilation]