對於透視變換,必須為map_matrix分配一個3x3數組,除了3x3矩陣和三個控點變為四個控點外,透視變化在其他方面與仿射變換完全類似。具體可以參考:點擊打開鏈接
主要用到兩個函數WarpPerspective和GetPerspectiveTransform。
1)WarpPerspective
對圖像進行透視變換
void cvWarpPerspective( const CvArr* src, CvArr* dst,const CvMat* map_matrix,
int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,
CvScalar fillval=cvScalarAll(0) );
src
輸入圖像.
dst
輸出圖像.
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 。
2)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.