Remap_Demo.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @function Remap_Demo.cpp
  3. * @brief Demo code for Remap
  4. * @author Ana Huaman
  5. */
  6. #include "opencv2/imgcodecs.hpp"
  7. #include "opencv2/highgui.hpp"
  8. #include "opencv2/imgproc.hpp"
  9. #include <iostream>
  10. using namespace cv;
  11. /// Function Headers
  12. void update_map( int &ind, Mat &map_x, Mat &map_y );
  13. /**
  14. * @function main
  15. */
  16. int main(int argc, const char** argv)
  17. {
  18. CommandLineParser parser(argc, argv, "{@image |chicky_512.png|input image name}");
  19. std::string filename = parser.get<std::string>(0);
  20. //! [Load]
  21. /// Load the image
  22. Mat src = imread( samples::findFile( filename ), IMREAD_COLOR );
  23. if (src.empty())
  24. {
  25. std::cout << "Cannot read image: " << filename << std::endl;
  26. return -1;
  27. }
  28. //! [Load]
  29. //! [Create]
  30. /// Create dst, map_x and map_y with the same size as src:
  31. Mat dst(src.size(), src.type());
  32. Mat map_x(src.size(), CV_32FC1);
  33. Mat map_y(src.size(), CV_32FC1);
  34. //! [Create]
  35. //! [Window]
  36. /// Create window
  37. const char* remap_window = "Remap demo";
  38. namedWindow( remap_window, WINDOW_AUTOSIZE );
  39. //! [Window]
  40. //! [Loop]
  41. /// Index to switch between the remap modes
  42. int ind = 0;
  43. for(;;)
  44. {
  45. /// Update map_x & map_y. Then apply remap
  46. update_map(ind, map_x, map_y);
  47. remap( src, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0) );
  48. /// Display results
  49. imshow( remap_window, dst );
  50. /// Each 1 sec. Press ESC to exit the program
  51. char c = (char)waitKey( 1000 );
  52. if( c == 27 )
  53. {
  54. break;
  55. }
  56. }
  57. //! [Loop]
  58. return 0;
  59. }
  60. /**
  61. * @function update_map
  62. * @brief Fill the map_x and map_y matrices with 4 types of mappings
  63. */
  64. //! [Update]
  65. void update_map( int &ind, Mat &map_x, Mat &map_y )
  66. {
  67. for( int i = 0; i < map_x.rows; i++ )
  68. {
  69. for( int j = 0; j < map_x.cols; j++ )
  70. {
  71. switch( ind )
  72. {
  73. case 0:
  74. if( j > map_x.cols*0.25 && j < map_x.cols*0.75 && i > map_x.rows*0.25 && i < map_x.rows*0.75 )
  75. {
  76. map_x.at<float>(i, j) = 2*( j - map_x.cols*0.25f ) + 0.5f;
  77. map_y.at<float>(i, j) = 2*( i - map_x.rows*0.25f ) + 0.5f;
  78. }
  79. else
  80. {
  81. map_x.at<float>(i, j) = 0;
  82. map_y.at<float>(i, j) = 0;
  83. }
  84. break;
  85. case 1:
  86. map_x.at<float>(i, j) = (float)j;
  87. map_y.at<float>(i, j) = (float)(map_x.rows - i);
  88. break;
  89. case 2:
  90. map_x.at<float>(i, j) = (float)(map_x.cols - j);
  91. map_y.at<float>(i, j) = (float)i;
  92. break;
  93. case 3:
  94. map_x.at<float>(i, j) = (float)(map_x.cols - j);
  95. map_y.at<float>(i, j) = (float)(map_x.rows - i);
  96. break;
  97. default:
  98. break;
  99. } // end of switch
  100. }
  101. }
  102. ind = (ind+1) % 4;
  103. }
  104. //! [Update]