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
You will need to add more tiles to get the complete image. For the pred_32
case 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.