"OpenCV" remapping and affine transformation

Source: Internet
Author: User
re-mapping remapping
void Cv::remap  (   inputarray  src,  outputarray   DST,
                    inputarray  map1, Inputarray    MAP2,
                    int     interpolation, int  bordermode = border_constant,
                    const Scalar &  Bordervalue = Scalar () 
)   

description
    src: Source image
    DST: Target images, mapping parameters of the same size and
    map_x:x direction as SRC. It corresponds to the first parameter of Method H (i,j)
    map_y: The mapping parameter in the y direction. Note that the map_y and map_x are the same size as SRC.
    cv_inter_linear: Non-integer pixel coordinate interpolation flag. Here is the default value (bilinear interpolation).
    border_constant: Default


code example

#include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include <iostream
> Using namespace CV;

using namespace Std;
Mat SRC, DST;
Mat map_x, map_y;
char* Remap_window = "Remap Demo";

int ind = 0;

void Update_map (void); int main (int argc, char** argv) {commandlineparser parser (argc, argv, "{@image |. /data/lena.jpg |
    Input image} ");
    string filename = parser.get<string> (0);

    src = imread (filename, imread_color);
    Dst.create (Src.size (), Src.type ());         Map_x.create (Src.size (), CV_32FC1);

    32-bit floating-point single-channel matrix Map_y.create (Src.size (), CV_32FC1);
        while (true) {char c = (char) waitkey (1000);

        if (c = =) {break;}
        Update_map ();  Remap (src, DST, map_x, map_y, Inter_linear, Border_constant, Scalar (0, 0, 0));
    Remapping Imshow (Remap_window, DST);
return 0;

    } void Update_map (void) {IND = ind% 4; for (Int J =0;j<src.rows;j++) for (int i = 0; i < Src.cols;  i++) {switch (IND) {case 0://Image width is reduced by half and displayed in the middle if
                    (i > src.cols*0.25 && i < src.cols*0.75 && J > src.rows*0.25 && J < src.rows*0.75)
                        {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;
                        else {map_x.at<float> (j, I) = 0;
                    Map_y.at<float> (j, I) = 0;
                } break;
                    Case 1://Image Upside down map_x.at<float> (j, i) = (float) i;
                    Map_y.at<float> (j, i) = (float) (SRC.ROWS-J);
                Break
    Case 2://Image Upside down map_x.at<float> (j, i) = (float) (src.cols-i);                Map_y.at<float> (j, i) = (float) j;
                Break
                    Case 3://Perform operation B and C at the same time map_x.at<float> (j, i) = (float) (src.cols-i);
                    Map_y.at<float> (j, i) = (float) (SRC.ROWS-J);
            Break
}} ind++; }


Run results


affine transformation affine transformations

void Cv::warpaffine (   inputarray  src,  outputarray   DST,
                        inputarray  M,    Size  dsize,
                        int flags = inter_linear,int    Bordermode = border_constant,
                        const Scalar &  Bordervalue = Scalar ( )

Description
    src: input source image
    DST: Output image
    M  : affine transformation matrix
    dsize: size of output image


code example

#include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include <iostream
> Using namespace CV;

using namespace Std;
char* Source_window = "source image";
char* Warp_window = "warp";

char* Warp_rotate_window = "warp + rotate";          int main (int, char** argv) {point2f srctri[3];

    Triangle three vertices point2f dsttri[3];
    Mat Rot_mat (2, 3, CV_32FC1);
    Mat Warp_mat (2, 3, CV_32FC1);

    Mat src, warp_dst, warp_rotate_dst; char* filename = ".
    /data/lena.jpg ";

    src = imread (filename, imread_color);

    WARP_DST = Mat::zeros (Src.rows, Src.cols, Src.type ());                              Srctri[0] = point2f (0, 0);                 Image upper left corner srctri[1] = point2f (src.cols-1.f, 0);                 Image upper right corner srctri[2] = point2f (0, SRC.ROWS-1.F);
    The lower left corner of the image dsttri[0] = point2f (src.cols*0.0f, src.rows*0.33f);
    DSTTRI[1] = point2f (src.cols*0.85f, src.rows*0.25f);
DSTTRI[2] = point2f (src.cols*0.15f, src.rows*0.7f);
    Warp_mat = Getaffinetransform (Srctri, Dsttri);   Get affine matrix Warpaffine (SRC, warp_dst, Warp_mat, Warp_dst.size ());
    Affine transformation Point Center = Point (WARP_DST.COLS/2, WARP_DST.ROWS/2);
    Double angle =-50.0;

    Double scale = 0.6;

    Rot_mat = getrotationmatrix2d (center, angle, scale);    Warpaffine (WARP_DST, WARP_ROTATE_DST, Rot_mat, Warp_dst.size ());
    Rotational affine transformation imshow (Source_window, SRC);
    Imshow (Warp_window, WARP_DST);

    Imshow (Warp_rotate_window, WARP_ROTATE_DST);
    Waitkey (0);
return 0; }


Run results


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.