2
votes

I want to load and display a .tif image in OpenCV Python. I load the image using cv2.imread('1_00001.tif') and then I display it using plt.imshow(img), but the image displayed is all black instead of what it was originally.

I can load and display the image correctly using PIL's Image.open() and matplotlib's mpimg.imread() so I think it is a cv2 specific problem. However, I have also successfully displayed .jpg and .tiff images using the same cv2.imread() function so it may also be a problem with specifically that .tif image.

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('1_00001.tif')
plt.imshow(img)

I expect an image of a circle with a few blurry lines inside, but the actual output is just a black image.

2
It may be helpful to restate the question at the end of your post.Len Joseph
Try printing img.shape and img.dtype and img.max().Mark Setchell
Your current code seems to work with opening various .tif images, its most likely a problem with that specific .tif imagenathancy
Can you share the problematic image?shortcipher3

2 Answers

2
votes

Check your image pixel values. plt.imshow clips pixel values from 0-255, so I would guess that you're feeding in a PNG image with values greater than 255, and they're all getting clipped to 255 (black). Usually you'll want to normalize a TIFF or PNG image before feeding them to plt.imshow, so it's interesting that you're not seeing this problem on some tiff images.

0
votes

Im think, Some tiff tags not work properly with openCV try

img=cv2.imread("YOURPATH/opencv/samples/data/lena.jpg",cv2.IMREAD_COLOR)
cv2.imwrite("1_00001.tif",img)
img1=cv2.imread("1_00001.tif")