gpt4 book ai didi

python - 将多张图像连接成一张

转载 作者:行者123 更新时间:2023-12-01 01:32:21 25 4
gpt4 key购买 nike

此函数接收由图像的裁剪部分组成的 numpy 数组列表。除了最右边和最底部的图像尺寸可能较小之外,所有裁剪图像的尺寸都相同。

predictions[2] 将返回从原始图像中裁剪的第三个子图像。每个裁剪都是一个 numpy 数组。有 WxH 裁剪,从左到右、从上到下枚举(因此,如果有 4 个子图像构成宽度,则 predictions 中的第 5 个图像将是左侧的第一个子图像第二行子图像)。

crops 包含查找构成重建图像的水平和垂直图像数量所需的信息。 crops[2][3] 将包含裁剪后的顶部第三个、左侧第四个图像。

crops 包含的图像比 predictions 中的图像尺寸更小(我基本上是在制作一个提高图像分辨率的模型)。重建图像来自预测中的图像,其排列顺序与crops中的图像相同。

def reconstruct(predictions, crops):
if len(crops) != 0:
print("use crops")

# TODO: properly extract the size of the full image
width_length = 0
height_length = 0

full_image = np.empty(shape=(height_length, width_length))
print(full_image.shape)

# TODO: properly merge the crops back into a single image
for height in range(len(predictions[0])):
for width in range(len(predictions)):
# concatenate here
print(height, width)

return full_image

我打算使用numpy.concatenate ,但根据我在 SO 上看到的其他答案,这不是一种有效的方法(显然 numpy 只会在内存中重新创建一个新变量,复制旧变量,然后添加新数据等)。所以现在我想知道如何正确地将多个图像合并为单个图像。我当前的想法是创建一个适当形状的 python 列表,并逐步用每个 numpy 数组的数据填充它,但即使如此,我也不确定这是否是正确的想法。

这或多或少是我试图连接成单个图像的一堆图像:

split images

这是预期的结果:

expected result

为了帮助您了解还有哪些功能可供您使用,这里还有一些代码:

def predict(args):
model = load_model(save_dir + '/' + args.model)
image = skimage.io.imread(tests_path + args.image)

predictions = []
images = []

crops = seq_crop(image) # crops into multiple sub-parts the image based on 'input_' constants

for i in range(len(crops)): # amount of vertical crops
for j in range(len(crops[0])): # amount of horizontal crops
current_image = crops[i][j]
images.append(current_image)

# Hack because GPU can only handle one image at a time
input_img = (np.expand_dims(images[p], 0)) # Add the image to a batch where it's the only member
predictions.append(model.predict(input_img)[0]) # returns a list of lists, one for each image in the batch

return predictions, image, crops


# adapted from: https://stackoverflow.com/a/52463034/9768291
def seq_crop(img):
"""
To crop the whole image in a list of sub-images of the same size.
Size comes from "input_" variables in the 'constants' (Evaluation).
Padding with 0 the Bottom and Right image.

:param img: input image
:return: list of sub-images with defined size
"""
width_shape = ceildiv(img.shape[1], input_width)
height_shape = ceildiv(img.shape[0], input_height)
sub_images = [] # will contain all the cropped sub-parts of the image

for j in range(height_shape):
horizontal = []
for i in range(width_shape):
horizontal.append(crop_precise(img, i*input_width, j*input_height, input_width, input_height))
sub_images.append(horizontal)

return sub_images

def crop_precise(img, coord_x, coord_y, width_length, height_length):
"""
To crop a precise portion of an image.
When trying to crop outside of the boundaries, the input to padded with zeros.

:param img: image to crop
:param coord_x: width coordinate (top left point)
:param coord_y: height coordinate (top left point)
:param width_length: width of the cropped portion starting from coord_x
:param height_length: height of the cropped portion starting from coord_y
:return: the cropped part of the image
"""

tmp_img = img[coord_y:coord_y + height_length, coord_x:coord_x + width_length]

return float_im(tmp_img) # From [0,255] to [0.,1.]

# from https://stackoverflow.com/a/17511341/9768291
def ceildiv(a, b):
"""
To get the ceiling of a division
:param a:
:param b:
:return:
"""
return -(-a // b)

if __name__ == '__main__':
preds, original, crops = predict(args) # returns the predictions along with the original

# TODO: reconstruct image
enhanced = reconstruct(preds, crops) # reconstructs the enhanced image from predictions

编辑:

答案有效。这是我使用过的版本:

# adapted from  https://stackoverflow.com/a/52733370/9768291
def reconstruct(predictions, crops):

# unflatten predictions
def nest(data, template):
data = iter(data)
return [[next(data) for _ in row] for row in template]

predictions = nest(predictions, crops)

H = np.cumsum([x[0].shape[0] for x in predictions])
W = np.cumsum([x.shape[1] for x in predictions[0]])
D = predictions[0][0]
recon = np.empty((H[-1], W[-1], D.shape[2]), D.dtype)
for rd, rs in zip(np.split(recon, H[:-1], 0), predictions):
for d, s in zip(np.split(rd, W[:-1], 1), rs):
d[...] = s
return recon

最佳答案

最方便的可能是np.block

import numpy as np
from scipy import misc
import Image

# get example picture
data = misc.face()
# chop it up
I, J = map(np.arange, (200, 200), data.shape[:2], (200, 200))
chops = [np.split(row, J, axis=1) for row in np.split(data, I, axis=0)]

# do something with the bits

predictions = [chop-(i+j)*(chop>>3) for j, row in enumerate(chops) for i, chop in enumerate(row)]

# unflatten predictions
def nest(data, template):
data = iter(data)
return [[next(data) for _ in row] for row in template]

pred_lol = nest(predictions, chops)

# almost builtin reconstruction
def np_block_2D(chops):
return np.block([[[x] for x in row] for row in chops])

recon = np_block_2D(pred_lol)
Image.fromarray(recon).save('demo.png')

重建的操纵图像:

enter image description here

但是我们可以通过避免中间数组来做得更快。相反,我们复制到预先分配的数组中:

def speed_block_2D(chops):
H = np.cumsum([x[0].shape[0] for x in chops])
W = np.cumsum([x.shape[1] for x in chops[0]])
D = chops[0][0]
recon = np.empty((H[-1], W[-1], D.shape[2]), D.dtype)
for rd, rs in zip(np.split(recon, H[:-1], 0), chops):
for d, s in zip(np.split(rd, W[:-1], 1), rs):
d[...] = s
return recon

时间,还包括每种方法的通用 ND 就绪变体:

numpy 2D:               0.991 ms
prealloc 2D: 0.389 ms
numpy general: 1.021 ms
prealloc general: 0.448 ms

一般情况和时序代码:

def np_block(chops):
d = 0
tl = chops
while isinstance(tl, list):
tl = tl[0]
d += 1
if d < tl.ndim:
def adjust_depth(L):
if isinstance(L, list):
return [adjust_depth(l) for l in L]
else:
ret = L
for j in range(d, tl.ndim):
ret = [ret]
return ret
chops = adjust_depth(chops)
return np.block(chops)

def speed_block(chops):
def line(src, i):
while isinstance(src, list):
src = src[0]
return src.shape[i]
def hyper(src, i):
src = iter(src)
fst = next(src)
if isinstance(fst, list):
res, dtype, szs = hyper(fst, i+1)
szs.append([res[i], *(line(s, i) for s in src)])
res[i] = sum(szs[-1])
return res, dtype, szs
res = np.array(fst.shape)
szs = [res[i], *(s.shape[i] for s in src)]
res[i] = sum(szs)
return res, fst.dtype, [szs]
shape, dtype, szs = hyper(chops, 0)
recon = np.empty(shape, dtype)
def cpchp(dst, src, i, szs=None):
szs = np.array(hyper(src, i)[2]) if szs is None else szs
dst = np.split(dst, np.cumsum(szs[-1][:-1]), i)
if isinstance(src[0], list):
szs = szs[:-1]
for ds, sr in zip(dst, src):
cpchp(ds, sr, i+1, szs)
szs = None
else:
for ds, sr in zip(dst, src):
ds[...] = sr
cpchp(recon, chops, 0, np.array(szs))
return recon

from timeit import timeit

T = (timeit(lambda: speed_block(pred_lol), number=1000),
timeit(lambda: np_block(pred_lol), number=1000),
timeit(lambda: speed_block_2D(pred_lol), number=1000),
timeit(lambda: np_block_2D(pred_lol), number=1000))

assert (np.all(speed_block(pred_lol)==np_block(pred_lol)) and
np.all(speed_block_2D(pred_lol)==np_block(pred_lol)) and
np.all(speed_block(pred_lol)==np_block_2D(pred_lol)))

print(f"""
numpy 2D: {T[3]:10.3f} ms
prealloc 2D: {T[2]:10.3f} ms
numpy general: {T[1]:10.3f} ms
prealloc general: {T[0]:10.3f} ms
""")

关于python - 将多张图像连接成一张,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52730668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com