OpenCV 灰度图像具有形状 (H,W),但我需要 (H,W,1) 来实现 Tensorflow

发布于 2025-01-17 09:02:00 字数 1491 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期情话 2025-01-24 09:02:00

输入图像是3个通道,但灰度尺度有一个通道。

import cv2
img = cv2.imread("HHH.jpg")
print(f"img.shape={img.shape}")
gray = cv2.cvtColor(np.float32(img), cv2.COLOR_BGR2GRAY)  # Grayscale image
print(f"gray.shape={gray.shape}")

输出:

img.shape=(960, 1280, 3)
gray.shape=(960, 1280)

如果您需要在(H,W,1)尺寸中馈送图像到TensorFlow,则通过扩展尺寸在TF中更改您的尺寸:
此代码为数据的最后一个维度添加了一个通道:

 tf.expand_dims(image, -1).shape.as_list()

Input images are 3 channels but gray scales have one channel.

import cv2
img = cv2.imread("HHH.jpg")
print(f"img.shape={img.shape}")
gray = cv2.cvtColor(np.float32(img), cv2.COLOR_BGR2GRAY)  # Grayscale image
print(f"gray.shape={gray.shape}")

output:

img.shape=(960, 1280, 3)
gray.shape=(960, 1280)

If you need to feed images in (H, W, 1) dimension to TensorFlow, so change your dimension by expanding dimension in TF:
This code adds a one channel to last dimension of data:

 tf.expand_dims(image, -1).shape.as_list()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文