/**
* 旋转图片
* @param src 原图
* @param dst 旋转后图片
* @param degree 角度
*/
public static void roateImageMat(Mat src, Mat dst, Double degree) {
Point center = new Point();
center.x = src.cols() / 2.0;
center.y = src.rows() / 2.0;
//旋转90°
if (degree == 90) {
Core.transpose(src, dst);
Core.flip(dst, dst, 1);
} else if (degree == -90 || degree == 270) {
Core.transpose(src, dst);
Core.flip(dst, dst, 0);
} else {
// 得到旋转矩阵算子
Mat matrix = Imgproc.getRotationMatrix2D(center, -degree, 1);
// 旋转
Imgproc.warpAffine(src, dst, matrix, dst.size(), 1, 0, new Scalar(255, 255, 255));
}
}
对图像应用仿射变换。函数 warpAffine 使用指定的矩阵变换源图像:
当设置了 WARP_INVERSE_MAP 标志时。 否则,先用 invertAffineTransform 反转变换,然后代入上面的公式,而不是 M。该函数不能就地操作。
1.函数 cv::warpAffine (InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar &borderValue=Scalar())
2、参数详解
src 输入图像。
dst 输出大小为 dsize 且类型与 src 相同的图像。
M 2×3 变换矩阵。
dsize 输出图像的大小。
flags 插值方法(见 InterpolationFlags)和可选标志 WARP_INVERSE_MAP 的组合,这意味着 M 是逆变换(dst→src)。
borderMode 像素外推法(参见 BorderTypes); 当borderMode=BORDER_TRANSPARENT时,表示目标图像中与源图像中的“异常值”对应的像素未被函数修改。
borderValue 在恒定边界的情况下使用的值; 默认情况下,它是 0。