Bear Bear - 3 months ago 8
Python Question

same binary image - different in python & matlab

OK. I'm thoroughly confused. I have a binary image. The values are (allegedly) 0 and 1. I read it in Matlab, to verify:

binaryImage = imread('binary.png');


I get the max and min values, and the values are 1 and 0, respectively.

maxValue = max(binaryImage(:));
minValue = max(binaryImage(:));


I take the same exact binary image, and read it in Python...

from scipy.misc import imread


to get the pixel values we flatten it

li = img.flatten()

# check each value
for s in li:
print s


The values, according to Python, are 0 and 255.

So... does this mean if I use C++ (ITK) I will get a different value? If I use OpenCV I will get a different value? (Technically the "on" values should only be 1 and 255, but you know what I mean.)

If the pixel is "on" (white), and I set the value to 1 (in a binary image), no matter what language I read it in, I expect to see 1 for that value.

How come it's different?

Thanks!

Answer

matlab supports logical arrays and scipy converts the image to grayscale. Depending on what you want to do you can convert the grayscale image to logicals

bit_img = img > 127

or convert the logical array to int8 in matlab

img = int8(binary_image) * 255