且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何重现合并不同尺寸的多个图像

更新时间:2023-12-05 16:32:16

您将需要添加更多的图块以获取完整的图像.对于 pred_32 情况,您将需要16个输入图像,用于1个输出图像;对于 pred_16 情况,您将需要64个输入图像,对于1个输出图像.在这种情况下,编写循环移出"所需的输出图像并一次输入一个图像可能更容易.假设您的图像从左到右填充了较大的图像,我认为以下代码可能会为您提供帮助:

 #预分配新图像数组out_shape =(32,128,128,1))pred_128 = np.zeros(out_shape)#输入大小对于pred_16,dx = 32#16dy = 32#16对于pred_16#输入图像计数器input_im_no = 0#循环播放新图像对于范围内的我(0,out_shape [0]):对于范围内的y(0,int(out_shape [1]/dy)):对于x in range(0,int(out_shape [2]/dx)):pred_128 [i,0 + dx * x:dx *(x + 1),0 + dy * y:dy *(y + 1),0] = pred_32 [input_im_no,:,:,0]#选择下一张图片input_im_no + = 1 

问题更新后的x和y顺序.

I previously asked a question for merging every 4 images of size 64x64 to 128x128 and I edited the answer as below:

How to merge multiple images from CNN prediction into one image?

# Initializing counters
i = 0  # Old image number
j = 0  # New image number

# Pre-allocate new images array
pred_128 = np.zeros((32, 128, 128, 1))

# Loop over new images
while j < 32:
    pred_128 [j, :64, :64, 0] = pred_64[0+i, :, :, 0]  # Upper left
    pred_128 [j, 64:, :64, 0] = pred_64[2+i, :, :, 0]  # Lower left
    pred_128 [j, :64, 64:, 0] = pred_64[1+i, :, :, 0]  # Upper right
    pred_128 [j, 64:, 64:, 0] = pred_64[3+i, :, :, 0]  # Lower right

    # Add to counters
    i += 4
    j += 1 

I want now to reuse this code to generate (32, 128, 128, 1) from different image size and: 1- (512, 32, 32, 1) 2- (2048, 16, 16, 1)

For the first case (512, 32, 32, 1), I used the following code and it returns error:

# Initializing counters
i = 0  # Old image number
j = 0  # New image number

# Pre-allocate new images array
pred_128 = np.zeros((32, 128, 128, 1))

# Loop over new images
while j < 32:
    pred_128 [j, :32, :32, 0] = pred_32[0+i, :, :, 0]  # Upper left
    pred_128 [j, 32:, :32, 0] = pred_32[2+i, :, :, 0]  # Lower left
    pred_128 [j, :32, 32:, 0] = pred_32[1+i, :, :, 0]  # Upper right
    pred_128 [j, 32:, 32:, 0] = pred_32[3+i, :, :, 0]  # Lower right

    # Add to counters
    i += 8
    j += 1



ValueError                                Traceback (most recent call last)
<ipython-input-48-b4a45801c652> in <module>()
      9 while j < 32:
     10     pred_128 [j, :32, :32, 0] = pred_32[0+i, :, :, 0]  # Upper left
---> 11     pred_128 [j, 32:, :32, 0] = pred_32[2+i, :, :, 0]  # Lower left
     12     pred_128 [j, :32, 32:, 0] = pred_32[1+i, :, :, 0]  # Upper right
     13     pred_128 [j, 32:, 32:, 0] = pred_32[3+i, :, :, 0]  # Lower right

ValueError: could not broadcast input array from shape (32,32) into shape (96,32)

Can anyone help for reproducing the codes and solve the issue for the two different cases: 1- (512, 32, 32, 1) #merging every 16 images

2- (2048, 16, 16, 1) #merging every 64 images


error after using the proposed code:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-79-b71bf1e0ef80> in <module>()
     12 # Loop over new images
     13 for i in range(0, out_shape[0]):
---> 14     for x in range(0, out_shape[1]/dx):
     15         for y in range(0, out_shape[2]/dy):
     16             pred_128[i, 0+dx*x:dx*(x+1), 0+dy*y:dy*(y+1), 0] = pred_32[input_im_no, :, :, 0]

TypeError: 'float' object cannot be interpreted as an integer

-order of every 16 32x32 image in the original data

You will need to add more tiles to get the complete image. For the pred_32case you will need 16 input images for 1 output image and for the pred_16 case 64 input images for 1 output image. It is probably easier in this case to write a loop that 'shifts' over the desired output image and inputs one image at a time. Assuming your images are filling the greater image from left to right, I think the following code might help you out:

# Pre-allocate new images array
out_shape = (32, 128, 128, 1))
pred_128 = np.zeros(out_shape)

# Input sizes
dx = 32  # 16 for the pred_16
dy = 32  # 16 for the pred_16

# Input images counter
input_im_no = 0

# Loop over new images
for i in range(0, out_shape[0]):
    for y in range(0, int(out_shape[1]/dy)):
        for x in range(0, int(out_shape[2]/dx)):
            pred_128[i, 0+dx*x:dx*(x+1), 0+dy*y:dy*(y+1), 0] = pred_32[input_im_no, :, :, 0]

            # Select next image
            input_im_no += 1

EDIT: x and y order after question update.