【发布时间】:2017-12-26 20:50:16
【问题描述】:
我正在生成图像直方图,将 jpeg 文件读取到 24 位数组中,并将其转换为 32 位 numpy 以使用 numpy.histogram 进行处理,遵循以下方法:
- 将文件读入 numpy 数组形状:(w,h,3) ->
img=imread(file,'RGB') - 转换为字节块大小:w*h*3 ->
by=img.tobytes() - 使用结构迭代器解压 ->
struct.iter_unpack('<3B',by) - 转换为 int ->
int.from_bytes(c, byteorder='big')
这种方法和我尝试过的其他方法(重塑为 w*h、3 等)的问题是 迭代器延迟,我的问题是:
有没有直接的方法在python中做到这一点没有迭代器,或者我应该写一个简单的C函数来做到这一点?
def jpeg2colors(fnme):
return np.asarray(
[int.from_bytes(c, byteorder='big')
for c in struct.iter_unpack('<3B', imread(fnme).tobytes())])
其他较慢的实现:
def conversionSLOW(fnme): # reshape to (w*h,3)
img = imread(fnme, mode='RGB')
return np.array([int.from_bytes(c, byteorder='big')
for c in img.reshape((img.shape[0] * img.shape[1], 3))])
def conversionbyte01(fnme): # int iteration -> closer to fastests
by = imread(fnme, mode='RGB').tobytes()
return np.asanyarray([int.from_bytes(by[c:c + 3], byteorder='big')
for c in range(0, len(by) - 3, 3)],
dtype=np.int32)
def conversionbyte02(fnme):
img = imread(fnme)
return np.array([int.from_bytes(c, byteorder='big') for c in img.reshape(img.shape[0] * img.shape[1], 3)])
def conversionbyte03(fnme):
img = imread(fnme)
return np.array([int.from_bytes(img.reshape(img.shape[0] * img.shape[1], 3), byteorder='big')])
编辑1:
找到了一个解决方案,创建一个 (w,h,4) numpy 数组并复制读取的图像,rest 简单快速(x40 从最快的迭代解决方案改进)
def fromJPG2_2int32_stbyst(fnme): # step by step
img = imread(fnme)
# create a (w,h,4) array and copy original
re = np.zeros((img.shape[0], img.shape[1], 4), dtype=np.uint8)
re[:, :, :-1] = img
# lineup to a byte structure ready for ' frombuffer'
re1 = re.reshape(img.shape[0] * img.shape[1] * 4)
by = re1.tobytes()
# got it just convert to int
cols = np.frombuffer(by, 'I')
return cols
def fromJPG2_2int32_v0(fnme): # more compact & efficient
img = imread(fnme)
re = np.zeros((img.shape[0], img.shape[1], 4), dtype=np.uint8)
re[:, :, :-1] = img
return np.frombuffer(re.reshape(img.shape[0] * img.shape[1] * 4).tobytes(), 'I')
def fromJPG2_2int32(fnme): # even better using numpy.c_[]
img = imread(fnme)
img = np.c_[img, np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)]
return np.frombuffer(img.reshape(img.shape[0] * img.shape[1] * 4).tobytes(), 'I')
【问题讨论】: