I am trying to read and display a tiff image with opencv. I tried different reading modes in imread (-1,0,1,2) The result of the code below displays the image wrongly as blue only while it is colored.
import numpy as np
import cv2
import matplotlib.pyplot as plt
def readImagesAndTimes():
# List of exposure times
times = np.array([ 1/30.0, 0.25, 2.5, 15.0 ], dtype=np.float32)
# List of image filenames
filenames = ["img01.tif", "img02.tif", "img03.tif", "img04.tif", "img05.tif"]
images = []
for filename in filenames:
im = cv2.imread("./data/hdr_images/" + filename, -1)
images.append(im)
return images, times
images, times = readImagesAndTimes()
for im in images:
print(im.shape)
plt.imshow(im, cmap = plt.cm.Spectral)
Original image:
[]
Displayed blue image of the code :
[]
The problem is that opencv uses bgr
color mode and matplotlib uses rgb
color mode. Therefore the red and blue color channels are switched.
You can easily fix that problem by proving matplotlib an rgb image or by using cv2.imshow
function.
BGR to RGB conversion:
for im in images:
# convert bgr to rgb
rgb = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
plt.imshow(rgb, cmap = plt.cm.Spectral)
opencv's imshow function:
for im in images:
# no color conversion needed, because bgr is also used by imshow
cv2.imshow('image',im)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With