Skip to content

Instantly share code, notes, and snippets.

@humandotlearning
Last active September 19, 2019 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save humandotlearning/7c06c0bf0a7d232436300dd01d52fc1c to your computer and use it in GitHub Desktop.
Save humandotlearning/7c06c0bf0a7d232436300dd01d52fc1c to your computer and use it in GitHub Desktop.
multiple image plot and some image augmentations
import numpy as np
import matplotlib.pyplot as plt
#utility fn. for plots
def ceildiv(a, b):
return -(-a // b)
#fn. to plot multiple images
def plots(ims, figsize=(12,6), rows=1, interp=False, titles=None, maintitle=None):
if type(ims[0]) is np.ndarray:
ims = np.array(ims)
if (ims.shape[-1] != 3): ims = ims.transpose((0,2,3,1))
f = plt.figure(figsize=figsize)
if maintitle is not None:
plt.suptitle(maintitle, fontsize=16)
for i in range(len(ims)):
sp = f.add_subplot(rows, ceildiv(len(ims), rows), i+1)
sp.axis('Off')
if titles is not None: sp.set_title(titles[i], fontsize=16)
plt.imshow(ims[i], interpolation=None if interp else 'none')
def crop(im, r, c, szx , szy):
return im[r:r+szx, c:c+szy]
#fn. to crop images randomly
def rm_crop( x , percent, crp_pts=False):
r,c,_ = x.shape
targx= int(x.shape[0] * percent)
targy= int(x.shape[1] * percent)
start_r = random.randint(0, r-targx)
start_c = random.randint(0, c-targy)
res = crop(x, start_r, start_c, targx, targy)
if crp_pts==False:
return res
elif crp_pts==True:
return (res, start_r,start_c, targx, targy)
#random lighting
def rand_light(image):
image1 = cv2.cvtColor(image,cv2.COLOR_RGB2HSV)
random_bright = .3+np.random.uniform()
#print(random_bright)
image1[:,:,2] = image1[:,:,2]*random_bright
image1 = cv2.cvtColor(image1,cv2.COLOR_HSV2RGB)
return image
#fn. to rotate
def rotate(img=img, angle=36):
rotated=[]
for i in np.arange(0, 360, angle):
rotated.append(imutils.rotate(img.copy(), i))
return rotated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment