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