Opencv-rotation
提供:Dev Guides
OpenCV-回転
*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
指定したパスを開くと、次のように出力画像を観察できます-