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);
}

结果:

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值