opencv image remapping cv::remap() (image up and down, left and right mirroring)


concept

What is remapping?
This is the process of taking pixels from one location in an image and positioning them to another location in a new image.
To complete the mapping process, some interpolation of non-integer pixel positions may be required, since there is not always a one-to-one pixel correspondence between the source and destination images.
We can express the remapping ( x , y ) for each pixel location as:
insert image description here
where g() is the mapped image, f() is the source image, and h(x, y) is the mapping function applied to (x, y) .

opencv function supports cv::remap()

1. Function prototype:

CV_EXPORTS_W void remap( InputArray src, OutputArray dst,
                         InputArray map1, InputArray map2,
                         int interpolation, int borderMode = BORDER_CONSTANT,
                         const Scalar& borderValue = Scalar());

2. Parameter description

src: source image.
dst: target image. It has the same size as map1 and the same type as src.
map1: The coordinate value of x when map1 is stored, there are types CV_16SC2,
CV_32FC1, or CV_32FC2
CV_32FC1 or CV_32FC2. For more information on floating point conversions, see ConvertMaps
Representation to Fixed Point Speed.
map2: map2 stores the y coordinate value, which has the type CV_16SC2,
CV_32FC1, or CV_32FC2
interpolation: interpolation method (see #InterpolationFlags). Method #INTER_AREA is
not supported by this function.
borderMode: pixel extrapolation method (see #BorderTypes) when
borderMode: border handling mode
borderValue: value to use for borders. The default value is 0.

Test code:

   cv::Mat src;
    src = cv::imread("D:\\QtProject\\Opencv_Example\\remap\\remap.png", cv::IMREAD_GRAYSCALE);
    if (src.empty()) {
    
    
        cout << "matTemplate Cannot load image" << endl;
        return;
    }
    cv::imshow("src", src);

    cv::Mat dstUpDown(src.size(), src.type());
    cv::Mat map_x(src.size(), CV_32FC1);
    cv::Mat map_y(src.size(), CV_32FC1);

    //上下镜像
    for( int i = 0; i < map_x.rows; i++ )
     {
    
    
         for( int j = 0; j < map_x.cols; j++ )
         {
    
    
            map_x.at<float>(i, j) = (float)j;
            map_y.at<float>(i, j) = (float)(map_x.rows - i);
         }
     }

    cv::remap( src, dstUpDown, map_x, map_y, cv::INTER_LINEAR, cv::BORDER_CONSTANT, cv::Scalar(0));
    cv::imshow("dstUpDown", dstUpDown);

    //左右镜像
    for( int i = 0; i < map_x.rows; i++ )
     {
    
    
         for( int j = 0; j < map_x.cols; j++ )
         {
    
    
            map_x.at<float>(i, j) = (float)(map_x.cols - j);
            map_y.at<float>(i, j) = (float)i;
         }
     }
    cv::Mat dstLeftRight(src.size(), src.type());
    cv::remap( src, dstLeftRight, map_x, map_y, cv::INTER_LINEAR, cv::BORDER_CONSTANT, cv::Scalar(0));
    cv::imshow("dstLeftRight", dstLeftRight);

Program running effect:

Mirror up and down: mirror
insert image description here
left and right
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44901043/article/details/123486972