如何使用 Opencv 2.4 将 python numpy 数组转换为 RGB 图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26681756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:51:58  来源:igfitidea点击:

How to convert a python numpy array to an RGB image with Opencv 2.4?

pythonopencvnumpy

提问by jmanring220

I have searched for similar questions, but haven't found anything helpful as most solutions use older versions of OpenCV.

我搜索了类似的问题,但没有找到任何有用的东西,因为大多数解决方案使用旧版本的 OpenCV。

I have a 3D numpy array, and I would like to display and/or save it as a BGR image using OpenCV (cv2).

我有一个 3D numpy 数组,我想使用 OpenCV (cv2) 将其显示和/或保存为 BGR 图像。

As a short example, suppose I had:

作为一个简短的例子,假设我有:

import numpy, cv2
b = numpy.zeros([5,5,3])

b[:,:,0] = numpy.ones([5,5])*64
b[:,:,1] = numpy.ones([5,5])*128
b[:,:,2] = numpy.ones([5,5])*192

What I would like to do is save and display b as a color image similar to:

我想要做的是将 b 保存并显示为类似于以下内容的彩色图像:

cv2.imwrite('color_img.jpg', b)
cv2.imshow('Color image', b)
cv2.waitKey(0)
cv2.destroyAllWindows()

This doesn't work, presumably because the data type of b isn't correct, but after substantial searching, I can't figure out how to change it to the correct one. If you can offer any pointers, it would be greatly appreciated!

这不起作用,大概是因为 b 的数据类型不正确,但经过大量搜索后,我无法弄清楚如何将其更改为正确的类型。如果您能提供任何指示,将不胜感激!

回答by jmunsch

The images c, d, e , and f in the following show colorspace conversion they also happen to be numpy arrays <type 'numpy.ndarray'>:

下面的图像 c、d、e 和 f 显示了颜色空间转换,它们也恰好是 numpy 数组<type 'numpy.ndarray'>

import numpy, cv2
def show_pic(p):
        ''' use esc to see the results'''
        print(type(p))
        cv2.imshow('Color image', p)
        while True:
            k = cv2.waitKey(0) & 0xFF
            if k == 27: break 
        return
        cv2.destroyAllWindows()

b = numpy.zeros([200,200,3])

b[:,:,0] = numpy.ones([200,200])*255
b[:,:,1] = numpy.ones([200,200])*255
b[:,:,2] = numpy.ones([200,200])*0
cv2.imwrite('color_img.jpg', b)


c = cv2.imread('color_img.jpg', 1)
c = cv2.cvtColor(c, cv2.COLOR_BGR2RGB)

d = cv2.imread('color_img.jpg', 1)
d = cv2.cvtColor(c, cv2.COLOR_RGB2BGR)

e = cv2.imread('color_img.jpg', -1)
e = cv2.cvtColor(c, cv2.COLOR_BGR2RGB)

f = cv2.imread('color_img.jpg', -1)
f = cv2.cvtColor(c, cv2.COLOR_RGB2BGR)


pictures = [d, c, f, e]

for p in pictures:
        show_pic(p)
# show the matrix
print(c)
print(c.shape)

See here for more info: http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor

有关更多信息,请参见此处:http: //docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor

OR you could:

或者你可以:

img = numpy.zeros([200,200,3])

img[:,:,0] = numpy.ones([200,200])*255
img[:,:,1] = numpy.ones([200,200])*255
img[:,:,2] = numpy.ones([200,200])*0

r,g,b = cv2.split(img)
img_bgr = cv2.merge([b,g,r])

回答by bikz05

You don't need to convert NumPyarray to Matbecause OpenCV cv2module can accept NumPyarray. The only thing you need to care for is that {0,1} is mapped to {0,255} and any value bigger than 1 in NumPyarray is equal to 255. So you should divide by 255 in your code, as shown below.

您不需要将NumPy数组转换为,Mat因为 OpenCVcv2模块可以接受NumPy数组。您唯一需要关心的是 {0,1} 映射到 {0,255} 并且NumPy数组中任何大于 1 的值都等于 255。因此您应该在代码中除以 255,如下所示。

img = numpy.zeros([5,5,3])

img[:,:,0] = numpy.ones([5,5])*64/255.0
img[:,:,1] = numpy.ones([5,5])*128/255.0
img[:,:,2] = numpy.ones([5,5])*192/255.0

cv2.imwrite('color_img.jpg', img)
cv2.imshow("image", img)
cv2.waitKey()

回答by Martin Thoma

You are looking for scipy.misc.toimage:

您正在寻找scipy.misc.toimage

import scipy.misc
rgb = scipy.misc.toimage(np_array)

It seems to be also in scipy 1.0, but has a deprecation warning. Instead, you can use pillowand PIL.Image.fromarray

它似乎也在scipy 1.0 中,但有弃用警告。相反,您可以使用pillowPIL.Image.fromarray

回答by AlexeyGy

If anyone else simply wants to display a black image as a background, here e.g. for 500x500 px:

如果其他人只想显示黑色图像作为背景,例如 500x500 像素:

import cv2
import numpy as np

black_screen  = np.zeros([500,500,3])
cv2.imshow("Simple_black", black_screen)
cv2.waitKey(0)

回答by Hasindu Samaraweera

The size of your image is not sufficient to see in a naked eye. So please try to use atleast 50x50

您的图像大小不足以用肉眼看到。所以请尽量使用至少 50x50

import cv2 as cv
import numpy as np

black_screen = np.zeros([50,50,3])

black_screen[:, :, 2] = np.ones([50,50])*64/255.0
cv.imshow("Simple_black", black_screen)
cv.waitKey(0)
cv.displayAllWindows()