仿射变换用来对图像的旋转平移缩放。
关键函数:
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)) );
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 );
const CvPoint2D32f * dst,
CvMat * map_matrix );
用图像的三个顶点,变换矩阵是3*2矩阵
仿射变换的旋转变换矩阵:
CVAPI(CvMat*) cv2DRotationMatrix( CvPoint2D32f center, double angle,
double scale, CvMat* map_matrix );
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");
}