0

I am working within the scikit-image package to analyse medical images.

For that, I want to create binary images and only count the number of white pixels in the images.

I do as follows:

  1. Create a grayscale image
  2. Run a threshold on it (skimage.filters.threshold_otsu)
  3. binary = grayscale > treshold_otsu

What I expect to have as a result is a binary image. If I print the np.array of it, it is encoded in False / True statements. However, I would have expected it to be in 0 (black pixels) and 255 (white pixels) encoded.

With the False - True statements, I cannot be sure which statement stands for white or black. Is there a conventional naming, like white always is True for example?

Does anyone know why I result with the Boolean encoding and not in 0-255 type?

Thank you very much!

Phil
  • 29
  • 5

1 Answers1

0

I think your expectation comes from working in different frameworks perhaps? I personally would not expect the result of an expression of the type "A > B" to be anything other than boolean: something is either greater than another thing, or it isn't.

How you decide to encode visually whether True is black or white is a separate decision. Conventionally in scikit-image/matplotlib circles, True is white and False is black, yes.

It's important to understand that even for uint8 values in 0-255, it is a convention that 255 is white and 0 is black. Numbers don't have an intrinsic color. You can easily reverse it e.g. by using:

import numpy as np
import matplotlib.pyplot as plt

plt.imshow(
        np.array([[0, 255]], dtype=np.uint8),
        cmap='gray_r',  # _r means "reversed"
        )

This will show white for 0 and black for 255.

For the record, it's straightforward to convert the boolean image to uint8 in [0, 255] as you want:

binary_uint8 = binary.astype(np.uint8) * 255
Juan
  • 5,433
  • 21
  • 23
  • Thanks a lot for the helpful answer! I realize now I was prejudiced in my expectations and the boolean encoding makes sense. the gray_r add on also helped me a lot to further understand what is happening - Thanks! Have a good day :) – Phil Jan 11 '22 at 13:00