c - how to undo cvWarpPerspective -
i found program change perspective,
my question how can source image, , coordinates of red point draw in second image ? here's code :
#include <opencv\cv.h> #include <opencv\highgui.h> int main(int argc, char** argv) { cvpoint2d32f srcquad[4], dstquad[4]; cvmat* warp_matrix = cvcreatemat(3,3,cv_32fc1); iplimage *src, *dst; src=cvloadimage("b.jpg",1) ; dst = cvcloneimage(src); dst->origin = src->origin; cvzero(dst); srcquad[0].x = 0; //src top left srcquad[0].y = 0; srcquad[1].x = src->width - 1; //src top right srcquad[1].y = 0; srcquad[2].x = 0; //src bottom left srcquad[2].y = src->height - 1; srcquad[3].x = src->width -1; //src bot right srcquad[3].y = src->height - 1; dstquad[0].x = src->width*0.05; //dst top left dstquad[0].y = src->height*0.33; dstquad[1].x = src->width*0.9; //dst top right dstquad[1].y = src->height*0.25; dstquad[2].x = src->width*0.2; //dst bottom left dstquad[2].y = src->height*0.7; dstquad[3].x = src->width*0.8; //dst bot right dstquad[3].y = src->height*0.9; cvgetperspectivetransform( srcquad, dstquad, warp_matrix ); cvwarpperspective( src, dst, warp_matrix ); cvnamedwindow( "perspective_warp", 1 ); cvshowimage( "perspective_warp", dst ); cvsaveimage("bbb.jpg",dst); cvwaitkey(); cvreleaseimage(&dst); cvreleasemat(&warp_matrix); return 0; }
update
after suggestion of @mrgloom can go first perspective.
cvinvert(warp_matrix,inversematrix); cvwarpperspective( dst, src2, inversematrix ); cvnamedwindow( "2", 2 ); cvshowimage( "2", src2 );
thanks help!
formula perspective transform, m_{i,j} homography matrix elements
u= (m11*x + m12*y + m13)/(m31*x + m32*y + m33); v= (m21*x + m22*y + m23)/(m31*x + m32*y + m33);
so backward transformation
x = (-v * m33 * m12 + v * m32 * m13 + m23 * m12 - m22 * m13 + m22 * u * m33 - m23 * u * m32) / (m11 * m22 - m11 * v * m32 - m12 * m21 + m12 * v * m31 - u * m31 * m22 + u * m32 * m21); t2 = m11 * v; t7 = u * m31; y = -(m11 * m23 - t2 * m33 - m13 * m21 + m13 * v * m31 - t7 * m23 + u * m33 * m21) / (m11 * m22 - t2 * m32 - m12 * m21 + m12 * v * m31 - t7 * m22 + u * m32 * m21);
so can calculate point correspondence (x,y)->(u,v) , (u,v)->(x,y) using homography matrix.
i'm not sure think backward transform can obtained inverting homography matrix (warp_matrix in case)
Comments
Post a Comment