|
|
|
"""! @file main.py
|
|
|
|
@brief Main file for the application
|
|
|
|
@author xlanro00
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Import basic libraries
|
|
|
|
import argparse as ap
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
#from datetime import datetime
|
|
|
|
|
|
|
|
# Libraries for image processing
|
|
|
|
import numpy as np
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
#from PIL import Image
|
|
|
|
import cv2 as cv
|
|
|
|
|
|
|
|
# Import custom image filter library
|
|
|
|
import filters as flt
|
|
|
|
|
|
|
|
|
|
|
|
class apply_filters:
|
|
|
|
def __init__(self):
|
|
|
|
# Parse arguments from command line
|
|
|
|
self.parse_arguments()
|
|
|
|
self.input_file = self.args.input_file
|
|
|
|
self.output_file = self.args.output_file
|
|
|
|
self.dpi = self.args.dpi
|
|
|
|
self.filters = self.args.filters
|
|
|
|
self.mirror = self.args.mirror if self.args.mirror else 0
|
|
|
|
|
|
|
|
# Parse configuration from json file
|
|
|
|
if self.args.config:
|
|
|
|
self.config_file = self.args.config[0]
|
|
|
|
self.preset_name = self.args.config[1]
|
|
|
|
self.config = json.load(open(self.config_file))
|
|
|
|
self.parse_conf()
|
|
|
|
|
|
|
|
# If no config file given, expect filters in command line
|
|
|
|
else:
|
|
|
|
self.filters = self.args.filters
|
|
|
|
|
|
|
|
#read as numpy.array
|
|
|
|
self.img = plt.imread(self.input_file)
|
|
|
|
|
|
|
|
self.width = self.img.shape[1]
|
|
|
|
self.height = self.img.shape[0]
|
|
|
|
print(self.width, self.height)
|
|
|
|
|
|
|
|
fig = plt.figure(figsize = (self.width, self.height),
|
|
|
|
frameon = False, dpi = self.dpi / 100) # dpi is in cm
|
|
|
|
|
|
|
|
ax = plt.Axes(fig, [0., 0., 1., 1.])
|
|
|
|
ax.set_axis_off()
|
|
|
|
fig.add_axes(ax)
|
|
|
|
|
|
|
|
if self.mirror:
|
|
|
|
self.mirror_image()
|
|
|
|
|
|
|
|
# Apply all filters
|
|
|
|
self.apply_filter(fig, ax)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_conf(self):
|
|
|
|
# Parse configuration file if given.
|
|
|
|
try:
|
|
|
|
self.filters = self.config[self.preset_name]
|
|
|
|
except(KeyError):
|
|
|
|
print("Preset not found", file = sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_arguments(self):
|
|
|
|
|
|
|
|
# Parse arguments
|
|
|
|
parser = ap.ArgumentParser(prog = 'main.py', description =
|
|
|
|
'Program for processing a 2D image into 3D fingerprint.')
|
|
|
|
|
|
|
|
# positional arguments
|
|
|
|
parser.add_argument("input_file", type = str, help = "Location with input file")
|
|
|
|
parser.add_argument("output_file", type = str, help = "Output file location")
|
|
|
|
parser.add_argument("dpi", type = int, help = "Scanner dpi")
|
|
|
|
|
|
|
|
# boolean switch
|
|
|
|
parser.add_argument('-m', "--mirror", help = "Mirror input image",
|
|
|
|
type = bool, action = ap.BooleanOptionalAction)
|
|
|
|
|
|
|
|
# file with configuration containing presets, new preset name
|
|
|
|
# pair argument - give both or none
|
|
|
|
parser.add_argument('--config', nargs=2, metavar=('config_file', 'preset'),
|
|
|
|
help='Config file with presets, name of the preset')
|
|
|
|
|
|
|
|
# array of unknown length, all filter names saved inside
|
|
|
|
parser.add_argument('filters', type = str, nargs = '*', help = "List of filter names")
|
|
|
|
|
|
|
|
self.args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def filter_factory(self, filter_name):
|
|
|
|
# selects filter method of filters library
|
|
|
|
# better this than a 100 if/else
|
|
|
|
print("Applying " + filter_name + " filter", file = sys.stderr)
|
|
|
|
return getattr(flt, filter_name)
|
|
|
|
|
|
|
|
|
|
|
|
def resize_image(self):
|
|
|
|
|
|
|
|
# open image as python image object
|
|
|
|
print("Resize image", file = sys.stderr)
|
|
|
|
|
|
|
|
self.convert_dpi()
|
|
|
|
self.img = self.img.resize((np.array(self.width, self.height)).astype(int))
|
|
|
|
|
|
|
|
|
|
|
|
def mirror_image(self):
|
|
|
|
|
|
|
|
# mirror image when mirroring is needed
|
|
|
|
# should be used only if we want a positive form
|
|
|
|
print("Mirroring image", file=sys.stderr)
|
|
|
|
self.img = cv.flip(self.img, 1) # 1 for vertical mirror
|
|
|
|
|
|
|
|
|
|
|
|
def apply_filter(self, fig, ax):
|
|
|
|
|
|
|
|
if len(self.filters) == 0:
|
|
|
|
# No filter given, just save the image
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
for filter_name in self.filters:
|
|
|
|
filter = self.filter_factory(filter_name)
|
|
|
|
filter.apply(self)
|
|
|
|
self.save_image(fig, ax)
|
|
|
|
|
|
|
|
|
|
|
|
def print_size(self, size):
|
|
|
|
print("Width: " + str(size[0]), file = sys.stderr)
|
|
|
|
print("Height: " + str(size[1]), file = sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
def save_image(self, fig, ax):
|
|
|
|
# Save processed image.
|
|
|
|
print("Saving image", file = sys.stderr)
|
|
|
|
ax.imshow(self.img)
|
|
|
|
fig.savefig(fname=self.output_file)
|
|
|
|
|
|
|
|
|
|
|
|
app = apply_filters()
|