图像旋转图形旋转是以图像的中心为原点,旋转一定的角度,也就是将图像上的所有像素都旋转一个相同的角度。旋转后图像的的大小一般会改变,即可以把转出显示区域的图像截去,或者扩大图像范围来显示所有的图像。图像的旋转变换也可以用矩阵变换来表示。

图形旋转是以图像的中心为原点,旋转一定的角度,也就是将图像上的所有像素都旋转一个相同的角度。旋转后图像的的大小一般会改变,即可以把转出显示区域的图像截去,或者扩大图像范围来显示所有的图像。图像的旋转变换也可以用矩阵变换来表示。
函数说明:Imgproc.getRotationMatrix2D(Point center, double angle, double scale)
参数详解: Point center:表示旋转的中心点; double angle:表示旋转的角度; double scale:图像缩放因子;
代码案例
package com.what21.opencv01.demo05;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 OpenCVRotate {static {System.loadLibrary(Core.NATIVE_LIBRARY_NAME);}public static void main(String[] args) {Mat src = Imgcodecs.imread("D:/1.jpg");Mat dst = src.clone();//复制矩阵进入dstPoint 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:/1$1.jpg", dst);affineTrans = Imgproc.getRotationMatrix2D(center, 110.0, 1.1);Imgproc.warpAffine(src, dst, affineTrans, dst.size(), Imgproc.INTER_NEAREST);Imgcodecs.imwrite("D:/1$2.jpg", dst);}}
1.jpg
1$1.jpg
1$2.jpg
透视变换(Perspective Transformation)透视变换(Perspective Transformation)是指利用透视中心、像点、目标点三点共线的条件,按透视旋转定律使承影面(透视面)绕迹线(透视轴)旋转某一角度,破坏原有的投影光线束,仍能保持承影面上投影几何图形不变的变换。
代码案例
package com.what21.opencv01.demo05;import org.opencv.core.Core;import org.opencv.core.CvType;import org.opencv.core.Mat;import org.opencv.core.Point;import org.opencv.imgcodecs.Imgcodecs;import org.opencv.imgproc.Imgproc;import org.opencv.utils.Converters;import java.util.List;/** * 透视变换 */public class OpenCVPerspective {static {System.loadLibrary(Core.NATIVE_LIBRARY_NAME);}public static void main(String[] args) {Mat src = Imgcodecs.imread("D:/1.jpg");//读取图像到矩阵中,取灰度图像if (src.empty()) {return;}try {int xMargin, yMargin;int x0 = src.cols() / 4;int x1 = (src.cols() / 4) * 3;int y0 = src.cols() / 4;int y1 = (src.cols() / 4) * 3;Mat dst = new Mat();List<Point> listSrcs = java.util.Arrays.asList(new Point(x0, y0), new Point(x0, y1), new Point(x1, y1), new Point(x1, y0));Mat srcPoints = Converters.vector_Point_to_Mat(listSrcs, CvType.CV_32F);xMargin = src.cols() / 10;yMargin = src.rows() / 10;List<Point> listDsts = java.util.Arrays.asList(new Point(x0xMargin, y0yMargin), listSrcs.get(1), listSrcs.get(2), new Point(x1 - xMargin, y0yMargin));Mat dstPoints = Converters.vector_Point_to_Mat(listDsts, CvType.CV_32F);Mat perspectiveMmat = Imgproc.getPerspectiveTransform(srcPoints, dstPoints);Imgproc.warpPerspective(src, dst, perspectiveMmat, src.size(), Imgproc.INTER_LINEAR);Imgcodecs.imwrite("D:/1.dst1.jpg", dst);xMargin = src.cols() / 8;yMargin = src.cols() / 8;listDsts.set(0, listSrcs.get(0));listDsts.set(1, listSrcs.get(1));listDsts.set(2, new Point(x1 - xMargin, y1 - yMargin));listDsts.set(3, new Point(x1 - xMargin, y0 - yMargin));dstPoints = Converters.vector_Point_to_Mat(listDsts, CvType.CV_32F);perspectiveMmat = Imgproc.getPerspectiveTransform(srcPoints, dstPoints);Imgproc.warpPerspective(src, dst, perspectiveMmat, src.size(), Imgproc.INTER_LINEAR);Imgcodecs.imwrite("D:/1.dst2.jpg", dst);xMargin = src.cols() / 6;yMargin = src.cols() / 6;listDsts.set(0, new Point(x0xMargin, y0yMargin));listDsts.set(1, listSrcs.get(1));listDsts.set(2, new Point(x1 - xMargin, y1 - yMargin));listDsts.set(3, listSrcs.get(3));dstPoints = Converters.vector_Point_to_Mat(listDsts, CvType.CV_32F);perspectiveMmat = Imgproc.getPerspectiveTransform(srcPoints, dstPoints);Imgproc.warpPerspective(src, dst, perspectiveMmat, src.size(), Imgproc.INTER_LINEAR);Imgcodecs.imwrite("D:/1.dst3.jpg", dst);} catch (Exception e) {e.printStackTrace();}}}
1.dst1.jpg
1.dst2.jpg
1.dst3.jpg
