Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the function of loading and saving image based on OpenCv and JVM

Shulou Source: shulou.com Published: 2022-06-02 03:05:14 10月04日 Update

Today, I will show you how to load and save images based on OpenCv and JVM. The editor thinks that the content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

Load Picture

OpenCv has a simple function named imread to read images from a file

The imread function is located in the package of the same name in the Imgcodecs class.

Load picture code

Import org.opencv.core.CvType;import org.opencv.core.Mat;import org.opencv.core.Core;import org.opencv.imgcodecs.Imgcodecs;import origami.Origami;public class HelloCv {public static void main (String [] args) throws Exception {System.loadLibrary (Core.NATIVE_LIBRARY_NAME); Mat mat = Imgcodecs.imread (". / images/test.jpg") System.out.println (mat.width () + "x" + mat.height () + "," + mat.type ());}}

A successful load will output the following message

This function can also load grayscale images

Control through IMREAD_GRAYSCALE

Mat mat = Imgcodecs.imread (". / images/test.jpg", Imgcodecs.IMREAD_GRAYSCALE)

IMREAD_GRAYSCALE forces an image to be converted to a grayscale image and loaded into a Mat object

In addition to IMREAD_GRAYSCALE parameters, you can pass other parameters to get specific processing channels and image depth.

Save pictures

The imwrite function can be used to save pictures, which is also used in the Imgcodecs class

Our picture is colored, changed to grayscale through IMREAD_GRAYSCALE, and output under the name of output.jpg

Import org.opencv.core.CvType;import org.opencv.core.Mat;import org.opencv.core.Core;import org.opencv.imgcodecs.Imgcodecs;import origami.Origami;public class HelloCv {public static void main (String [] args) throws Exception {System.loadLibrary (Core.NATIVE_LIBRARY_NAME); Mat mat = Imgcodecs.imread (". / images/test.jpg", Imgcodecs.IMREAD_GRAYSCALE) / / Grayscale loading System.out.println (mat.width () + "x" + mat.height () + "," + mat.type ()); Imgcodecs.imwrite (". / images/output.jpg", mat);}}

For JPEG, you can use the CV_IMWRITE_JPEG_QUALITY parameter, which ranges from 0 to 100 (the higher the value, the higher the image quality). The default value is 95.

For PNG, you can use 0room9 as a parameter value for the degree of compression, and a higher value indicates that the image is smaller and the compression time is longer. The default value is 3.

You can use another OpenCV object called MatOfInt to compress the output file using compression parameters. MatOfInt is an integer matrix, or a simpler form, an array.

Import org.opencv.core.CvType;import org.opencv.core.Mat;import org.opencv.core.Core;import org.opencv.core.MatOfInt;import org.opencv.imgcodecs.Imgcodecs;import origami.Origami;public class HelloCv {public static void main (String [] args) throws Exception {System.loadLibrary (Core.NATIVE_LIBRARY_NAME); Mat mat = Imgcodecs.imread (". / images/test.jpg", Imgcodecs.IMREAD_GRAYSCALE) System.out.println (mat.width () + "x" + mat.height () + "," + mat.type ()); MatOfInt moi = new MatOfInt (Imgcodecs.IMWRITE_PNG_COMPRESSION,9); Imgcodecs.imwrite (". / images/output.png", mat,moi);}}

Uncompressed pre-Siz

Compressed size

The above is based on OpenCv and JVM how to load and save image function of all the content, more and how to load and save image function based on OpenCv and JVM related content can search the previous articles or browse the following articles to learn ha! I believe the editor will add more knowledge to you. I hope you can support it!

Tags: Images parameters pictures functions grayscale functions content articles output size objects files more not bad success code can be passed that is form color Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS Microsoft Shulou Tech Info Docker