How to get pixel values of a binarized image in android studio?
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.
wrong assumption here, already. opencv uses 255 for white, and 0 for black.
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 !
how can i store them? I would like to store the pixel values in an int array.
why do you need that ?
@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.
so, print it out, then (or save a Mat to disc, using imwrite())
still, here's what you could try: (but opencv keeps binary images as bytes, not integers !:
(but that's an expensive, and silly copy)
@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.
i doubt you need any bitmap conversion here.
@berak. last question what kind of value would be stored in every element of the array? I really dont know anything about Byte array.
all i'm saying is: use opencv methods to save the bwImage, don't convert to bitmap, or try to get arrays from it.
255 for white, 0 for black, as i've said before already
okay thanks. This helped a lot!
@berak there was an error on the first line it says "incompatible types. required int, found long".
yea right,
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 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.