copyMakeBorder_demo.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @file copyMakeBorder_demo.cpp
  3. * @brief Sample code that shows the functionality of copyMakeBorder
  4. * @author OpenCV team
  5. */
  6. #include "opencv2/imgproc.hpp"
  7. #include "opencv2/imgcodecs.hpp"
  8. #include "opencv2/highgui.hpp"
  9. using namespace cv;
  10. //![variables]
  11. // Declare the variables
  12. Mat src, dst;
  13. int top, bottom, left, right;
  14. int borderType = BORDER_CONSTANT;
  15. const char* window_name = "copyMakeBorder Demo";
  16. RNG rng(12345);
  17. //![variables]
  18. /**
  19. * @function main
  20. */
  21. int main( int argc, char** argv )
  22. {
  23. //![load]
  24. const char* imageName = argc >=2 ? argv[1] : "lena.jpg";
  25. // Loads an image
  26. src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
  27. // Check if image is loaded fine
  28. if( src.empty()) {
  29. printf(" Error opening image\n");
  30. printf(" Program Arguments: [image_name -- default lena.jpg] \n");
  31. return -1;
  32. }
  33. //![load]
  34. // Brief how-to for this program
  35. printf( "\n \t copyMakeBorder Demo: \n" );
  36. printf( "\t -------------------- \n" );
  37. printf( " ** Press 'c' to set the border to a random constant value \n");
  38. printf( " ** Press 'r' to set the border to be replicated \n");
  39. printf( " ** Press 'ESC' to exit the program \n");
  40. //![create_window]
  41. namedWindow( window_name, WINDOW_AUTOSIZE );
  42. //![create_window]
  43. //![init_arguments]
  44. // Initialize arguments for the filter
  45. top = (int) (0.05*src.rows); bottom = top;
  46. left = (int) (0.05*src.cols); right = left;
  47. //![init_arguments]
  48. for(;;)
  49. {
  50. //![update_value]
  51. Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
  52. //![update_value]
  53. //![copymakeborder]
  54. copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
  55. //![copymakeborder]
  56. //![display]
  57. imshow( window_name, dst );
  58. //![display]
  59. //![check_keypress]
  60. char c = (char)waitKey(500);
  61. if( c == 27 )
  62. { break; }
  63. else if( c == 'c' )
  64. { borderType = BORDER_CONSTANT; }
  65. else if( c == 'r' )
  66. { borderType = BORDER_REPLICATE; }
  67. //![check_keypress]
  68. }
  69. return 0;
  70. }