源码如下:
int picProcessBasics::IMGAffineTransform(Mat* srcImage, Mat* dstImage_warp,double angle,double scale)
{
if(NULL == srcImage)
return -1;
int res = 0;
//定义两组点,代表两个三角形
Point2f srcTriangle[3];
Point2f dstTriangle[3];
//定义一些Mat变量
Mat rotMat( 2, 3, CV_32FC1 );
Mat warpMat( 2, 3, CV_32FC1 );
Mat dstImage_warp_rotate;
// 设置目标图像的大小和类型与源图像一致
*dstImage_warp = Mat::zeros( srcImage->rows, srcImage->cols, srcImage->type() );
//设置源图像和目标图像上的三组点以计算仿射变换
srcTriangle[0] = Point2f( 0,0 );
srcTriangle[1] = Point2f( static_cast
srcTriangle[2] = Point2f( 0, static_cast
dstTriangle[0] = Point2f( static_cast
dstTriangle[1] = Point2f( static_cast
dstTriangle[2] = Point2f( static_cast
//求得仿射变换
warpMat = getAffineTransform( srcTriangle, dstTriangle );
//对源图像应用刚刚求得的仿射变换
warpAffine( *srcImage, *dstImage_warp, warpMat, dstImage_warp->size() );
//对图像进行缩放后再旋转
// 计算绕图像中点顺时针旋转angle度缩放因子为scale的旋转矩阵
Point center = Point( dstImage_warp->cols/2, dstImage_warp->rows/2 );
// 通过上面的旋转细节信息求得旋转矩阵
rotMat = getRotationMatrix2D( center, angle, scale );
// 旋转已缩放后的图像
warpAffine( *dstImage_warp, dstImage_warp_rotate, rotMat, dstImage_warp->size() );
dstImage_warp_rotate.copyTo(*dstImage_warp);
return res;
}
变换前图像:
仿射变换后旋转90度缩放比例为1: