第六章 - 圖像變換 - 圖像拉伸、收縮、扭曲、旋轉[2] - 透視變換(cvWarpPerspective)

透視變換(單應性?)能提供更大的靈活性,可是一個透視投影並非線性變換,所以所採用的映射矩陣是3*3,且控點變爲4個,其餘方面與仿射變換徹底相似,下面的例程是針對密集變換,稀疏圖像變換則採用cvPerspectiveTransform函數來處理。數組

------------------------------------------------------------------------------------------------函數

 

WarpPerspectiveui

對圖像進行透視變換spa

void cvWarpPerspective( const CvArr* src, CvArr* dst,const CvMat* map_matrix,.net

                       int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,指針

                       CvScalar fillval=cvScalarAll(0) );code

srcorm

輸入圖像.blog

dstip

輸出圖像.

map_matrix

3×3 變換矩陣

flags

插值方法和如下開關選項的組合:

·       CV_WARP_FILL_OUTLIERS- 填充全部縮小圖像的象素。若是部分象素落在輸入圖像的邊界外,那麼它們的值設定爲 fillval.

·       CV_WARP_INVERSE_MAP- 指定 matrix 是輸出圖像到輸入圖像的反變換,所以能夠直接用來作象素插值。不然, 函數從 map_matrix 獲得反變換。

fillval

用來填充邊界外面的值

函數 cvWarpPerspective 利用下面指定矩陣變換輸入圖像:

  • 若是沒有指定 CV_WARP_INVERSE_MAP , 
  • 不然, 

要變換稀疏矩陣,使用 cxcore 中的函數 cvTransform 。

-----------------------------

 

GetPerspectiveTransform

由四對點計算透射變換

CvMat* cvGetPerspectiveTransform( const CvPoint2D32f*src, const CvPoint2D32f* dst,

                                  CvMat*map_matrix );

 

#define cvWarpPerspectiveQMatrixcvGetPerspectiveTransform

src

輸入圖像的四邊形頂點座標。

dst

輸出圖像的相應的四邊形頂點座標。

map_matrix

指向3×3輸出矩陣的指針。

函數cvGetPerspectiveTransform計算知足如下關係的透射變換矩陣:

這裏,dst(i)= (x'i,y'i),src(i)= (xi,yi),i = 0..3.

------------------------------------------------------------------------------------------------

/*code*/

 

  1. #include <highgui.h>   
  2. #include <cv.h>   
  3.   
  4. int main(int argc, char** argv)  
  5. {  
  6.     CvPoint2D32f srcTri[4], dstTri[4]; //二維座標下的點,類型爲浮點   
  7.     //CvMat* rot_mat = cvCreateMat( 2, 3, CV_32FC1 );  //多通道矩陣   
  8.     CvMat* warp_mat = cvCreateMat( 3, 3, CV_32FC1 );  
  9.     IplImage *src, *dst;  
  10.   
  11.     if( argc == 2 && ( ( src = cvLoadImage( argv[1], 1 ) ) != 0 ) )  
  12.     {  
  13.         dst = cvCloneImage( src );  //製做圖像的完整拷貝   
  14.         dst ->origin = src ->origin;    
  15.         /* 
  16.         int origin; /* 0 - 頂—左結構, 
  17.         1 - 底—左結構 (Windows bitmaps 風格)  
  18.         */  
  19.         cvZero( dst );  //清空數組   
  20.   
  21.         //計算矩陣仿射變換   
  22.         srcTri[0].x = 0;  
  23.         srcTri[0].y = 0;  
  24.         srcTri[1].x = src -> width - 1;  //縮小一個像素   
  25.         srcTri[1].y = 0;  
  26.         srcTri[2].x = 0;  
  27.         srcTri[2].y = src -> height - 1;  
  28.         srcTri[3].x = src -> width - 1;  //bot right   
  29.         srcTri[3].y = src -> height - 1;  
  30.   
  31.         //改變目標圖像大小   
  32.         dstTri[0].x = src -> width * 0.05;  
  33.         dstTri[0].y = src -> height * 0.33;  
  34.         dstTri[1].x = src -> width * 0.9;  
  35.         dstTri[1].y = src -> height * 0.25;  
  36.         dstTri[2].x = src -> width * 0.2;  
  37.         dstTri[2].y = src -> height * 0.7;  
  38.         dstTri[3].x = src -> width * 0.8;  
  39.         dstTri[3].y = src -> height * 0.9;  
  40.   
  41.         cvGetPerspectiveTransform( srcTri, dstTri, warp_mat );  //由三對點計算仿射變換    
  42.         cvWarpPerspective( src, dst, warp_mat );  //對圖像作仿射變換   
  43.   
  44.         //輸出   
  45.         cvNamedWindow( "Perspective Warp", 1 );  
  46.         cvShowImage( "Perspective Warp", dst );  //最終是輸出dst    
  47.         cvWaitKey();  
  48.     }  
  49.     cvReleaseImage( &dst );  
  50.     cvReleaseMat( &warp_mat );  
  51.   
  52.     return 0;  
  53. }  
相關文章
相關標籤/搜索