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.
https://docs.opencv.org/master/javado...
double[]
will only return a single pixel (with up to 4 channels)@berak changed it back to bytes and I check what value bytes[0] have and its -1. which is odd.
ah, right, that's another obstacle: java's
byte
type is signed [-128...128]silly, isn't it ?
@berak is it still possible to get the pixel value of a mat and store that pixel value in an element of an array?
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. 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.