Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
# Dependencies: opencv-python, matplotlib, numpy, tensorflow, keras
import cv2
from matplotlib import pyplot as plt
import os
import numpy as np
from collections import namedtuple
import sys
import tensorflow as tf
from keras.models import model_from_json
def processImage(input_img, bitmask):
'''
Applies the bitmasking and cropping procedure to the given image, using
the given bitmask
Parameters
----------
input_img : MxNx3 uint8 array
Original input image
bitmask : MxN boolean array
Bitmask to be applied to the input image
Returns
-------
processedImage : JxKx3 uint8 array
The original image after bitmasking and cropping has been applied
'''
# Apply the bitmask
bitmaskedImage = applyBitmask(input_img, bitmask)
# Crop the image
processedImage = cropImage(bitmaskedImage)
return processedImage
def loadBitmaskImage(fullFilename):
'''
Loads an image file (JPG) of the bitmask, and converts it to a
boolean array
Parameters
----------
fullFilename : String
Full filepath of the bitmask image file
Returns
-------
bitmask : MxN boolean array
The bitmask matrix computed from the input image
'''
# Load in raw bitmask image (JPG)
raw_img = cv2.imread(fullFilename)
# Flatten the RGB channels into one channel with the summing operation
# Resultant matrix of type uint32
flat_img = np.sum(raw_img, axis=2)
# Threshold integer matrix into bit matrix (of type bool)
thld = 400
bitmask = flat_img > thld
return bitmask
def applyBitmask(image, bitmask):
'''
Applies the given bitmask to the given RGB image
Parameters
----------
image : MxNx3 uint8 array
Input image to be bitmasked
bitmask : MxN bool array
Bitmask to apply to input image
Returns
-------
masked_img : MxNx3 uint8 array
The input image after the bitmasking procedure
'''
# Apply the bitmask to the image
masked_img = np.dstack((
np.multiply(image[:,:,0], bitmask),
np.multiply(image[:,:,1], bitmask),
np.multiply(image[:,:,2], bitmask)))
return masked_img
def cropImage(input_img):
'''
Crops the given image to the minimum bounding rectangle to minimize black
background in image.
Parameters
----------
input_img : MxNx3 uint8 array
Input image to be cropped, post-bitmasking
Returns
-------
output_img : JxKx3 uint8 array
The input image after the cropping procedure
'''
# Flatten colors of image
flat_img = np.sum(input_img, axis=2)
# Flatten Columns
flat_cols = np.sum(flat_img, axis=0)
# Flatten Rows
flat_rows = np.sum(flat_img, axis=1)
# Find first and last non-zero rows and columns
idx_left = np.min(np.nonzero(flat_cols))
idx_right = np.max(np.nonzero(flat_cols))
idx_bottom = np.min(np.nonzero(flat_rows))
idx_top = np.max(np.nonzero(flat_rows))
# Crop out all zero-rows and zero-columns
output_img = input_img[idx_bottom:idx_top+1,idx_left:idx_right+1, :]
return output_img
def loadImage(fullFilename):
'''
Loads the image from the given filename. Reads the EXIF orientation data
and rotates the image to nominal (horizontal) position before returning
Parameters
----------
fullFilename : String
The full filepath and filename to the image to be loaded
Returns
-------
img: MxNx3 uint8
BGR image data matrix, loaded with opencv
'''
# Load the image file as binary first to extract EXIF data:
imfile = open(fullFilename, 'rb')
# Get EXIF Tags
# TODO this call seems to be generating some error messages
tags = exifread.process_file(imfile)
# Find the orientation of the image
try :
orientation = str(tags['Image Orientation'])
except:
orientation = 'Horizontal (normal)'
## Load the image into an array:
raw_img = cv2.imread(fullFilename)
# Rotate the image back to horizontal, if necessary:
if(orientation.startswith('Horizontal (normal)')):
img = raw_img
elif(orientation.startswith('Rotated 90 CW')):
img = cv2.rotate(raw_img,rotateCode=cv2.ROTATE_90_COUNTERCLOCKWISE)
elif(orientation.startswith('Rotated 90 CCW')):
img = cv2.rotate(raw_img,rotateCode=cv2.ROTATE_90_CLOCKWISE)
else:
img = cv2.rotate(raw_img,rotateCode=cv2.ROTATE_180)
return img
def imshow(image):
'''
IMSHOW: Plots the given image in a MATPLOTLIB window.
Parameters
----------
image : MxNx3 uint8
BGR image data loaded with opencv.
Returns
-------
None.
'''
fig = plt.figure()
plt.axis("off")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
def loadNetwork(model_filepath, weights_filepath):
# Load Model From File
model_file = open(model_filepath, 'r')
model_json = model_file.read()
model_file.close()
model = model_from_json(model_json)
# Import Weights from File
model.load_weights(weights_filepath)
return model
if __name__=="__main__":
test()