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 use OpenCV and JVM to realize Matrix Image processing

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to use OpenCV and JVM to implement matrix image processing. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The return value of the submat (int rowStart, int rowEnd, int colStart, int colEnd) function is a matrix object. The content is a submatrix or sub-region of the original image.

First we use imread to read the picture, and then output some information about the matrix object itself.

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);}}

Because the matrix is the original picture, its isSubmat is false.

Now we use the first form of the submat function, where the input parameters are the start and end values of each row and column.

Picture clipping 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 ("C:/HWKJ/ZRQ/OpenCv/matrixcv/images/test.jpg") System.out.println (mat); Mat submat = mat.submat (200,240,300,350); System.out.println (submat);}}

Here, pay attention to the size in submat. According to the size of the original drawing, if you exceed the size of the original drawing, an error will be reported as follows.

Then we output the cropped picture.

So how do you confirm the area where you want to intercept the image? That is to say, how to determine the filling of these four parameters? The following figure is an example

The intercepted picture

The other two ways of submat

Range (int row,int column)

Row: wide start and end range

Column: high start and end range

Mat submat2 = mat.submat (new Range (20300), new Range (100500)); Imgcodecs.imwrite (". / images/output2.png", submat2)

Rect (int x, int yjinint width, int height)

X: Abscissa

Y: ordinate

Width: wide

Height: high

Mat submat3 = mat.submat (new Rect (0200100100)); / / submat3.setTo (new Scalar (255 images/output3.png 0)); / / draw the picture as blue Imgcodecs.imwrite (". / images/output3.png", submat3)

Open setTo as follows:

Imgcodecs.imwrite (". / images/blurtest.png", mat)

Complete code:

Import org.opencv.core.CvType;import org.opencv.core.Scalar;import org.opencv.core.Mat;import org.opencv.core.Rect;import org.opencv.core.Range;import org.opencv.core.Core;import org.opencv.core.Size;import org.opencv.core.MatOfInt;import org.opencv.imgcodecs.Imgcodecs;import org.opencv.imgproc.Imgproc;import origami.Origami;public class HelloCv {public static void main (String [] args) throws Exception {System.loadLibrary (Core.NATIVE_LIBRARY_NAME) Mat mat = Imgcodecs.imread ("C:/HWKJ/ZRQ/OpenCv/matrixcv/images/test.jpg"); System.out.println (mat); Mat submat = mat.submat (200,400,200,550); / / System.out.println (submat); Imgcodecs.imwrite (". / images/output.png", submat); Mat submat2 = mat.submat (new Range (20300), new Range (100500)) Imgcodecs.imwrite (". / images/output2.png", submat2); Mat submat3 = mat.submat (new Rect (0200400200)); submat3.setTo (new Scalar (255 images/output3.png)); Imgcodecs.imwrite (". / images/output3.png", submat3); / / Imgproc.blur (submat,submat,new Size (25.0 images/blurtest.png)); Imgcodecs.imwrite (". / images/blurtest.png", mat) }} Image blurring import org.opencv.core.CvType;import org.opencv.core.Mat;import org.opencv.core.Core;import org.opencv.core.Size;import org.opencv.core.MatOfInt;import org.opencv.imgcodecs.Imgcodecs;import org.opencv.imgproc.Imgproc;import origami.Origami;public class HelloCv {public static void main (String [] args) throws Exception {System.loadLibrary (Core.NATIVE_LIBRARY_NAME) Mat mat = Imgcodecs.imread ("C:/HWKJ/ZRQ/OpenCv/matrixcv/images/test.jpg"); System.out.println (mat); Mat submat = mat.submat (200,400,200,550); / / System.out.println (submat); / / Imgcodecs.imwrite (". / images/output.png", submat); Imgproc.blur (submat,submat,new Size (25.0) System.out.println ("after:" + mat); Imgcodecs.imwrite (". / images/blurtest.png", mat);}}

Submatrix generating matrix

SetTo and copyTo are two very important functions in OpenCv.

SetTo can set all pixels in a matrix to a specified color

CopyTo can copy an existing matrix to another matrix.

The first color value represents the depth of blue, the second value represents the depth of green, and the last value represents the depth of red.

/ / get red, green and blue Scalar Red = new Scalar (0mem0255); Scalar Green = new Scalar (0min255rep 0); Scalar Blue = new Scalar (255meme 0re0)

We think of these colors as complementary colors to RGB. So set the other channels to a maximum of 255 and the main channel to 0. Turquoise is a complementary color to red, so the red value channel is set to 0 and the other two channels are set to 255

Define turquoise, magenta, and yellow

Scalar cyan = new Scalar (255pm 0255); Scalar magena= new Scalar (255pm 0255); Scalar yellow = new Scalar (0255255)

Let's use setTo to set the submatrix to the given Scalar color

Private void setColors (Mat mat, boolean comp,int row) {for (int I = 0; I)

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