仿射变换

仿射变换用来对图像的旋转平移缩放。
关键函数:
CVAPI(void)  cvWarpAffine( const CvArr* src, CvArr* dst, const CvMat* map_matrix,
                           int flags CV_DEFAULT(CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS),
                           CvScalar fillval CV_DEFAULT(cvScalarAll(0)) );
计算仿射变换的变换矩阵:
CVAPI(CvMat*) cvGetAffineTransform( const CvPoint2D32f * src,
                                    const CvPoint2D32f * dst,
                                    CvMat * map_matrix );
用图像的三个顶点,变换矩阵是3*2矩阵

仿射变换的旋转变换矩阵:
CVAPI(CvMat*)  cv2DRotationMatrix( CvPoint2D32f center, double angle,
                                   double scale, CvMat* map_matrix );
center: 旋转中心
angle: 旋转角度
scale: 缩放比例
map_matrix: 输出的变换矩阵

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       


using namespace cv;


int main()
{
	IplImage *img = cvLoadImage("lena.jpg");
	IplImage *dst = cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);
	CvMat *warp_map = cvCreateMat(2,3,CV_32FC1);
	CvPoint2D32f srcTri[3],dstTri[3];

	srcTri[0].x = 0;
	srcTri[0].y = 0;
	srcTri[1].x = 0;
	srcTri[1].y = img->height - 1;
	srcTri[2].x = img->width - 1;
	srcTri[2].y = 0;
	dstTri[0].x = 0;
	dstTri[0].y = img->height * 0.33;
	dstTri[1].x = 0;
	dstTri[1].y = img->height * 0.66;
	dstTri[2].x = img->width * 0.66;
	dstTri[2].y = img->width * 0.55;

	cvGetAffineTransform(srcTri,dstTri,warp_map);
	cvWarpAffine(img,dst,warp_map);
	//cvGetQuadrangleSubPix(img,dst,warp_map);
	cvNamedWindow("lena_warp");
	cvNamedWindow("lena_rot");
	cvShowImage("lena_warp",dst);
	CvPoint2D32f center = cvPoint2D32f(img->width/2,img->height/2);
	cv2DRotationMatrix(center,40,0.5,warp_map);

	cvWarpAffine(img,dst,warp_map);
	cvShowImage("lena_rot",dst);
	cvWaitKey(0);
	cvReleaseImage(&img);
	cvReleaseImage(&dst);
	cvDestroyWindow("lena");

}

      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值