核心函数:
cvWarpAffine
cvGetAffineTransform
cv2DRotationMatrix
步骤:
先用GetAffineTransform(形变,拉伸,收缩,源目标图像三个点对应确定)或者cv2DRotationMatrix(旋转)求出变换矩阵,然后用cvWarpAffine进行变换
程序:
代码:
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <iostream>
int WarpAffine(int argc,char** argv) //warp:弯曲的 affine:仿射的
{
IplImage* src=cvLoadImage("e:\\picture\\4.jpg");
IplImage* dst1=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
IplImage* dst2=cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
cvZero(dst1);
cvZero(dst2);
//设置源图像的三点,以及目标图像对应的三点所在的坐标
CvPoint2D32f SrcPt[3];
CvPoint2D32f DstPt[3];
SrcPt[0].x=0; //src top left
SrcPt[0].y=0;
SrcPt[1].x=src->width-1; //src top right
SrcPt[1].y=0;
SrcPt[2].x=0; //src buttom left
SrcPt[2].y=src->height-1;
DstPt[0].x=src->width*0.1; //dst top left
DstPt[0].y=src->height*0.1;
DstPt[1].x=src->width*0.8; //dst top right
DstPt[1].y=src->height*0.8;
DstPt[2].x=src->width*0.2; //dst buttom left
DstPt[2].y=src->height*0.8;
//形变,拉伸,收缩
CvMat *WarpMat=cvCreateMat(2,3,CV_32FC1);
cvGetAffineTransform(SrcPt,DstPt,WarpMat); //由三对点计算仿射变换,即求出变换矩阵
cvWarpAffine(src,dst1,WarpMat,9,cvScalar(0,0,255)); //对图像做仿射变换
//旋转
CvMat *RotationMat=cvCreateMat(2,3,CV_32FC1);
CvPoint2D32f center=cvPoint2D32f(dst1->width/2,dst1->height/2);
double angle=90;
double scale=0.6;
cv2DRotationMatrix(center,angle,scale,RotationMat); //用cv2DRotationMatrix求出旋转的变换矩阵,第一个参数为变换中心,第二个参数为旋转角度,正的为逆时针,第三个参数无缩放大小
cvWarpAffine(dst1,dst2,RotationMat,9,cvScalar(255,0,0));
cvNamedWindow("src");
cvNamedWindow("dst1");
cvNamedWindow("dst2");
cvShowImage("src",src);
cvShowImage("dst1",dst1);
cvShowImage("dst2",dst2);
cvWaitKey(0);
cvDestroyWindow("src");
cvDestroyWindow("dst1");
cvDestroyWindow("dst2");
cvReleaseImage(&src);
cvReleaseImage(&dst1);
cvReleaseImage(&dst2);
return 0;
}
本文出自 “flyclc” 博客,请务必保留此出处http://flyclc.blog.51cto.com/1385758/1539822
稠密仿射变换 cvWarpAffine,布布扣,bubuko.com
原文地址:http://flyclc.blog.51cto.com/1385758/1539822