Specific code as follows:
#include <opencv2/opencv.hpp> #include <iostream> #include <math.h> using namespace cv; Mat src, dst, map_x, map_y; const char* OUTPUT_TITLE = "remap demo"; int index = 0; //输入默认为0 void update_map(void); //申明函数 int main(int argc, char** argv) { src = imread("L:/5.jpg"); IF (! Src.data) { the printf ( " Could Not Load Image ... \ n- " ); return - . 1 ; } char input_win [] = " INPUT Image " ; namedWindow (input_win, CV_WINDOW_AUTOSIZE); namedWindow (OUTPUT_TITLE , CV_WINDOW_AUTOSIZE); imshow (input_win, the src); map_x.create (src.size (), CV_32FC1); // define the size and floating point map_x.size map_y.create (src.size (), CV_32FC1); / / every 500ms check the input loop is updated once int C = 0 ; the while (to true ) { C = waitKey ( 500 ); IF (( char ) C == 27 ) { // If the input is Esc to exit BREAK ; } index =% C 4 ; // input values taking modulo 4 remainder operation update_map ( ); // Call update_map () function remap (the src, DST, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, the Scalar ( 0 , 0 , 255 )); // remap function parameters: input image output image 34. 2. mapping table x, y 5. 6. default interpolation method 7. color imshow (OUTPUT_TITLE, DST); } return 0 ; } void update_map ( void ) { for ( int Row = 0 ; Row <src.rows; Row ++ ) { for ( int COL = 0 ; COL <src.cols; COL ++ ) { Switch (index) { Case 0 : // the original image is reduced by half, the center of the image is mapped to the IF (COL> (src.cols * 0.25 ) && COL <= (src.cols * 0.75 ) && Row> (src.rows * 0.25 ) && Row <= (* src.rows 0.75)) { map_x.at<float>(row, col) = 2 * (col - (src.cols*0.25)); map_y.at<float>(row, col) = 2 * (row - (src.rows*0.25)); } else { //边缘部分0像素填充 map_x.at<float>(row, col) = 0; map_y.at<float>(row, col) = 0; } break; case 1://上下颠倒 map_x.at<float>(row, col) = (src.cols - col - 1); map_y.at<float>(row, col) = row; break; case 2: //左右镜像 map_x.at<float>(row, col) = col; map_y.at<float>(row, col) = (src.rows - row - 1); break; case 3://上下左右颠倒 map_x.at<float>(row, col) = (src.cols - col - 1); map_y.at<float>(row, col) = (src.rows - row - 1); break; } } } }
result:
Press 0
Press 1:
Press 2:
Press 3: