#include <stdlib.h>
#include <string.h>
// OpenCV
#include <opencv/cxcore.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv/cvaux.h>
using namespace std;
using namespace cv;
int main()
{
IplImage* grey = cvLoadImage("fix.png");
IplImage* Img_old = cvCloneImage(grey);
CvPoint2D32f src[4];
CvPoint2D32f dst[4];
//原影像的四個頂點
src[0].x = 632;
src[0].y = 434;
src[1].x = 740;
src[1].y = 434;
src[2].x = 273;
src[2].y = 642;
src[3].x = 1048;
src[3].y = 642;
//目標影像的四個頂點
dst[0].x = 1;
dst[0].y = 1;
dst[1].x = 1280;
dst[1].y = 1;
dst[2].x = 1;
dst[2].y = 720;
dst[3].x = 1280;
dst[3].y = 720;
float newm[9];
CvMat newM = cvMat( 3, 3, CV_32F, newm );
//獲得透視轉換矩陣
cvWarpPerspectiveQMatrix(src,dst,&newM);
//透視轉換
cvWarpPerspective(Img_old,grey,&newM,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
cvShowImage("原影像",Img_old);
cvShowImage("目標影像",grey);
//逆轉換
cvWarpPerspectiveQMatrix(dst,src,&newM);
cvWarpPerspective(grey,Img_old,&newM,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
cvShowImage("逆轉換後",Img_old);
cvWaitKey(0);
return 0;
}
原始影像
透視轉換
再轉換回來


