How to reconstruct image/array from list of extracted patches in python?
up vote
2
down vote
favorite
I have an image size of 256x256
. I will extract the image into patches with a size of 32x32
, and the center of a patch will slide with window step of 20. If extracted patch size is smaller than 32x32
. Then add zero padding to obtain 32x32
. The code to extract patch is
import numpy as np
image = np.random.randn(256,256)
patch_H, patch_W = 32, 32
step_H, step_W = 20, 20
lst_H = np.arange(0, image.shape[0] + patch_H, step_H)
lst_W = np.arange(0, image.shape[1] + patch_W, step_W)
image_patches =
for i in range(len(lst_H)):
for j in range(len(lst_W)):
h= lst_H[i]
w= lst_W[j]
patch = image[h : h + patch_H, w : w + patch_W]
# Zero padding
if (patch.shape[0]!= patch_H or patch.shape[1]!= patch_W):
patch = np.pad(patch,[(0, patch_H- patch.shape[0]), (0, patch_W- patch.shape[1])], mode='constant')
patch =patch[None,:,:]
image_patches.append(patch)
image_patches = np.vstack(image_patches) # (225, 32, 32)
Using the code above, I obtain 225 patches size of 32x32
. From the image_patches
, I want to generate a new image with the same size as the original image (256x256
) and the values are summed up between overlapping patches.
The difficult is that I have to unpad the patches that are zero padding in the first step. Thus I cannot assign the image_rec[h : h + patch_H, w : w + patch_W]+=image_patches[num_patches,:,:]
in bellow code if I did not unpad them. How could I solve the problem? In my opinion, I guess we have to save a dictionary to save the other keys such as zero flag, position...This is what I done
# Recover the image
image_rec = np.zeros (image.shape)
num_patches=0
for i in range(len(lst_H)):
for j in range(len(lst_W)):
h= lst_H[i]
w= lst_W[j]
image_rec[h : h + patch_H, w : w + patch_W]+=image_patches[num_patches,:,:]
num_patches +=1
python arrays python-3.x image
add a comment |
up vote
2
down vote
favorite
I have an image size of 256x256
. I will extract the image into patches with a size of 32x32
, and the center of a patch will slide with window step of 20. If extracted patch size is smaller than 32x32
. Then add zero padding to obtain 32x32
. The code to extract patch is
import numpy as np
image = np.random.randn(256,256)
patch_H, patch_W = 32, 32
step_H, step_W = 20, 20
lst_H = np.arange(0, image.shape[0] + patch_H, step_H)
lst_W = np.arange(0, image.shape[1] + patch_W, step_W)
image_patches =
for i in range(len(lst_H)):
for j in range(len(lst_W)):
h= lst_H[i]
w= lst_W[j]
patch = image[h : h + patch_H, w : w + patch_W]
# Zero padding
if (patch.shape[0]!= patch_H or patch.shape[1]!= patch_W):
patch = np.pad(patch,[(0, patch_H- patch.shape[0]), (0, patch_W- patch.shape[1])], mode='constant')
patch =patch[None,:,:]
image_patches.append(patch)
image_patches = np.vstack(image_patches) # (225, 32, 32)
Using the code above, I obtain 225 patches size of 32x32
. From the image_patches
, I want to generate a new image with the same size as the original image (256x256
) and the values are summed up between overlapping patches.
The difficult is that I have to unpad the patches that are zero padding in the first step. Thus I cannot assign the image_rec[h : h + patch_H, w : w + patch_W]+=image_patches[num_patches,:,:]
in bellow code if I did not unpad them. How could I solve the problem? In my opinion, I guess we have to save a dictionary to save the other keys such as zero flag, position...This is what I done
# Recover the image
image_rec = np.zeros (image.shape)
num_patches=0
for i in range(len(lst_H)):
for j in range(len(lst_W)):
h= lst_H[i]
w= lst_W[j]
image_rec[h : h + patch_H, w : w + patch_W]+=image_patches[num_patches,:,:]
num_patches +=1
python arrays python-3.x image
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have an image size of 256x256
. I will extract the image into patches with a size of 32x32
, and the center of a patch will slide with window step of 20. If extracted patch size is smaller than 32x32
. Then add zero padding to obtain 32x32
. The code to extract patch is
import numpy as np
image = np.random.randn(256,256)
patch_H, patch_W = 32, 32
step_H, step_W = 20, 20
lst_H = np.arange(0, image.shape[0] + patch_H, step_H)
lst_W = np.arange(0, image.shape[1] + patch_W, step_W)
image_patches =
for i in range(len(lst_H)):
for j in range(len(lst_W)):
h= lst_H[i]
w= lst_W[j]
patch = image[h : h + patch_H, w : w + patch_W]
# Zero padding
if (patch.shape[0]!= patch_H or patch.shape[1]!= patch_W):
patch = np.pad(patch,[(0, patch_H- patch.shape[0]), (0, patch_W- patch.shape[1])], mode='constant')
patch =patch[None,:,:]
image_patches.append(patch)
image_patches = np.vstack(image_patches) # (225, 32, 32)
Using the code above, I obtain 225 patches size of 32x32
. From the image_patches
, I want to generate a new image with the same size as the original image (256x256
) and the values are summed up between overlapping patches.
The difficult is that I have to unpad the patches that are zero padding in the first step. Thus I cannot assign the image_rec[h : h + patch_H, w : w + patch_W]+=image_patches[num_patches,:,:]
in bellow code if I did not unpad them. How could I solve the problem? In my opinion, I guess we have to save a dictionary to save the other keys such as zero flag, position...This is what I done
# Recover the image
image_rec = np.zeros (image.shape)
num_patches=0
for i in range(len(lst_H)):
for j in range(len(lst_W)):
h= lst_H[i]
w= lst_W[j]
image_rec[h : h + patch_H, w : w + patch_W]+=image_patches[num_patches,:,:]
num_patches +=1
python arrays python-3.x image
I have an image size of 256x256
. I will extract the image into patches with a size of 32x32
, and the center of a patch will slide with window step of 20. If extracted patch size is smaller than 32x32
. Then add zero padding to obtain 32x32
. The code to extract patch is
import numpy as np
image = np.random.randn(256,256)
patch_H, patch_W = 32, 32
step_H, step_W = 20, 20
lst_H = np.arange(0, image.shape[0] + patch_H, step_H)
lst_W = np.arange(0, image.shape[1] + patch_W, step_W)
image_patches =
for i in range(len(lst_H)):
for j in range(len(lst_W)):
h= lst_H[i]
w= lst_W[j]
patch = image[h : h + patch_H, w : w + patch_W]
# Zero padding
if (patch.shape[0]!= patch_H or patch.shape[1]!= patch_W):
patch = np.pad(patch,[(0, patch_H- patch.shape[0]), (0, patch_W- patch.shape[1])], mode='constant')
patch =patch[None,:,:]
image_patches.append(patch)
image_patches = np.vstack(image_patches) # (225, 32, 32)
Using the code above, I obtain 225 patches size of 32x32
. From the image_patches
, I want to generate a new image with the same size as the original image (256x256
) and the values are summed up between overlapping patches.
The difficult is that I have to unpad the patches that are zero padding in the first step. Thus I cannot assign the image_rec[h : h + patch_H, w : w + patch_W]+=image_patches[num_patches,:,:]
in bellow code if I did not unpad them. How could I solve the problem? In my opinion, I guess we have to save a dictionary to save the other keys such as zero flag, position...This is what I done
# Recover the image
image_rec = np.zeros (image.shape)
num_patches=0
for i in range(len(lst_H)):
for j in range(len(lst_W)):
h= lst_H[i]
w= lst_W[j]
image_rec[h : h + patch_H, w : w + patch_W]+=image_patches[num_patches,:,:]
num_patches +=1
python arrays python-3.x image
python arrays python-3.x image
edited Nov 19 at 17:40
iPython
1
1
asked Nov 19 at 17:06
John
91031332
91031332
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379515%2fhow-to-reconstruct-image-array-from-list-of-extracted-patches-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown