Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report