将 python 列表加载为 opencv 图像

发布于 2025-01-15 07:08:10 字数 862 浏览 0 评论 0原文

我有一个名为 image 的 2D python 列表,该数组仅包含 0-255 范围内的整数。 例如,列表是这样的

image = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]

我读过 如何使用 Opencv 2.4 将 python numpy 数组转换为 RGB 图像?,所以我将 python 列表转换为 numpy 数组

image = np.asarray(image)

我可以运行以下命令

cv2.imwrite('image.jpg', image)

但是我该怎么办直接将数组加载为图像而不将其保存到文件中?像这样的

opencvImage = cv2.imread(image, 0)

上面的代码显然不起作用,那么我如何将python列表或numpy数组直接加载到opencv而不将其保存为文件?预期的图像输出是 RGB 图像或灰度图像,预期的形状是列表/数组本身的形状。

下面的代码也不起作用,我也不明白为什么我可以将numpy数组保存为图像文件但opencv无法直接显示它

cv2.imshow("image", image)

上面的代码从opencv抛出assertionfailed错误

I have a 2D python list named image, the array contain only integer in the range from 0-255.
For example, the list is like this

image = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]

I have read How to convert a python numpy array to an RGB image with Opencv 2.4?, so i converted the python list to numpy array

image = np.asarray(image)

I could run the following

cv2.imwrite('image.jpg', image)

But how do i load the array as image directly without saving it to file ? something like this

opencvImage = cv2.imread(image, 0)

The above code obviously don't work, so how do i load python list or numpy array directly to opencv without saving it as file? the expected image output is rgb image or greyscale image, and the expected shape is the shape of the list/array itself.

The following code also don't work, i also don't understand why i can save the numpy array as image file but opencv cannot show it directly

cv2.imshow("image", image)

The code above throws assertionfailed error from opencv

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

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

发布评论

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

评论(2

蓝海 2025-01-22 07:08:10

根据提供的信息,唯一可能的输出是形状为 (4, 3) 的灰色图像。 OpenCV 图像只是基本的 Numpy 数组。您可以使用以下方法转换列表:

lst = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]
image = np.asarray(lst, dtype=np.uint8) # Specifying dtype is critical here

# image = array([[  0,  39,  57],
#                [255, 182, 124],
#                [ 19, 223, 200],
#                [176, 190, 100]])

然后您可以使用 cv2.imshow("image", image) 显示它,但请注意图像有点太小,不容易看到。另请注意,可能需要 cv2.waitKey() 循环,以便窗口不会冻结并正确打印图像。

Based on the provided information, the only possible output is a gray image with the shape (4, 3). OpenCV image are just basic Numpy arrays. You can convert the list using:

lst = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]
image = np.asarray(lst, dtype=np.uint8) # Specifying dtype is critical here

# image = array([[  0,  39,  57],
#                [255, 182, 124],
#                [ 19, 223, 200],
#                [176, 190, 100]])

Then you can show it with cv2.imshow("image", image) but be aware that the image is a bit too small to be easily seen. Note also that a cv2.waitKey() loop may be needed so the window does not freeze and print the image correctly.

酸甜透明夹心 2025-01-22 07:08:10

您可以折皱 numpy.array 然后除以 255.0 然后使用 cv2 显示图像,如下所示:

import numpy as np
import cv2

image = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]
image = np.array(image, copy=False) / 255.0

cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

或者您可以使用 matplotlib code> 用于显示如下图像:

import matplotlib.pyplot as plt
import numpy as np

image = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]
image = np.array(image, copy=False) / 255.0

plt.imshow(image); plt.axis('off')
plt.show()

输出:

在此处输入图像描述

You can crease numpy.array then divide by 255.0 then show the image with cv2 like below:

import numpy as np
import cv2

image = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]
image = np.array(image, copy=False) / 255.0

cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Or you can use matplotlib for showing the image like below:

import matplotlib.pyplot as plt
import numpy as np

image = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]
image = np.array(image, copy=False) / 255.0

plt.imshow(image); plt.axis('off')
plt.show()

Output:

enter image description here

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