Smoothing.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * file Smoothing.cpp
  3. * brief Sample code for simple filters
  4. * author OpenCV team
  5. */
  6. #include <iostream>
  7. #include "opencv2/imgproc.hpp"
  8. #include "opencv2/imgcodecs.hpp"
  9. #include "opencv2/highgui.hpp"
  10. using namespace std;
  11. using namespace cv;
  12. /// Global Variables
  13. int DELAY_CAPTION = 1500;
  14. int DELAY_BLUR = 100;
  15. int MAX_KERNEL_LENGTH = 31;
  16. Mat src; Mat dst;
  17. char window_name[] = "Smoothing Demo";
  18. /// Function headers
  19. int display_caption( const char* caption );
  20. int display_dst( int delay );
  21. /**
  22. * function main
  23. */
  24. int main( int argc, char ** argv )
  25. {
  26. namedWindow( window_name, WINDOW_AUTOSIZE );
  27. /// Load the source image
  28. const char* filename = argc >=2 ? argv[1] : "lena.jpg";
  29. src = imread( samples::findFile( filename ), IMREAD_COLOR );
  30. if (src.empty())
  31. {
  32. printf(" Error opening image\n");
  33. printf(" Usage:\n %s [image_name-- default lena.jpg] \n", argv[0]);
  34. return EXIT_FAILURE;
  35. }
  36. if( display_caption( "Original Image" ) != 0 )
  37. {
  38. return 0;
  39. }
  40. dst = src.clone();
  41. if( display_dst( DELAY_CAPTION ) != 0 )
  42. {
  43. return 0;
  44. }
  45. /// Applying Homogeneous blur
  46. if( display_caption( "Homogeneous Blur" ) != 0 )
  47. {
  48. return 0;
  49. }
  50. //![blur]
  51. for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
  52. {
  53. blur( src, dst, Size( i, i ), Point(-1,-1) );
  54. if( display_dst( DELAY_BLUR ) != 0 )
  55. {
  56. return 0;
  57. }
  58. }
  59. //![blur]
  60. /// Applying Gaussian blur
  61. if( display_caption( "Gaussian Blur" ) != 0 )
  62. {
  63. return 0;
  64. }
  65. //![gaussianblur]
  66. for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
  67. {
  68. GaussianBlur( src, dst, Size( i, i ), 0, 0 );
  69. if( display_dst( DELAY_BLUR ) != 0 )
  70. {
  71. return 0;
  72. }
  73. }
  74. //![gaussianblur]
  75. /// Applying Median blur
  76. if( display_caption( "Median Blur" ) != 0 )
  77. {
  78. return 0;
  79. }
  80. //![medianblur]
  81. for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
  82. {
  83. medianBlur ( src, dst, i );
  84. if( display_dst( DELAY_BLUR ) != 0 )
  85. {
  86. return 0;
  87. }
  88. }
  89. //![medianblur]
  90. /// Applying Bilateral Filter
  91. if( display_caption( "Bilateral Blur" ) != 0 )
  92. {
  93. return 0;
  94. }
  95. //![bilateralfilter]
  96. for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
  97. {
  98. bilateralFilter ( src, dst, i, i*2, i/2 );
  99. if( display_dst( DELAY_BLUR ) != 0 )
  100. {
  101. return 0;
  102. }
  103. }
  104. //![bilateralfilter]
  105. /// Done
  106. display_caption( "Done!" );
  107. return 0;
  108. }
  109. /**
  110. * @function display_caption
  111. */
  112. int display_caption( const char* caption )
  113. {
  114. dst = Mat::zeros( src.size(), src.type() );
  115. putText( dst, caption,
  116. Point( src.cols/4, src.rows/2),
  117. FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) );
  118. return display_dst(DELAY_CAPTION);
  119. }
  120. /**
  121. * @function display_dst
  122. */
  123. int display_dst( int delay )
  124. {
  125. imshow( window_name, dst );
  126. int c = waitKey ( delay );
  127. if( c >= 0 ) { return -1; }
  128. return 0;
  129. }