dovahcrow / patchify.py Goto Github PK
View Code? Open in Web Editor NEWA library that helps you split image into small, overlappable patches, and merge patches into original image.
License: MIT License
A library that helps you split image into small, overlappable patches, and merge patches into original image.
License: MIT License
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?
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)
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.
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?
How do the unpatchify works for overlapping patches?
Does it average the values in overlapped area or it takes the value of the next patch as it is?
Thanks.
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")
Is there anyway we can preserve patched tiff files meta data while pachifying or the extent data?
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")
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
You have created simple library that does one thing and does it great. Thanks for that!
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?
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].
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.
Can this be used for 3D volumes? Or is it limited to 2D images?
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.
Not working when I tried with real (and "big" image).
Colab here
Thanks.
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)?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.