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









share|improve this question




























    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









    share|improve this question


























      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









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 17:40









      iPython

      1




      1










      asked Nov 19 at 17:06









      John

      91031332




      91031332





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python