Bootstrap

opencv c++图像旋转(21)

1、API

仿射变换API:

src输入图像
dst输出图像
M2×3 transformation matrix,旋转矩阵,用getRotationMatrix2D来获取
dsize输出图像尺寸定义
flags插值方式combination of interpolation methods (see InterpolationFlags) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation ( dst→src ).
borderMode边缘处理方式pixel extrapolation method (see BorderTypes); when borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image corresponding to the "outliers" in the source image are not modified by the function.
borderValuevalue used in case of a constant border; by default, it is 0.

旋转矩阵API

center图像旋转中心,注意定义时用Point2f类型
angle旋转角度
scale决定是否对图像本身放缩,不放缩传入1.0即可。Isotropic scale factor.

 

 代码示例:

void QuickDemo::rotate_Demo(Mat& image)
{
	Mat dst, M;
	int w = image.cols;
	int h = image.rows;
	//获取旋转矩阵
	M = getRotationMatrix2D(Point2f(w / 2, h / 2), 45, 1.0);
	//获取旋转后图像的尺寸
	double cos = abs(M.at<double>(0, 0));
	double sin = abs(M.at<double>(0, 1));
	int nw = cos * w + sin * h;
	int nh = sin * w + cos * h;
	//分别获取x,y方向的偏移量
	M.at<double>(0, 2) += (nw / 2 - w / 2);
	M.at<double>(1, 2) += (nh / 2 - h / 2);
	//旋转
	warpAffine(image, dst, M, Size(nh, nw), INTER_LINEAR, 0, Scalar(255, 0, 0));
	imshow("旋转图像", dst);
}

结果:

 

;