copyMakeBorder_demo.cpp 2.1 KB
Newer Older
1 2 3 4 5 6
/**
 * @file copyMakeBorder_demo.cpp
 * @brief Sample code that shows the functionality of copyMakeBorder
 * @author OpenCV team
 */

7
#include "opencv2/imgproc.hpp"
8
#include "opencv2/imgcodecs.hpp"
9
#include "opencv2/highgui.hpp"
10 11 12

using namespace cv;

13
//![variables]
14 15 16
Mat src, dst;
int top, bottom, left, right;
int borderType;
17
const char* window_name = "copyMakeBorder Demo";
18
RNG rng(12345);
19
//![variables]
20 21 22 23

/**
 * @function main
 */
24
int main( int argc, char** argv )
25
{
26
  //![load]
27 28 29 30 31 32
  String imageName("../data/lena.jpg"); // by default
  if (argc > 1)
  {
      imageName = argv[1];
  }
  src = imread( imageName, IMREAD_COLOR ); // Load an image
33

34
  if( src.empty() )
35
    {
36
      printf(" No data entered, please enter the path to an image file \n");
37
      return -1;
38
    }
39
  //![load]
40 41 42 43 44 45 46 47

  /// Brief how-to for this program
  printf( "\n \t copyMakeBorder Demo: \n" );
  printf( "\t -------------------- \n" );
  printf( " ** Press 'c' to set the border to a random constant value \n");
  printf( " ** Press 'r' to set the border to be replicated \n");
  printf( " ** Press 'ESC' to exit the program \n");

48
  //![create_window]
49
  namedWindow( window_name, WINDOW_AUTOSIZE );
50
  //![create_window]
51

52
  //![init_arguments]
53
  /// Initialize arguments for the filter
54
  top = (int) (0.05*src.rows); bottom = (int) (0.05*src.rows);
55
  left = (int) (0.05*src.cols); right = (int) (0.05*src.cols);
56
  //![init_arguments]
57

58
  dst = src;
59
  imshow( window_name, dst );
60

61
  for(;;)
62
       {
63
         //![check_keypress]
S
StevenPuttemans 已提交
64 65
         char c = (char)waitKey(500);
         if( c == 27 )
66
           { break; }
S
StevenPuttemans 已提交
67
         else if( c == 'c' )
68
           { borderType = BORDER_CONSTANT; }
S
StevenPuttemans 已提交
69
         else if( c == 'r' )
70
           { borderType = BORDER_REPLICATE; }
71
         //![check_keypress]
72

73
         //![update_value]
74
         Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
75 76 77
         //![update_value]

         //![copymakeborder]
78
         copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
79
         //![copymakeborder]
80

81
         //![display]
82
         imshow( window_name, dst );
83
         //![display]
84 85 86 87
       }

  return 0;
}