OpenCV3 Java图像的旋转(Imgproc.getRotationMatrix2D)
一般图像的旋转是以图像的中心为原点,旋转一定的角度,也就是将图像上的所有像素都旋转一个相同的角度。旋转后图像的的大小一般会改变,即可以把转出显示区域的图像截去,或者扩大图像范围来显示所有的图像。图像的旋转变换也可以用矩阵变换来表示。
函数说明:
Imgproc.getRotationMatrix2D(Point center, double angle, double scale)
参数详解:
Point center:表示旋转的中心点;
double angle:表示旋转的角度;
double scale:图像缩放因子;
代码案例:
package com.what21.opencv.demo; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Point; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class Rotate { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat src=Imgcodecs.imread("D:/ShareData/internet.jpg"); //读取图像到矩阵中,取灰度图像 if(src.empty()){ return ; } try{ Mat dst=src.clone(); //复制矩阵进入dst Point center =new Point(src.width()/2.0,src.height()/2.0); Mat affineTrans=Imgproc.getRotationMatrix2D(center, 33.0, 1.0); Imgproc.warpAffine(src, dst, affineTrans, dst.size(),Imgproc.INTER_NEAREST); Imgcodecs.imwrite("D:/ShareData/internet.033.jpg",dst); affineTrans=Imgproc.getRotationMatrix2D(center, 110.0, 1.1); Imgproc.warpAffine(src,dst,affineTrans,dst.size(),Imgproc.INTER_NEAREST); Imgcodecs.imwrite("D:/ShareData/internet.110.jpg",dst); }catch(Exception e){ e.printStackTrace(); } } }
评论