2011-06-30 01:39:12 +08:00
|
|
|
/**
|
|
|
|
* @function Remap_Demo.cpp
|
|
|
|
* @brief Demo code for Remap
|
|
|
|
* @author Ana Huaman
|
|
|
|
*/
|
|
|
|
|
2014-07-04 22:48:15 +08:00
|
|
|
#include "opencv2/imgcodecs.hpp"
|
2011-06-30 01:39:12 +08:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
/// Global variables
|
|
|
|
Mat src, dst;
|
|
|
|
Mat map_x, map_y;
|
2012-11-07 22:21:20 +08:00
|
|
|
const char* remap_window = "Remap demo";
|
2011-06-30 01:39:12 +08:00
|
|
|
int ind = 0;
|
|
|
|
|
|
|
|
/// Function Headers
|
|
|
|
void update_map( void );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function main
|
|
|
|
*/
|
2012-11-07 22:21:20 +08:00
|
|
|
int main( int, char** argv )
|
2011-06-30 01:39:12 +08:00
|
|
|
{
|
|
|
|
/// Load the image
|
|
|
|
src = imread( argv[1], 1 );
|
|
|
|
|
|
|
|
/// Create dst, map_x and map_y with the same size as src:
|
|
|
|
dst.create( src.size(), src.type() );
|
|
|
|
map_x.create( src.size(), CV_32FC1 );
|
|
|
|
map_y.create( src.size(), CV_32FC1 );
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2011-06-30 01:39:12 +08:00
|
|
|
/// Create window
|
2013-04-08 02:45:38 +08:00
|
|
|
namedWindow( remap_window, WINDOW_AUTOSIZE );
|
2011-06-30 01:39:12 +08:00
|
|
|
|
|
|
|
/// Loop
|
2012-11-07 22:21:20 +08:00
|
|
|
for(;;)
|
2011-06-30 01:39:12 +08:00
|
|
|
{
|
|
|
|
/// Each 1 sec. Press ESC to exit the program
|
|
|
|
int c = waitKey( 1000 );
|
|
|
|
|
|
|
|
if( (char)c == 27 )
|
|
|
|
{ break; }
|
|
|
|
|
|
|
|
/// Update map_x & map_y. Then apply remap
|
|
|
|
update_map();
|
2013-04-06 22:16:51 +08:00
|
|
|
remap( src, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0) );
|
2011-06-30 01:39:12 +08:00
|
|
|
|
|
|
|
// Display results
|
|
|
|
imshow( remap_window, dst );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function update_map
|
|
|
|
* @brief Fill the map_x and map_y matrices with 4 types of mappings
|
|
|
|
*/
|
|
|
|
void update_map( void )
|
|
|
|
{
|
|
|
|
ind = ind%4;
|
|
|
|
|
|
|
|
for( int j = 0; j < src.rows; j++ )
|
|
|
|
{ for( int i = 0; i < src.cols; i++ )
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2011-06-30 01:39:12 +08:00
|
|
|
switch( ind )
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
if( i > src.cols*0.25 && i < src.cols*0.75 && j > src.rows*0.25 && j < src.rows*0.75 )
|
2011-06-30 01:39:12 +08:00
|
|
|
{
|
2012-11-07 22:21:20 +08:00
|
|
|
map_x.at<float>(j,i) = 2*( i - src.cols*0.25f ) + 0.5f ;
|
|
|
|
map_y.at<float>(j,i) = 2*( j - src.rows*0.25f ) + 0.5f ;
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ map_x.at<float>(j,i) = 0 ;
|
|
|
|
map_y.at<float>(j,i) = 0 ;
|
2011-06-30 01:39:12 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-10-17 07:18:30 +08:00
|
|
|
case 1:
|
2012-11-07 22:21:20 +08:00
|
|
|
map_x.at<float>(j,i) = (float)i ;
|
|
|
|
map_y.at<float>(j,i) = (float)(src.rows - j) ;
|
2012-10-17 07:18:30 +08:00
|
|
|
break;
|
2011-06-30 01:39:12 +08:00
|
|
|
case 2:
|
2012-11-07 22:21:20 +08:00
|
|
|
map_x.at<float>(j,i) = (float)(src.cols - i) ;
|
|
|
|
map_y.at<float>(j,i) = (float)j ;
|
2012-10-17 07:18:30 +08:00
|
|
|
break;
|
2011-06-30 01:39:12 +08:00
|
|
|
case 3:
|
2012-11-07 22:21:20 +08:00
|
|
|
map_x.at<float>(j,i) = (float)(src.cols - i) ;
|
|
|
|
map_y.at<float>(j,i) = (float)(src.rows - j) ;
|
2012-10-17 07:18:30 +08:00
|
|
|
break;
|
2011-06-30 01:39:12 +08:00
|
|
|
} // end of switch
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
2011-06-30 01:39:12 +08:00
|
|
|
}
|
|
|
|
ind++;
|
|
|
|
}
|