我有一张 RGB 图像,想将其转换为 numpy 数组。我尝试了以下操作:
im = cv.LoadImage("abc.tiff")
a = numpy.asarray(im)
它创建了一个没有形状的数组。我猜它是一个IplImage对象。
您可以使用更新的OpenCV Python接口(如果我没有记错,它自OpenCV 2.2版本起可用)。 它本地使用NumPy数组:
import cv2
im = cv2.imread("abc.tiff",mode='RGB')
print(type(im))
结果:
<type 'numpy.ndarray'>
$ pip install opencv-python
命令来安装OpenCV。 - krcmode
参数。请看下面的答案以获取更新后的方法。 - belvederefPIL(Python Imaging Library)和Numpy很好地配合使用。
我使用以下函数。
from PIL import Image
import numpy as np
def load_image( infilename ) :
img = Image.open( infilename )
img.load()
data = np.asarray( img, dtype="int32" )
return data
def save_image( npdata, outfilename ) :
img = Image.fromarray( np.asarray( np.clip(npdata,0,255), dtype="uint8"), "L" )
img.save( outfilename )
'Image.fromarray'有点丑陋,因为它对输入数据进行了剪切以保持在[0,255]的范围内,然后将其转换为字节,最后创建了一张灰度图像。我大多数时间都在处理灰度图像。一个RGB图像可能如下所示:out_img = Image.fromarray( ycc_uint8, "RGB" )
out_img.save( "ycc.tif" )
TypeError: long() argument must be a string or a number, not 'PixelAccess'
,查看PIL的PixelAccess
类文档,似乎没有提供能够使np.array
将其底层数据转换为ndarray
格式的方法。您需要省略使用img.load()
,只处理Image.open(...)
的结果。 - elyfrom matplotlib.image import imread
img = imread('abc.tiff')
print(type(img))
输出结果:<class 'numpy.ndarray'>
截至今天,你最好的选择是使用:
img = cv2.imread(image_path) # reads an image in the BGR format
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # BGR -> RGB
您会看到img
将是一种numpy数组类型:
<class 'numpy.ndarray'>
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # BGR -> RGB
的优点。 - Mafcv2.COLOR_BGR2GRAY
。 - belvederef虽然回答晚了,但我现在更喜欢imageio
模块而不是其他替代品。
import imageio
im = imageio.imread('abc.tiff')
与cv2.imread()
类似,它默认生成一个numpy数组,但是以RGB形式呈现。
您需要使用cv.LoadImageM而不是cv.LoadImage:
In [1]: import cv
In [2]: import numpy as np
In [3]: x = cv.LoadImageM('im.tif')
In [4]: im = np.asarray(x)
In [5]: im.shape
Out[5]: (487, 650, 3)
numpy
和PIL中的Image
轻松地获取rgb图像的numpy数组。import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
im = Image.open('*image_name*') #These two lines
im_arr = np.array(im) #are all you need
plt.imshow(im_arr) #Just to verify that image array has been constructed properly
from keras.preprocessing import image
X_test=image.load_img('four.png',target_size=(28,28),color_mode="grayscale"); #loading image and then convert it into grayscale and with it's target size
X_test=image.img_to_array(X_test); #convert image into array
使用David Poole的答案时,如果是灰度PNG文件或其他文件可能会出现SystemError。我的解决方案是:
import numpy as np
from PIL import Image
img = Image.open( filename )
try:
data = np.asarray( img, dtype='uint8' )
except SystemError:
data = np.asarray( img.getdata(), dtype='uint8' )
实际上,img.getdata() 可以用于所有文件,但它的速度较慢,因此我只在其他方法失败时使用它。
OpenCV图像格式支持numpy数组接口。可以制作一个辅助函数来支持灰度或彩色图像。这意味着BGR -> RGB转换可以通过numpy切片方便地完成,而不是完整复制图像数据。
注意:这是一种步幅技巧,因此修改输出数组也会更改OpenCV图像数据。如果需要副本,请在数组上使用.copy()
方法!
import numpy as np
def img_as_array(im):
"""OpenCV's native format to a numpy array view"""
w, h, n = im.width, im.height, im.channels
modes = {1: "L", 3: "RGB", 4: "RGBA"}
if n not in modes:
raise Exception('unsupported number of channels: {0}'.format(n))
out = np.asarray(im)
if n != 1:
out = out[:, :, ::-1] # BGR -> RGB conversion
return out
cv
是OpenCV模块,那么你应该将其标记为这样。这个链接可能会有所帮助:http://opencv.willowgarage.com/documentation/python/cookbook.html#numpy-and-opencv - Paul