Opencv affine transformation and perspective transformation

Source: Internet
Author: User

It is basically the answer to Exercise 7 in Chapter 4th of learning opencv.

First, you can write your own perspective conversion code:

// Perspective conversion <br/> # include <cv. h> <br/> # include <pighgui. h> <br/> # include <math. h> </P> <p> int main (INT argc, char ** argv) <br/> {<br/> cvpoint2d32f srcquad [4], dstquad [4]; <br/> cvmat * warp_matrix = cvcreatemat (3,3, cv_32fc1); <br/> iplimage * SRC, * DST; <br/> src = cvloadimage (".. /lena.jpg ", 1); <br/> If (! SRC) <br/> return-1; <br/> cvnamedwindow ("perspective_warp", 1); <br/> int presskey; <br/> int DF1 (1 ), df2 (1), df3 (1), df4 (1), df5 (1), df6 (1), df7 (1), df8 (1 ); <br/> DF1 = 5; df2 = 33; df3 = 90; df4 = 25; df5 = 20; df6 = 70; df7 = 80; df8 = 90; <br/> // DF1 = df2 = df3 = df4 = df5 = df6 = df7 = df8 = 0; <br/> DST = cvcloneimage (SRC ); <br/> DST-> origin = Src-> origin; <br/> cvzero (DST); </P> <p> srcquad [0]. X = 0; <br/> srcquad [0]. y = 0; <br/> srcquad [1]. X = Src-> width-1; <br/> srcquad [1]. y = 0; <br/> srcquad [2]. X = 0; <br/> srcquad [2]. y = Src-> height-1; <br/> srcquad [3]. X = Src-> width-1; <br/> srcquad [3]. y = Src-> height-1; <br/> float Delta = 0.01; <br/> while (1) <br/> {<br/> // dstquad [0]. X = Src-> width * Delta * (DF1% 101); <br/> // dstquad [0]. y = Src-> height * Delta * (df2% 101); <br/> // dstquad [1]. X = Src-> width-1 + Src-> width * Delta * (df3% 101); <br/> // dstquad [1]. y = Src-> height * Delta * (df4% 101); <br/> // dstquad [2]. X = Src-> width * Delta * (df5% 101); <br/> // dstquad [2]. y = Src-> height-1 + Src-> height * Delta * (df6% 101); <br/> // dstquad [3]. X = Src-> width-1 + Src-> width * Delta * (df7% 101); <br/> // dstquad [3]. y = Src-> height-1 + Src-> height * Delta * (df8% 101); </P> <p> dstquad [0]. X = Src-> width * Delta * ABS (DF1% 101); <br/> dstquad [0]. y = Src-> height * Delta * ABS (df2% 101); <br/> dstquad [1]. X = Src-> width * Delta * ABS (df3% 101); <br/> dstquad [1]. y = Src-> height * Delta * ABS (df4% 101); <br/> dstquad [2]. X = Src-> width * Delta * ABS (df5% 101); <br/> dstquad [2]. y = Src-> height * Delta * ABS (df6% 101); <br/> dstquad [3]. X = Src-> width * Delta * ABS (df7% 101); <br/> dstquad [3]. y = Src-> height * Delta * ABS (df8% 101); </P> <p> cvgetperspectivetransform (srcquad, dstquad, warp_matrix); <br/> cvwarpperspective (SRC, DST, warp_matrix); <br/> // -------------------------- character display period <br/> cvfont font = cvfont (1, 1); <br/> char Buf [8]; <br/> char dspstr1 [32] = {'/0'}; // <br/> char dspstr2 [32] = {'/0 '}; // <br/> char dspstr3 [32] = {'/0'}; // </P> <p> memset (BUF,'/0 ', sizeof (BUF); <br/> strcat (dspstr1, "|"); <br/> strcat (dspstr1, ITOA (ABS (df6% 101), Buf, 10); <br/> strcat (dspstr1, ","); <br/> strcat (dspstr1, ITOA (ABS (df7% 101), Buf, 10 )); <br/> strcat (dspstr1, ","); <br/> strcat (dspstr1, ITOA (ABS (df8% 101), Buf, 10 )); <br/> strcat (dspstr1, "|"); </P> <p> strcat (dspstr2, "|"); <br/> strcat (dspstr2, ITOA (ABS (df4% 101), Buf, 10); <br/> strcat (dspstr2, ", 00,"); <br/> strcat (dspstr2, ITOA (ABS (df5% 101), Buf, 10); <br/> strcat (dspstr2, "| "); <br/> // "zookeeper | <br/> strcat (dspstr3," | "); <br/> strcat (dspstr3, ITOA (ABS (DF1% 101), Buf, 10); <br/> strcat (dspstr3, ","); <br/> strcat (dspstr3, ITOA (ABS (df2% 101), Buf, 10); <br/> strcat (dspstr3, ","); <br/> strcat (dspstr3, ITOA (ABS (df3% 101), Buf, 10); <br/> strcat (dspstr3, "|"); </P> <p> cvputtext (DST, dspstr1, cvpoint (DST-> width-120, 20), & font, cvscalar (0, 0xff); <br/> cvputtext (DST, dspstr2, cvpoint (DST-> width-120, 40), & font, cvscalar (0, 0xff); <br/> cvputtext (DST, dspstr3, cvpoint (DST-> width-120, 60), & font, cvscalar (0, 0xff); <br/> // reset <br/> cvshowimage ("perspective_warp ", DST); <br/> presskey = cvwaitkey (); <br/> printf ("% x pressed/N", presskey); <br/> switch (presskey) <br/> {<br/> case '1': <br/> DF1 ++; <br/> break; <br/> case 0x230000: <br/> DF1 --; <br/> break; <br/> case '2': <br/> df2 ++; <br/> break; <br/> case 0x280000: <br/> df2 --; <br/> break; <br/> case '3': <br/> df3 ++; <br/> break; <br/> case 0x220000: <br/> df3 --; <br/> break; <br/> case '4 ': <br/> df4 ++; <br/> break; <br/> case 0x250000: <br/> df4 --; <br/> break; <br/> case '6': <br/> df5 ++; <br/> break; <br/> case 0x270000: <br/> df5 --; <br/> break; <br/> case '7': <br/> df6 ++; <br/> break; <br/> case 0x240000: <br/> df6 --; <br/> break; <br/> case '8': <br/> df7 ++; <br/> break; <br/> case 0x260000: <br/> df7 --; <br/> break; <br/> case '9': <br/> df8 ++; <br/> break; <br/> case 0x210000: <br/> df8 --; <br/> break; <br/> Case 27: <br/> cvreleaseimage (& DST); <br/> cvdestroywindow ("perspective_warp"); <br/> cvreleasemat (& warp_matrix); <br/> return 0; <br/> default: break; <br/>}< br/> return 0; <br/>}

The running effect of this program is relatively easy to analyze, that is, after the four corners of an image are changed, the image changes accordingly, each angle is a point in a two-dimensional plane with the x and y attributes. The x and y of the four points are indirectly stored in the df1 to the df8 variable.

Note the differences between the cvCloneImage function and the cvCopyImage function. memory can be allocated in advance. The latter is not responsible for memory allocation, but for value transfer. This Code does not include the image zoom-in, zoom-out, and rotation functions. However, if you want to, you can use the functions in the example to write these functions by yourself, because enlarging, downgrading, and rotating an image is only a special case of arbitrarily changing the four corners of an image.

 

Next, let's look at the self-written affine transformation code:

// Affine transformation <br/> # include <cv. h> <br/> # include <pighgui. h> </P> <p> int main (INT argc, char ** argv) <br/> {<br/> cvpoint2d32f srctri [3], dsttri [3]; <br/> cvmat * rot_mat = cvcreatemat (2, 3, cv_32fc1); <br/> cvmat * warp_mat = cvcreatemat (2, 3, cv_32fc1); <br/> iplimage * SRC, * DST, * srcbak; <br/> // int DF1 (0), df2 (33), df3 (85), df4 (25), df5 (15 ), df6 (70); <br/> int DF1 (0), df2 (0), df3 (0), df4 (0), df5 (0), df6 (0 ); <br/> double angle (-5 0.0), scale (0.6); <br/> float Delta = 0.01; <br/> int presskey; </P> <p> If (! (Src = cvloadimage (".. /ac.jpg ", 1) <br/>{< br/> return-1; <br/>}< br/> srctri [0]. X = 0; <br/> srctri [0]. y = 0; <br/> srctri [1]. X = Src-> width-1; <br/> srctri [1]. y = 0; <br/> srctri [2]. X = 0; <br/> srctri [2]. y = Src-> height-1; <br/> DST = cvcloneimage (SRC); <br/> srcbak = cvcloneimage (SRC); <br/> cvcopy (SRC, srcbak); <br/> DST-> origin = Src-> origin; <br/> cvzero (DST); <br/> cvnamedwindow ("at", 1 ); <br/> cvfont font = cvfont (1, 1); </P> <p> while (1) <br/>{< br/> dsttri [0]. X = srcbak-> width * Delta * (DF1% 101); <br/> dsttri [0]. y = srcbak-> height * Delta * (df2% 101); <br/> dsttri [1]. X = Src-> width-1 + srcbak-> width * Delta * (df3% 101); <br/> dsttri [1]. y = srcbak-> height * Delta * (df4% 101); <br/> dsttri [2]. X = srcbak-> width * Delta * (df5% 101); <br/> dsttri [2]. y = Src-> height-1 + srcbak-> height * Delta * (df6% 101); </P> <p> cvgetaffinetransform (srctri, dsttri, warp_mat ); <br/> cvwarpaffine (srcbak, DST, warp_mat); <br/> cvcopy (DST, Src ); <br/> // compute rotation matrix <br/> cvpoint2d32f center = cvpoint2d32f (SRC-> width/2, Src-> height/2 ); <br/> cv2drotationmatrix (center, angle, scale, rot_mat); <br/> // do the transformation <br/> cvwarpaffine (SRC, DST, rot_mat ); <br/> // -------------------------- character display ------------------------------------------- <br/> char Buf [8]; <br/> char dspstr1 [32] = {'/0 '}; // <br/> char dspstr2 [32] = {'/0'}; // <br/> char dspstr3 [32] = {'/0 '}; // </P> <p> memset (BUF, '/0', sizeof (BUF); <br/> strcat (dspstr1, "| "); <br/> strcat (dspstr1, ITOA (df4% 101, Buf, 10); <br/> strcat (dspstr1 ,","); <br/> strcat (dspstr1, ITOA (df5%101, Buf, 10); <br/> strcat (dspstr1 ,","); <br/> strcat (dspstr1, ITOA (df6% 101, Buf, 10); <br/> strcat (dspstr1, "| "); </P> <p> strcat (dspstr2, "| angle ="); <br/> strcat (dspstr2, ITOA (INT (angle), Buf, 10 )); <br/> strcat (dspstr2, ", scale ="); <br/> strcat (dspstr2, ITOA (INT (scale * 100), Buf, 10 )); <br/> strcat (dspstr2, "% |"); <br/> // "zookeeper | <br/> strcat (dspstr3," | "); <br/> strcat (dspstr3, ITOA (DF1% 101, Buf, 10); <br/> strcat (dspstr3 ,","); <br/> strcat (dspstr3, ITOA (df2% 101, Buf, 10); <br/> strcat (dspstr3 ,","); <br/> strcat (dspstr3, ITOA (df3% 101, Buf, 10); <br/> strcat (dspstr3, "| "); <br/> cvputtext (DST, dspstr1, cvpoint (DST-> width-120, 20), & font, cvscalar (0, 0xff )); <br/> cvputtext (DST, dspstr2, cvpoint (DST-> width-210, 40), & font, cvscalar (0, 0xff )); <br/> cvputtext (DST, dspstr3, cvpoint (DST-> width-120, 60), & font, cvscalar (0, 0xff )); <br/> // container <br/> cvshowimage ("at", DST); <br/> presskey = cvwaitkey (); <br/> // printf ("src-> width: % d/N", Src-> width); <br/> printf ("% C pressed/N ", presskey); <br/> switch (presskey) <br/>{< br/> case '1': <br/> DF1 ++; <br/> break; <br/> case 0x230000: <br/> DF1 --; <br/> break; <br/> case '2': <br/> df2 ++; <br/> break; <br/> case 0x280000: <br/> df2 --; <br/> break; <br/> case '3 ': <br/> df3 ++; <br/> break; <br/> case 0x220000: <br/> df3 --; <br/> break; <br/> case '4': // Rotation Angle <br/> Angle ++; <br/> break; <br/> case 0x250000: <br/> Angle --; <br/> break; <br/> case '6': // scale <br/> scale + = 0.01; <br/> break; <br/> case 0x270000: <br/> Scale-= 0.01; <br/> break; <br/> case '7 ': <br/> df4 ++; <br/> break; <br/> case 0x240000: <br/> df4 --; <br/> break; <br/> case '8': <br/> df5 ++; <br/> break; <br/> case 0x260000: <br/> df5 --; <br/> break; <br/> case '9': <br/> df6 ++; <br/> break; <br/> case 0x210000: <br/> df6 --; <br/> break; <br/> Case 27: <br/> cvreleaseimage (& DST ); <br/> cvdestroywindow ("at"); <br/> cvreleasemat (& rot_mat); <br/> cvreleasemat (& warp_mat); <br/> return 0; <br/> default: break; <br/>}</P> <p> cvreleaseimage (& DST ); <br/> cvreleasemat (& rot_mat); <br/> cvreleasemat (& warp_mat); <br/> return 0; <br/>}

In this program, the image zoom-in, zoom-out, and rotation functions are added. In addition, the affine transform is also a special case of perspective transformation, because the image is always parallelogram during the process of affine transformation. Therefore, to determine the target image of the affine transformation, only the coordinates of the three points are required, because the three points can determine a parallelogram, which is less than the coordinate of a point in the perspective transformation.

 

Related Article

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.