如何将 RGB 图像转换为 numpy 数组?
我有一个 RGB 图像。我想将它转换为 numpy 数组。我做了以下操作
im = cv.LoadImage("abc.tiff")
a = numpy.asarray(im)
它创建了一个没有形状的数组。我假设它是一个 iplimage 对象。
I have an RGB image. I want to convert it to numpy array. I did the following
im = cv.LoadImage("abc.tiff")
a = numpy.asarray(im)
It creates an array with no shape. I assume it is a iplimage object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
您可以使用更新的 OpenCV python 接口(如果我没记错的话,它自 OpenCV 2.2 起就可用)。它本身使用 numpy 数组:
结果:
You can use newer OpenCV python interface (if I'm not mistaken it is available since OpenCV 2.2). It natively uses numpy arrays:
result:
PIL(Python 图像库)和 Numpy 可以很好地协同工作。
我使用以下功能。
“Image.fromarray”有点难看,因为我将传入数据剪辑为 [0,255],转换为字节,然后创建灰度图像。我大部分时间都穿灰色的衣服。
RGB 图像类似于:
PIL (Python Imaging Library) and Numpy work well together.
I use the following functions.
The 'Image.fromarray' is a little ugly because I clip incoming data to [0,255], convert to bytes, then create a grayscale image. I mostly work in gray.
An RGB image would be something like:
您还可以使用 matplotlib 来实现此目的。
输出:
<类'numpy.ndarray'>
You can also use matplotlib for this.
output:
<class 'numpy.ndarray'>
截至今天,您最好的选择是使用:
您将看到
img
将是类型为 numpy 的数组:As of today, your best bet is to use:
You'll see
img
will be a numpy array of type:迟到的答案,但与其他替代方案相比,我更喜欢
imageio
模块与
cv2.imread()
类似,它默认生成一个 numpy 数组,但采用 RGB形式。Late answer, but I've come to prefer the
imageio
module to the other alternativesSimilar to
cv2.imread()
, it produces a numpy array by default, but in RGB form.您可以使用
numpy
和Image from PIL 轻松获取 rgb 图像的 numpy 数组
You can get numpy array of rgb image easily by using
numpy
andImage from PIL
您需要使用 cv.LoadImageM 而不是 cv.LoadImage:
You need to use cv.LoadImageM instead of cv.LoadImage:
使用以下语法加载图像:-
load the image by using following syntax:-
当使用 David Poole 的答案时,我收到一个包含灰度 PNG 和其他文件的系统错误。我的解决方案是:
实际上 img.getdata() 适用于所有文件,但速度较慢,因此我仅在其他方法失败时才使用它。
When using the answer from David Poole I get a SystemError with gray scale PNGs and maybe other files. My solution is:
Actually img.getdata() would work for all files, but it's slower, so I use it only when the other method fails.
OpenCV 图像格式支持 numpy 数组接口。可以创建辅助函数来支持灰度或彩色图像。这意味着 BGR -> RGB 转换可以使用 numpy 切片方便地完成,而不是图像数据的完整副本。
注意:这是一个跨步技巧,因此修改输出数组也会更改 OpenCV 图像数据。如果你想要一个副本,请在数组上使用
.copy()
方法!OpenCV image format supports the numpy array interface. A helper function can be made to support either grayscale or color images. This means the BGR -> RGB conversion can be conveniently done with a numpy slice, not a full copy of image data.
Note: this is a stride trick, so modifying the output array will also change the OpenCV image data. If you want a copy, use
.copy()
method on the array!我还采用了 imageio,但我发现以下机制对于预处理和后处理很有用:
理由是我使用 numpy 进行图像处理,而不仅仅是图像显示。为此,uint8 很尴尬,所以我转换为 0 到 1 范围内的浮点值。
保存图像时,我注意到我必须自己剪切超出范围的值,否则我最终会得到真正的灰色输出。 (灰色输出是 imageio 将 [0, 256) 之外的整个范围压缩到该范围内的值的结果。)
还有一些其他奇怪的地方,我在评论中提到过。
I also adopted imageio, but I found the following machinery useful for pre- and post-processing:
The rationale is that I am using numpy for image processing, not just image displaying. For this purpose, uint8s are awkward, so I convert to floating point values ranging from 0 to 1.
When saving images, I noticed I had to cut the out-of-range values myself, or else I ended up with a really gray output. (The gray output was the result of imageio compressing the full range, which was outside of [0, 256), to values that were inside the range.)
There were a couple other oddities, too, which I mentioned in the comments.
使用 Keras:
Using Keras:
我们可以使用open CV2的以下函数来转换BGR 2 RGB格式。
We can use following function of open CV2 to convert BGR 2 RGB format.
尝试计时将图像加载到 numpy 数组的选项,它们非常相似。为了简单和快速,请使用
plt.imread
。结果:
Try timing the options to load an image to numpy array, they are quite similar. Go for
plt.imread
for simplicity and speed.Result:
您可以尝试以下方法。以下是文档的链接。
You can try the following method. Here is a link to the docs.