Search code examples
pythonnumpyopencvimage-processing

Read black and white image with OpenCV


I tried to simply read a binary mask image that I have created. When I read it using

bw_mask = cv2.imread('image.png')

the image is read as a numpy array of zeros that looks like:

array([[[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    ...,
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]],

   [[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    ...,
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]],

   [[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    ...,
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]],

   ...,

   [[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    ...,
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]],

   [[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    ...,
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]],

   [[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0],
    ...,
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]]], dtype=uint8)

Could you please explain to me why, I really want to learn the reason in depth but in a clear layman words. the colored image opens just as a typical image(not array).

Why is it possible to read it, if these operations are applied?

bw_mask = cv2.imread('image.png', cv2.IMREAD_ANYDEPTH)
bw_mask = bw_mask.view(np.int32)
img_cv_16bit = bw_mask.astype(np.uint16)
img_cv_8bit = np.clip(img_cv_16bit // 16, 0, 255).astype(np.uint8)
result = cv2.resize(img_cv_8bit, (800, 600), interpolation=cv2.INTER_CUBIC)
plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))
plt.show()

I still do not understand why this works, I would appreciate your explanation and answering.


Solution

  • Works fine, though you should be explicit about only caring about grayscale:

    import cv2
    
    bw_mask = cv2.imread(
        filename='wrwoW.png',
        flags=cv2.IMREAD_GRAYSCALE,
    )
    print(bw_mask.min())
    print(bw_mask.max())
    print(bw_mask.sum())
    
    0
    255
    704116830
    

    This is especially true since (for some reason) the image you posted isn't actually saved in grayscale:

    image properties

    wrwoW.png: PNG image data, 2340 x 1900, 8-bit/color RGBA, non-interlaced