Two functions can be implemented using opencv:
Cvgetperspectivetransform
Cvwarpperspective
Not much to mention.Code:
C/C ++ code cvpoint2d32f srcpoints [4]; cvpoint2d32f dstpoints [4]; srcpoints [0]. X = o0x; // srcpoints [0] for the four points in the original image. y = o0y; srcpoints [1]. X = o1x; srcpoints [1]. y = o1y; srcpoints [2]. X = o2x; srcpoints [2]. y = o2y; srcpoints [3]. X = o3x; srcpoints [3]. y = o3y; dstpoints [0]. X = xofst0; // The four positions corresponding to the transformed dstpoints [0]. y = yofst0; dstpoints [1]. X = xofst1; dstpoints [1]. y = yofst1; dstpoints [3]. X = xofst2; dstpoints [3]. y = yofst2; dstpoints [2]. X = xofst3; dstpoints [2]. y = yofst3; percentile (srcpoints, dstpoints, & mat); // obtain the perspective transform matrix cvwarpperspective (SRC, DST, & mat, cv_inter_linear + cv_warp_fill_outliers, cvscalarall (0 )); // perform perspective transformation
Reprinted on the website
StaticIntComputepixel (FloatX,FloatY,OutFloatX1,OutFloatY1)
{
DoubleR, nn;
If(X=0&&Y=0)
{
X1=X;
Y1=Y;
Return1;
}
Nn=Math. SQRT (x*X+Y*Y );
R=(Math. Abs (X)>Math. Abs (y ))?Math. Abs (NN/X): Math. Abs (NN/Y );
X1=(Float) (R*X );
Y1=(Float) (R*Y );
Return1;
}
StaticColor getpixelcolorinterpolated (RefBitmap image,FloatX,FloatY)
{
IntXI=(Int) (X );
If(X<0) XI--;
IntYi=(Int) (Y );
If(Y<0) Yi--;
If(Xi<-1|XI> =Image. Width|Yi<-1|Yi> =Image. Height)
{
ReturnGetpixelcolorwithoverflow (RefImage,-999,-999);
}
// Get four neighbouring pixels
If (XI + 1 ) < Image. Width && XI > = 0 && (Yi + 1 ) < Image. Height && Yi > = 0 )
{
Ushort WT1 = ( Ushort ) (X - Xi) * 256.0f ), WT2 = ( Ushort ) (Y - Yi) * 256.0f );
Ushort WD = ( Ushort ) (WT1 * WT2 > 8 );
Ushort WB = ( Ushort ) (WT1 - WD );
Ushort WC = ( Ushort ) (WT2 - WD );
Ushort Wa = ( Ushort )( 256 - WT1 - WC );
Ushort WRR, WGG, WBB;
Color CLR=Image. getpixel (XI, Yi );
WBB=(Ushort) (Wa*CLR. B); WGG=(Ushort) (Wa*CLR. G); WRR=(Ushort) (Wa*CLR. R );
CLR=Image. getpixel (XI+1, Yi );
WBB+ =(Ushort) (WB*CLR. B); WGG+ =(Ushort) (WB*CLR. G); WRR+ =(Ushort) (WB*CLR. R );
CLR=Image. getpixel (XI, Yi+1);
WBB+ =(Ushort) (WC*CLR. B); WGG+ =(Ushort) (WC*CLR. G); WRR+ =(Ushort) (WC*CLR. R );
CLR = image. getpixel (xi + 1 , yi + 1 );
WBB + = ( ushort ) (WD * CLR. b); WGG += ( ushort ) (WD * CLR. g); WRR += ( ushort ) (WD * CLR. r);
return color. fromargb ( 255 , WRR > 8 , WGG > 8 , WBB > 8 );
}< br> else
{< br> float t1 = x - XI, t2 = Y - Yi;
float d = t1 * T2;
float B = t1 - D;
float C = t2 - D;
float A = 1 - t1 - C;
color rgb11, rgb21, rgb12, rgb22;
rgb11 = getpixelcolorwithoverflow ( ref image, XI, Yi );
rgb21 = getpixelcolorwithoverflow ( ref image, xi + 1 , Yi );
rgb12 = getpixelcolorwithoverflow ( ref image, XI, yi + 1 );
rgb22 = getpixelcolorwithoverflow ( ref image, xi + 1 , yi + 1 );
// calculate linear interpolation
return color. fromargb ( 255 ,
( byte ) (A * rgb11.r + B * rgb21.r + C * rgb12.r + d * rgb22.r ),
( byte ) (A * rgb11.g + B * rgb21.g + C * rgb12.g + d * rgb22.g ),
( byte ) (A * rgb11. B + B * rgb21. B + C * rgb12. B + d * rgb22. B ));
}< BR >}
StaticColor getpixelcolorwithoverflow (RefBitmap image,LongX,LongY)
{
If(!Isinside (RefImage, x, y ))
{
ReturnColor. fromargb (255,255,255,255);
}
ReturnImage. getpixel ((Int) X ,(Int) Y );
}
StaticBoolIsinside (RefBitmap image,LongX,LongY)
{
Return(0<=Y&&Y<Image. Height&&0<=X&&X<Image. width );
}
PrivateVoidTransformellipsetoolstripmenuitem_click (ObjectSender, eventargs E)
{
Bitmap image=NewBitmap ("D: \ PSD \ info1.jpg");
IntX, Y;
FloatX1, Y1;
FloatFX, fy, xmid, ymid, Ar;
Bitmap image2=NewBitmap (image );
Xmid = ( Float ) (Image. Width / 2.0 );
Ymid = ( Float ) (Image. Height / 2.0 );
Ar = ( Float ) (Image. Height) / ( Float ) (Image. width );
For (Y = 0 ; Y < Image. height; y ++ )
{
For (X = 0 ; X < Image. width; x ++ )
{
Computepixel (Ar * (X - Xmid), y - Ymid, Out FX, Out FY );
X1 = Xmid + FX / Ar;
Y1 = Ymid + FY;
Image2.setpixel (X, Y, getpixelcolorinterpolated (RefImage, X1, Y1 ));
}
}
This. Picturebox1.image=Image2;
}
ThisArticleGood
Http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html#warp-affine
Http://www.opencv.org.cn/forum/viewtopic.php? F = 1 & t = 8940 & P = 34421 & hilit = % E9 % 80% 8f + % E8 % A7 % 86 + % E5 % 8f % 98 + % E6 % 8d % A2 # p34421