【问题标题】:Visualize with OpenCV image read by tensorflow使用 tensorflow 读取的 OpenCV 图像进行可视化
【发布时间】:2016-08-11 07:57:44
【问题描述】:

通过下面的代码,我使用 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 读取的图像?

【问题讨论】:

  • 红蓝通道好像互换了,tensorflow中读取RGB图像的默认顺序是什么?是 BGR 还是 RGB?
  • 你是对的! Tensorflow 格式是 RGB,而 OpenCV 格式是 BGR。那么,如何在这两种颜色空间之间进行转换呢?
  • 我现在只是在看文档,您可能需要在加载后自己交换频道,因为我看不到任何指定频道顺序的选项

标签: opencv tensorflow


【解决方案1】:

试试:

bgr_img = cv2.cvtColor(original_image, cv2.COLOR_RGB2BGR)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2013-07-21
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    相关资源
    最近更新 更多