How to get pixel values of a binarized image in android studio?

asked 2018-11-13 10:19:25 -0600

Janrex gravatar image

I want to get the pixel value of a binarized image and store it in an integer array. I used openCV to grayscale the image and used adaptivethreshold for the binary: grayBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);

    Utils.bitmapToMat(imageBitmap,Rgba);//bitmap to Mat
    Imgproc.cvtColor(Rgba,grayMat,Imgproc.COLOR_RGB2GRAY);//grayscale
    //binary
  Imgproc.adaptiveThreshold(grayMat,imageBW,255,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY,5,4);
    //mat to bitmap
    Utils.matToBitmap(imageBW,grayBitmap);

and I want to get pixel value of imageBW to see the value of each pixels and possibly convert them into 1 and 0 depending o for black and 1 for white if necessary.

edit retag flag offensive close merge delete

Comments

possibly convert them into 1 and 0 depending o for black and 1 for white

wrong assumption here, already. opencv uses 255 for white, and 0 for black.

to see the value of each pixels

use: System.out.println(imageBW.dump());


please try to explain, what you're trying to achieve with the whole thing, else it is yet another XY-problem !

berak gravatar imageberak ( 2018-11-13 10:22:39 -0600 )edit

how can i store them? I would like to store the pixel values in an int array.

Janrex gravatar imageJanrex ( 2018-11-13 10:25:49 -0600 )edit

why do you need that ?

berak gravatar imageberak ( 2018-11-13 10:26:24 -0600 )edit

@berak I would like to simply see what value each pixel of a binary image is and store them in an int array. i would use that array to convert it bitmap and display that image i know there are mat to bitmap functions but this code is for experimentation.

Janrex gravatar imageJanrex ( 2018-11-13 10:29:22 -0600 )edit

so, print it out, then (or save a Mat to disc, using imwrite())

berak gravatar imageberak ( 2018-11-13 10:30:32 -0600 )edit

still, here's what you could try: (but opencv keeps binary images as bytes, not integers !:

byte[] bytes = new byte[ bwImage.total()];
bwImage.get(0,0, bytes);

(but that's an expensive, and silly copy)

berak gravatar imageberak ( 2018-11-13 10:34:09 -0600 )edit

@berak I need to save it in a int array. and convert it to bitmap this is an experiment to test if what I'm thinking is possible.

Janrex gravatar imageJanrex ( 2018-11-13 10:35:18 -0600 )edit

i doubt you need any bitmap conversion here.

berak gravatar imageberak ( 2018-11-13 10:36:27 -0600 )edit

@berak. last question what kind of value would be stored in every element of the array? I really dont know anything about Byte array.

Janrex gravatar imageJanrex ( 2018-11-13 10:56:41 -0600 )edit

all i'm saying is: use opencv methods to save the bwImage, don't convert to bitmap, or try to get arrays from it.

what kind of value would be stored in every element of the array?

255 for white, 0 for black, as i've said before already

berak gravatar imageberak ( 2018-11-13 11:00:52 -0600 )edit

okay thanks. This helped a lot!

Janrex gravatar imageJanrex ( 2018-11-13 11:02:11 -0600 )edit

@berak there was an error on the first line it says "incompatible types. required int, found long".

Janrex gravatar imageJanrex ( 2018-11-13 11:04:38 -0600 )edit

yea right,

byte[] bytes = new byte[(int) bwImage.total()];

but if you could not solve that, on your own, you have a serious problem ! (you don't know the basics of your language, also you never cared to look up the docs, but instead asked again ..)

berak gravatar imageberak ( 2018-11-13 11:07:50 -0600 )edit

@berak I treid the imageBW.get(0,0,bytes) and its giving me this error java.lang.UnsupportedOperationException: Mat data type is not compatible: 0

also, using imageBW.get(0,0,bytes) will save the pixel value of the first pixel to bytes[0]? edit: instead of byte i tried double.

Janrex gravatar imageJanrex ( 2018-11-14 07:31:59 -0600 )edit

https://docs.opencv.org/master/javado...

  • the get() version, that returns a double[]will only return a single pixel (with up to 4 channels)
  • the version, that takes an array by reference, will return as many elements, as you allocated for that array (and you have to use the correct type, which is byte here)
berak gravatar imageberak ( 2018-11-14 07:55:24 -0600 )edit

@berak changed it back to bytes and I check what value bytes[0] have and its -1. which is odd.

Janrex gravatar imageJanrex ( 2018-11-14 08:20:15 -0600 )edit

ah, right, that's another obstacle: java's byte type is signed [-128...128]

silly, isn't it ?

berak gravatar imageberak ( 2018-11-14 08:24:24 -0600 )edit

@berak is it still possible to get the pixel value of a mat and store that pixel value in an element of an array?

Janrex gravatar imageJanrex ( 2018-11-14 08:30:10 -0600 )edit

you're aleady doing this, no ?

and maybe you should forget about arrays at all, and try to find the proper opencv functionality for your problem

berak gravatar imageberak ( 2018-11-14 08:31:41 -0600 )edit

@berak. The reason I'm doing this is that my groupmate created a for loop that will be used in striding for CNN and he needs an array of pixel values(255,0 or 1,0) to get the feature map and convert the output array to image. we are only allowed to use opeCV for grayscale and binarization.

Janrex gravatar imageJanrex ( 2018-11-14 08:39:01 -0600 )edit