Last active
September 19, 2019 10:54
-
-
Save humandotlearning/7c06c0bf0a7d232436300dd01d52fc1c to your computer and use it in GitHub Desktop.
multiple image plot and some image augmentations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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