OpenCV for Android - Access to Mat Elements

What is the standard way to access and modify individual Mat elements in OpenCV4Android? Also, what is the data format for BGR (which, in my opinion, is the default) and shades of gray?
edit: Let me make it more specific. mat.get (row, col) returns a double array. What is in this array?

+6
source share
3 answers

What is the standard way to access and modify individual Mat elements in OpenCV4Android?

A Mat is what we call a โ€œmatrixโ€ in mathematics โ€” a rectangular array of values โ€‹โ€‹given by rows and columns. These "values" represent pixels in the case of a Mat image (for example, each matrix element may be the color of each pixel in a Mat image). From this tutorial:

enter image description here

in the above image you can see that the carโ€™s mirror is nothing more than a matrix containing all the pixel intensities of the dots.

So, how would you go about iterating through the matrix? How about this:

 for (int row=0; row<mat.rows(); row++) { for (int col=0; col<mat.cols(); col++ ) { //...do what you want.. //eg get the value of the 3rd element of 2nd row //by mat.get(2,3); } } 

What is the standard way to access and modify individual Mat elements in OpenCV4Android?

You get the value of the Mat element using its get(x)(y) function, where x is the first coordinate (row number) and y is the second coordinate (column number) of the element. For example, to get the 4th element of the 7th row of a BGR Mat image called bgrImageMat , use the get Mat method to get an array of type double , which will be 3 size, each element of the array representing each of the Blue , Green and Red channels BGR image format.

 double [] bgrColor = bgrImageMat.get(); 

Also, what is the data format for BGR (which, in my opinion, is the default) and shades of gray? edit: Let me make it more specific. mat.get (row, col) returns a double array. What is in this array?

You can read about the BGR color format and grayscale from the Internet. e.g. BGR and Grayscale .

In short, the BGR format has 3 channels: Blue , Green and Red . Thus, the double array returned by mat.get(row, col) when Mat is a BGR image is an array of size 3, and each of its elements contains the values โ€‹โ€‹of each of Blue , Green and Red respectively.

Similarly, the grayscale format is a 1-channel color format, so the returned double will be 1.

+7
source

If you just want to access some pixels, use its double [] get (int row, int col) and using put (int row, int col, double ...) . If you want to access the entire image or loop through the image data in a loop, it is best to copy the Mat data into a primitive Java data type. When you're done with the data, just copy the data back into the Mat structure.

Images use CV_8U , if you have a grayscale image, it will use CV_8UC1 , if you have an RGB image, it will use Mat CV_8UC3 > (3 channels CV_8U). CV_8U is the equivalent of a byte in java. :)

I can give you an example of the method that I use in Java (Android platform) for binarizing grayscale images:

 private Mat featuresVectorBinarization(Mat fv){ int size = (int) fv.total() * fv.channels(); double[] buff = new double[size]; fv.get(0, 0, buff); for(int i = 0; i < size; i++) { buff[i] = (buff[i] >= 0) ? 1 : 0; } Mat bv = new Mat(fv.size(), CvType.CV_8U); bv.put(0, 0, buff); return bv; } 

Hope this helps.

+12
source

What I understood from studying the OpenCV Mat object is that Mat is an object that can represent any image of pixels W x H.

Now let's say you want to access the center pixel of the image, and then

X = W / 2 Y = H / 2

Then you can access the pixel data as follows double [] data = matObject.get (x, y);

Now, what is the data and what is the size of the data array. It depends on the type of image. if the image has shades of gray, then data.length = 1, since there is only one channel, and the data [0] represents the color value of this pixel, ie 0 (black) - 255 (white)

if the image is a color image, then data.length = 4 (rgba), since there are four channels, and the data [0-n] represents the color value of this pixel

0
source

Source: https://habr.com/ru/post/925241/


All Articles