使用下面的代码,我使用OpenCV和Tensorflow读取相同的图像。
import tensorflow as tf
import cv2
def get_image(image_path):
"""Reads the jpg image from image_path.
Returns the image as a tf.float32 tensor
Args:
image_path: tf.string tensor
Reuturn:
the decoded jpeg image casted to float32
"""
return tf.image.convert_image_dtype(
tf.image.decode_jpeg(
tf.read_file(image_path), channels=3),
dtype=tf.uint8)
path = "./images/2010_006748.jpg"
original_image = cv2.imread(path)
image_tensor = get_image(tf.constant(path))
# convert to uint8
image_tensor = tf.image.convert_image_dtype(image_tensor, dtype=tf.uint8)
with tf.Session() as sess:
image = sess.run(image_tensor)
cv2.imshow("tf", image)
cv2.imshow("original", original_image)
cv2.waitKey(0)
从图像中可以看出,OpenCV(正确的颜色)和Tensorflow(错误的颜色)读取的图像之间存在差异。
我尝试使用cv2.normalize(image, image, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8UC3)
规范化Tensorflow图像的颜色,但没有任何改变。
我还尝试将图片读作tf.uint8
(将初始广告素材移至tf.float32
),但没有更改。
如何正确使用OpenCV显示使用Tensorflow读取的图像?
答案 0 :(得分:6)
尝试:
bgr_img = cv2.cvtColor(original_image, cv2.COLOR_RGB2BGR)