Giter Club home page Giter Club logo

patchify.py's People

Contributors

dovahcrow avatar netaddi avatar vdivakar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

patchify.py's Issues

Different step size between axis

To cover my entire image dataset along the X-axis, I need to use a step size smaller than my image patch, while along the Y-axis, it fits perfectly with a step equal to the size of my patch.

Is it possible to use different step sizes for each axis?

distortion in the unpatchified image

The original image is in shape (844,1280) and I patched it with patchify using 128 steps , because I need like +40 patches with size of (256,256) , unpatchify succeeded but sadly I have distortions in the result , is there a way to fix this ?

patched = patchify(image,(256,256),step=128)
unpatched_img = unpatchify(patched, image.shape)

Figure 2020-12-23 141118
Figure 2020-12-23 141107

Does unpatchify perform averaging ?

So I am doing some image processing on 3D images and rather than operate on the entire image I want to split it first into patches and perform the image processing on each patch.

Say I have an image with dimensions 240x240x160 and I patchify it with a patch shape 40x40x40 and 20 as step size then the patches will be overlapping.

My question is the following:
When these overlapping patches are unpatchified, are the overlapping pixels averaged over all their values to get the final output ?

Thanks in advance.

patch.save() dpi by default 96

I am trying to save the images using the save method but the dpi value is by default 96. Is there any way to increase or decrease it by user?

Circular Import Issue

I am using the demo script verbatim in VS Code with Python 3.11 and I am getting a circular import error

ImportError: cannot import name 'patchify' from partially initialized module 'patchify' (most likely due to a circular import)

import numpy as np
from PIL import Image
from patchify import patchify
   
image = Image.open("C:\\Projects\\Python\\Roboflow\\patchify.jpg")
image = np.asarray(image)
patches = patchify(image, (512, 512, 3), step=512)
print(patches.shape)  # (6, 10, 1, 512, 512, 3)

for i in range(patches.shape[0]):
    for j in range(patches.shape[1]):
        patch = patches[i, j, 0]
        patch = Image.fromarray(patch)
        num = i * patches.shape[1] + j
        patch.save(f"patch_{num}.jpg")

Preserving tiff meta data

Is there anyway we can preserve patched tiff files meta data while pachifying or the extent data?

Getting fewer images than expected

Hi,

https://drive.google.com/file/d/14eJIc8H2fa0fu6afUaJ44D1n8YKsoeSI/view?usp=sharing

I am running this code to patchify a large satellite image (above). Per the dimensions of the image (9728x3840 pixels) I am supposed to be getting 570 patches in total, however I end up with less patches (542). Any help as to what I might be missing? Link to image above...

import numpy as np
from matplotlib import pyplot as plt
from patchify import patchify
import tifffile as tiff
import cv2

img = cv2.imread("D:/patchify tests/satellite image/crop_.tif")
patches_img = patchify(img, (256,256,3), step=256)

for i in range(patches_img.shape[0]):
for j in range(patches_img.shape[1]):
single_patch_img = patches_img[i, j, 0, :, :]
if not cv2.imwrite('C:/Users/Downloads/patches/' + 'image_' +
str(i)+str(j)+'.png', single_patch_img):
raise Exception("Could not write the image")

How to calculate step if > 1

Is there a way to calculate step which does not throw error and greater than 1
For example, I have 3D image of shape (512, 512, 110), I want to create multiple patches of shape (128,128,10), I don't know what should be the step size? Can I calculate it?
This line in readme is not clear to me
(width - patch_width) mod step_size = 0

Great job!

@dovahcrow

You have created simple library that does one thing and does it great. Thanks for that!

Python 3.9 support

Are there plans to support Python 3.9?

I am not able to install patchify with Python 3.9. One problem seems to be a too old scikit-image version which is required by patchify. How can I help you on that?

For 3d image patch, when passing more than 3 channels the documentation for 'patch size' is incorrect

I wanted to pass a 12 channel/band image with shape [256,256,12], where 256, 256 are size of the image and 12 are the channel/band to create patches of dimension 128,128,12.

Inorder to patchify, the documentation mentions to pass the patch size as : 128,128,12
patches = patchify(image, (2,2,3), step=1) # patch shape [2,2,3]

However, this only works in case of 3 channel/band data and not for more than 3 channel/band data. I have tried and tested this senario.
For more than 3 channel/band data as in this case, the image should be passed with dimensions: [12, 256, 256] and the patch size dimensions should be [12,128,128] instead of [128,128,12].

image
Please review this to the documentation. Thanks.

The unpatchify need a very small step...

Hello, I have a problem when i useing patchify.

In my task, i want divide different size of images to (256, 256), and then input them to a (Deep learning generation) model. Last, I want unpatchify them. This is my code:

def choose_step(num1, num2):
    """Choose the max step, to acclerate the algorithm.
     求长和宽的公约数集合,满足小于256的最大公约数即为最大step。
    """
    num1 = num1 - 256
    num2 = num2 - 256
    cd1 = [a for a in range(1, num1+ 1) if num1 % a==0]
    cd2 = [a for a in range(1, num2+ 1) if num2 % a==0]
    cd = list(set(cd1) & set(cd2))
    cd = sorted(cd, reverse=True)
    for i in range(len(cd)):
        if cd[i] < 256:
            return cd[i]
        else:
            continue
    print("The max cd is 1!")
    return 1

for id in range(len(im_list)):
    # im = Image.open(im_list[id]).convert('RGB')
    im = mpimg.imread(im_list[id])
    im = np.array(im).swapaxes(0, 1)
    f_name = os.path.basename(im_list[id])
    max_step = choose_step(im.shape[0], im.shape[1])
    print(im.shape, max_step)
    im_patches = patchify(im, patch_size=(256, 256, 3), step=max_step)    
    trans = transforms.Compose(transforms_)
    for i in tqdm(range(im_patches.shape[0])):
        for j in range(im_patches.shape[1]):
            im_patch = im_patches[i][j][0]
            im_patch = trans(Image.fromarray((im_patch)))
            im_patch = im_patch.unsqueeze(0)
            im_patch = generator(im_patch.cuda())
            im_patch = im_patch.squeeze(0).cpu().detach().numpy()
            im_patch = im_patch.swapaxes(0, 2).swapaxes(0, 1)
            im_patch = (im_patch + 1) * 128
            im_patch = im_patch.astype('uint8')
            im_patches[i][j][0] = im_patch   
    out = unpatchify(im_patches, imsize=(im.shape))
    print(out.min(), out.max())
    out = out.swapaxes(0, 1)
    mpimg.imsave(output_path + "/" + f_name, out)
    print("已保存第{}张测试图片".format(id))

But, i found most of the images need a very small step, which causes the algorithm to run very slowly. There will be a lot of extra image parts that need to be synthesized. I think there may be a problem with the algorithm I chose step, but according to the requirements in your document, I can't think of other algorithm implementation methods.

3D volumes

Can this be used for 3D volumes? Or is it limited to 2D images?

Allow unpatchify with nonuniform step size

skimage will have the step size of the last patch reduced if it is not possible to create patches with a uniform step size. unpatchify should relax the uniform step size assumption. related #5.

images of same shape do not always return same number of patches

I'm running the latest version of patchify 0.2.3.
I'm creating patches for a dataset with tifs and masks each of which have the same shape (I run a check).

def make_patches(path, channels):
  flag = 1 if channels == 3 else 0
  image = cv2.imread(path, flag) 
  SIZE_X = (image.shape[1]//patch_size)*patch_size 
  SIZE_Y = (image.shape[0]//patch_size)*patch_size
  image = image[:SIZE_Y, :SIZE_X]       

  #Extract patches from each image
  if channels == 3:
    patches_img = patchify(image, (patch_size, patch_size, channels), step=patch_size) 
    patches_img = np.reshape(patches_img, (-1, patch_size, patch_size, channels))
  else:
    patches_img = patchify(image, (patch_size, patch_size), step=patch_size)  
    patches_img = np.reshape(patches_img, (-1, patch_size, patch_size, 1))
  return patches_img

the pngs are single channel.

Why would patchify create a different number of patches for images of the same dimensions (width, height)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.