Skip to content

Instantly share code, notes, and snippets.

@ucalyptus2
Created June 28, 2020 05:46
Show Gist options
  • Save ucalyptus2/64172b3d008c353805a1ab447e617463 to your computer and use it in GitHub Desktop.
Save ucalyptus2/64172b3d008c353805a1ab447e617463 to your computer and use it in GitHub Desktop.
import os
import numpy as np
def pad(img, crop_size):
h, w, c = img.shape
n_h = int(h/crop_size)
n_w = int(w/crop_size)
w_toadd = (n_w+1) * crop_size - w
h_toadd = (n_h+1) * crop_size - h
img_pad = np.pad(img, [(0, h_toadd), (0, w_toadd), (0,0)], mode='constant')
return img_pad
def crop(img, crop_size, stride):
cropped_images = []
h, w, c = img.shape
n_h = int(h/stride)
n_w = int(w/stride)
for i in range(n_h):
for j in range(n_w):
crop_img = img[(i * stride):((i * stride) + crop_size), (j * stride):((j * stride) + crop_size), :]
if (crop_img.shape) == (crop_size, crop_size, c):
cropped_images.append(crop_img)
return cropped_images
def uncrop(shape, crops, crop_size, stride):
img = np.zeros(shape)
h, w, c = shape
n_h = int(h/stride)
n_w = int(w/stride)
for i in range(n_h):
for j in range(n_w):
img[(i * stride):((i * stride) + crop_size), (j * stride):((j * stride) + crop_size), :] = crops[i * n_w + j]
return img
def unpad(shape, img):
h, w, c = shape
return img[:h, :w, :]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment