Skip to content

Commit

Permalink
[Steph] Updated imagewrapper to take parameters for crop
Browse files Browse the repository at this point in the history
  • Loading branch information
weberswords authored and aperez-tw committed Dec 20, 2017
1 parent 2948085 commit 2c4db5b
Showing 1 changed file with 48 additions and 26 deletions.
74 changes: 48 additions & 26 deletions data/imageWrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cv2
import math
import os
import random

import numpy as np
Expand All @@ -11,18 +12,41 @@ class ImageWrapper:
def __init__(self, image, root_directory):
self.image = cv2.imread(image,1)
self.root_directory = root_directory
self.height, self.width, self.channels = image.shape
self.name = self.root_directory.split('/')[-1]
self.height, self.width, self.channels = self.image.shape
self.name = image.split('/')[-1]

def get_name_path(self, process):
name_list = self.name.split('.')
name_list.insert(1, process)
new_name = self.root_directory + '_'.join(name_list[:2]) + '.' + name_list[2]
process_directory = self.root_directory + "/" + process
new_name = process_directory + "/" + '_'.join(name_list[:2]) + '.' + name_list[2]
if not os.path.exists(process_directory):
os.makedirs(process_directory)
return new_name

def crop(self):
print("Cropping image to 480x480...")
cropped_image = self.image[0:480, 120:600]
def get_crop_measurements(self, height, width):
if self.width > width:
width_difference = self.width - width
width_adjustment = width_difference/2
else:
width_adjustment = 0

if self.height > height:
height_difference = self.height - height
height_adjustment = height_difference/2
else:
height_adjustment = 0

return int(width_adjustment), int(height_adjustment)

def crop(self, height, width):
print("Cropping image to %dx%d..." % (height, width))
width_adjustment, height_adjustment = self.get_crop_measurements(height, width)
width_left = 0 + width_adjustment
width_right = self.width - width_adjustment
height_top = 0 + height_adjustment
height_bottom = self.height - height_adjustment
cropped_image = self.image[height_top:height_bottom, width_left:width_right]
return cropped_image

def should_activate(self):
Expand All @@ -37,42 +61,40 @@ def should_activate(self):

def flip(self):
print("Flipping...")
if self.should_activate():
flipped_image = cv2.flip(self.image, 1)
image_name = self.get_name_path('flip')
cv2.imwrite(image_name, flipped_image)
flipped_image = cv2.flip(self.image, 1)
image_name = self.get_name_path('flip')
cv2.imwrite(image_name, flipped_image)

def rotate(self):
def rotate(self, degrees_rotation=90):
print("Rotating...")
rows, cols, channels = self.image.shape
if self.should_activate():
rotation_matrix = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 1)
rotated_image = cv2.warpAffine(self.image, rotation_matrix, (cols, rows))
image_name = self.get_name_path('rotate')
cv2.imwrite(image_name, rotated_image)
rotation_matrix = cv2.getRotationMatrix2D((cols/2, rows/2), degrees_rotation, 1)
rotated_image = cv2.warpAffine(self.image, rotation_matrix, (cols, rows))
image_name = self.get_name_path('rotate')
print(image_name)
cv2.imwrite(image_name, rotated_image)

def add_noise(self):
print("Adding noise...")
rgb_img = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
height, width, channels = rgb_img.shape
print("H: %d\tW: %d\tC:%d" % (height, width, channels))
noise = np.random.randint(0,50, (height, width))
jitter = np.zeros_like(rgb_img)
jitter[:,:,1] = noise
image_name = self.get_name_path('noisy')
noisy_image = cv2.add(self.image, jitter)
combined = np.vstack((self.image[:math.ceil(height/2),:,:], noisy_image[math.ceil(height/2):,:,:]))
print(image_name)
cv2.imwrite(image_name,combined)

if self.should_activate(self):
image_name = self.get_name_path('noisy')
noisy_image = cv2.add(self.image, jitter)
combined = np.vstack((self.image[:math.ceil(height/2),:,:], noisy_image[math.ceil(height/2):,:,:]))
cv2.imwrite(image_name,combined)

def brighten(self):
def brighten(self, max_brighten=13):
print("Brightening...")
image_for_brightening = Image.fromarray(self.image)
enhancer = ImageEnhance.Brightness(image_for_brightening)
factor = random.randint(0, 13)/4.0
factor = random.randint(0, max_brighten)/4.0
brightened = enhancer.enhance(factor)
image_name = self.get_name_path(self, 'brighten')
image_name = self.get_name_path('brighten')
print(type(brightened))
cv2.imwrite(image_name,brightened)


Expand Down

0 comments on commit 2c4db5b

Please sign in to comment.