Home > Java > javaTutorial > How to flip an image using Java OpenCV library?

How to flip an image using Java OpenCV library?

WBOY
Release: 2023-09-09 13:05:05
forward
1383 people have browsed it

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.

Example

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");
   }
}
Copy after login

Enter

如何使用Java OpenCV库翻转图像?

Output

如何使用Java OpenCV库翻转图像?

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template