'Some greyscale images imported using opencv are displayed as black, some as greyscale. Why?

I am facing a problem I do not understand.

I have greyscale images where the object of interest is grey and white, and the background is black.

Some images are displayed as just black, though, when I import them into Python using opencv. Some images are displayed correctly. See code and examples below,

import cv2
import matplotlib.pyplot as plt
import numpy as np
from imantics import Polygons, Mask
import imantics as imcs
import skimage
from shapely.geometry import Polygon as Pollygon
import matplotlib.image as mpimg
import PIL

mask1 = cv2.imread('mask1.jpg', 0)
mask2 = cv2.imread('mask2.jpg', 0)

cv2.imshow("title", mask1)
cv2.waitKey()
cv2.imshow("title", mask2)
cv2.waitKey()

Mask1 is displayed correctly, mask1

Mask 2 is not displayed correctly (only black), mask2

I am performing further processing of the images - e.g. finding contour of grey and white areas in image, which is why I need to understand why there seems to be a problem with some of the imported images.



Solution 1:[1]

I figured out the answer.

The problem was due to the image size.

If I import mask2 as,

mask2 = cv2.imread('mask2.jpg',64)

It is displayed correctly. Previously I was just seeing a small part of the image which happened to be black.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WHeisenbergCV