2012年5月26日 星期六

[OpenCV] 透視轉換

#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;
}



原始影像

透視轉換

再轉換回來

1 則留言:

  1. 哈囉,你好,最近我剛好也正在學習透視轉換與逆透視轉換,我有2個問題想要請教你。
    1. 不管是透視轉換或是逆透視轉換在圖片轉換之後都會有圖片失真的現象,在物體高度的部份會有拉長的情形,這是有辦法可以改善的嗎?
    2. 在使用透視轉換或是逆透視轉換之後,有什麼辦法可以得知你與物體的距離跟物體的高度呢?

    回覆刪除