将 python 列表加载为 opencv 图像
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据提供的信息,唯一可能的输出是形状为
(4, 3)
的灰色图像。 OpenCV 图像只是基本的 Numpy 数组。您可以使用以下方法转换列表:然后您可以使用
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: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 acv2.waitKey()
loop may be needed so the window does not freeze and print the image correctly.您可以折皱
numpy.array
然后除以255.0
然后使用cv2
显示图像,如下所示:或者您可以使用
matplotlib
code> 用于显示如下图像:输出:
You can crease
numpy.array
then divide by255.0
then show the image withcv2
like below:Or you can use
matplotlib
for showing the image like below:Output: