📜  OpenCV-旋转

📅  最后修改于: 2020-11-23 03:49:19             🧑  作者: Mango


您可以使用imgproc类的warpAffine()方法对图像执行旋转操作。以下是此方法的语法-

Imgproc.warpAffine(src, dst, rotationMatrix, size);

此方法接受以下参数-

  • src-一个Mat对象,代表此操作的源(输入图像)。

  • dst-表示此操作的目标(输出图像)的Mat对象。

  • rotationMatrix-表示旋转矩阵的Mat对象。

  • 尺寸-类型整数代表输出图像的大小的变量。

以下程序演示了如何旋转图像。

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Size;

import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class Rotation {
   public static void main(String args[]) {
      // Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

      // Reading the Image from the file and storing it in to a Matrix object
      String file ="E:/OpenCV/chap24/transform_input.jpg";
      Mat src = Imgcodecs.imread(file);

      // Creating an empty matrix to store the result
      Mat dst = new Mat();

      // Creating a Point object
      Point point = new Point(300, 200)

      // Creating the transformation matrix M
      Mat rotationMatrix = Imgproc.getRotationMatrix2D(point, 30, 1);

      // Creating the object of the class Size
      Size size = new Size(src.cols(), src.cols());

      // Rotating the given image
      Imgproc.warpAffine(src, dst, rotationMatrix, size);

      // Writing the image
      Imgcodecs.imwrite("E:/OpenCV/chap24/rotate_output.jpg", dst);

      System.out.println("Image Processed");
   }
}

假设下面是上述程序中指定的输入图像transform_input.jpg

转换输入

输出

在执行程序时,您将获得以下输出-

Image Processed

如果打开指定的路径,则可以观察到输出图像,如下所示:

旋转输出