opencv之透视变换cvWarpPerspective

透视变换(单应性?)能提供更大的灵活性,但是一个透视投影并不是线性变换,因此所采用的映射矩阵是3*3,且控点变为4个,其他方面与仿射变换完全类似,下面的例程是针对密集变换,稀疏图像变换则采用cvPerspectiveTransform函数来处理。

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

WarpPerspective

对图像进行透视变换

void cvWarpPerspective( const CvArr* src, CvArr* dst,constCvMat* 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 。

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

GetPerspectiveTransform

由四对点计算透射变换

CvMat* cvGetPerspectiveTransform( const CvPoint2D32f*src, constCvPoint2D32f* 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.

  1.  #include   
  2. #include   
  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.         cvZero( dst );  //清空数组  
  17.   
  18.         //计算矩阵仿射变换  
  19.         srcTri[0].x = 0;  
  20.         srcTri[0].y = 0;  
  21.         srcTri[1].x = src -> width - 1;  //缩小一个像素  
  22.         srcTri[1].y = 0;  
  23.         srcTri[2].x = 0;  
  24.         srcTri[2].y = src -> height - 1;  
  25.         srcTri[3].x = src -> width - 1;  //bot right  
  26.         srcTri[3].y = src -> height - 1;  
  27.   
  28.         //改变目标图像大小  
  29.         dstTri[0].x = src -> width * 0.05;  
  30.         dstTri[0].y = src -> height * 0.33;  
  31.         dstTri[1].x = src -> width * 0.9;  
  32.         dstTri[1].y = src -> height * 0.25;  
  33.         dstTri[2].x = src -> width * 0.2;  
  34.         dstTri[2].y = src -> height * 0.7;  
  35.         dstTri[3].x = src -> width * 0.8;  
  36.         dstTri[3].y = src -> height * 0.9;  
  37.   
  38.         cvGetPerspectiveTransform( srcTri, dstTri, warp_mat );  //由三对点计算仿射变换   
  39.         cvWarpPerspective( src, dst, warp_mat );  //对图像做仿射变换  
  40.   
  41.         //输出  
  42.         cvNamedWindow( "Perspective Warp", 1 );  
  43.         cvShowImage( "Perspective Warp", dst );  //最终是输出dst   
  44.         cvWaitKey();  
  45.     }  
  46.     cvReleaseImage( &dst );  
  47.     cvReleaseMat( &warp_mat );  
  48.   
  49.     return 0;  
  50. }  

透视变换(单应性?)能提供更大的灵活性,但是一个透视投影并不是线性变换,因此所采用的映射矩阵是3*3,且控点变为4个,其他方面与仿射变换完全类似,下面的例程是针对密集变换,稀疏图像变换则采用cvPerspectiveTransform函数来处理。

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

WarpPerspective

对图像进行透视变换

void cvWarpPerspective( const CvArr* src, CvArr* dst,constCvMat* 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 。

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

GetPerspectiveTransform

由四对点计算透射变换

CvMat* cvGetPerspectiveTransform( const CvPoint2D32f*src, constCvPoint2D32f* 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.

  1.  #include   
  2. #include   
  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.         cvZero( dst );  //清空数组  
  17.   
  18.         //计算矩阵仿射变换  
  19.         srcTri[0].x = 0;  
  20.         srcTri[0].y = 0;  
  21.         srcTri[1].x = src -> width - 1;  //缩小一个像素  
  22.         srcTri[1].y = 0;  
  23.         srcTri[2].x = 0;  
  24.         srcTri[2].y = src -> height - 1;  
  25.         srcTri[3].x = src -> width - 1;  //bot right  
  26.         srcTri[3].y = src -> height - 1;  
  27.   
  28.         //改变目标图像大小  
  29.         dstTri[0].x = src -> width * 0.05;  
  30.         dstTri[0].y = src -> height * 0.33;  
  31.         dstTri[1].x = src -> width * 0.9;  
  32.         dstTri[1].y = src -> height * 0.25;  
  33.         dstTri[2].x = src -> width * 0.2;  
  34.         dstTri[2].y = src -> height * 0.7;  
  35.         dstTri[3].x = src -> width * 0.8;  
  36.         dstTri[3].y = src -> height * 0.9;  
  37.   
  38.         cvGetPerspectiveTransform( srcTri, dstTri, warp_mat );  //由三对点计算仿射变换   
  39.         cvWarpPerspective( src, dst, warp_mat );  //对图像做仿射变换  
  40.   
  41.         //输出  
  42.         cvNamedWindow( "Perspective Warp", 1 );  
  43.         cvShowImage( "Perspective Warp", dst );  //最终是输出dst   
  44.         cvWaitKey();  
  45.     }  
  46.     cvReleaseImage( &dst );  
  47.     cvReleaseMat( &warp_mat );  
  48.   
  49.     return 0;  
  50. }  

猜你喜欢

转载自blog.csdn.net/hanxiaoyong_/article/details/85879694