The flip() method of OpenCV's Core class can flip the image along the x/y axis. The method accepts the following parameters:
Source matrix, containing the data of the original image.
An empty target matrix used to hold the data of the result image.
A flip code that specifies the orientation of the image (0 means flipping along the x-axis, a positive number means flipping along the y-axis, and a negative number means flipping along both axes at the same time).
To flip the image, you can follow these steps:
Use the loadLibrary() method to load the OpenCV core native library.
Use the imread() method to read the contents of the image file into the matrix.
Create an empty matrix to hold the results.
Call the flip() method by passing the matrix created above.
Use the imwrite() method to create the image, passing the target matrix as a parameter.
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; public class ChangingOrientation { 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 ="D:\Images\cat.jpg"; Mat src = Imgcodecs.imread(file); //Creating an empty matrix to store the result Mat dst = new Mat(); //Changing the orientation of an image Core.flip(src, dst, -1); //Writing the image Imgcodecs.imwrite("D:\Images\flipping.jpg", dst); System.out.println("Image Processed"); } }
The above is the detailed content of How to flip an image using Java OpenCV library?. For more information, please follow other related articles on the PHP Chinese website!