|
|
@ -15,6 +15,8 @@ import matplotlib.pyplot as plt
|
|
|
|
#from PIL import Image
|
|
|
|
#from PIL import Image
|
|
|
|
import cv2 as cv
|
|
|
|
import cv2 as cv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from stl import mesh
|
|
|
|
|
|
|
|
|
|
|
|
# Import custom image filter library
|
|
|
|
# Import custom image filter library
|
|
|
|
import filters as flt
|
|
|
|
import filters as flt
|
|
|
|
|
|
|
|
|
|
|
@ -36,14 +38,20 @@ class apply_filters:
|
|
|
|
|
|
|
|
|
|
|
|
# If no config file given, expect filters in command line
|
|
|
|
# If no config file given, expect filters in command line
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
|
|
|
|
if not self.args.filters:
|
|
|
|
|
|
|
|
print("No filters given, saving original image")
|
|
|
|
|
|
|
|
|
|
|
|
print("No config file given, using command line arguments")
|
|
|
|
print("No config file given, using command line arguments")
|
|
|
|
i = 0
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
|
|
|
|
for filter in self.args.filters:
|
|
|
|
for filter in self.args.filters:
|
|
|
|
if filter.find('=') == -1:
|
|
|
|
if filter.find('=') == -1:
|
|
|
|
|
|
|
|
# if no '=' in filter, it is a new filter
|
|
|
|
self.filters.append(filter)
|
|
|
|
self.filters.append(filter)
|
|
|
|
i += 1
|
|
|
|
i += 1
|
|
|
|
self.params[i] = {}
|
|
|
|
self.params[i] = {}
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
|
|
|
|
# else it's a parameter for current filter
|
|
|
|
key, value = filter.split('=')
|
|
|
|
key, value = filter.split('=')
|
|
|
|
self.params[i][key] = value
|
|
|
|
self.params[i][key] = value
|
|
|
|
self.parse_params(self.params[i])
|
|
|
|
self.parse_params(self.params[i])
|
|
|
@ -51,44 +59,44 @@ class apply_filters:
|
|
|
|
self.input_file = self.args.input_file
|
|
|
|
self.input_file = self.args.input_file
|
|
|
|
self.output_file = self.args.output_file
|
|
|
|
self.output_file = self.args.output_file
|
|
|
|
self.dpi = self.args.dpi
|
|
|
|
self.dpi = self.args.dpi
|
|
|
|
self.mirror = self.args.mirror if self.args.mirror else 0
|
|
|
|
self.run()
|
|
|
|
|
|
|
|
|
|
|
|
#read as numpy.array
|
|
|
|
def run(self):
|
|
|
|
|
|
|
|
# read as numpy.array
|
|
|
|
self.img = cv.imread(self.input_file, cv.IMREAD_GRAYSCALE)
|
|
|
|
self.img = cv.imread(self.input_file, cv.IMREAD_GRAYSCALE)
|
|
|
|
|
|
|
|
|
|
|
|
self.width = self.img.shape[1]
|
|
|
|
self.width = self.img.shape[1]
|
|
|
|
self.height = self.img.shape[0]
|
|
|
|
self.height = self.img.shape[0]
|
|
|
|
#print(self.width, self.height)
|
|
|
|
print(self.width, self.height)
|
|
|
|
|
|
|
|
|
|
|
|
fig = plt.figure(figsize = (self.width, self.height),
|
|
|
|
fig = plt.figure(figsize=(self.width, self.height),
|
|
|
|
frameon = False, dpi = self.dpi / 100) # dpi is in cm
|
|
|
|
frameon=False, dpi=self.dpi / 100) # dpi is in cm
|
|
|
|
|
|
|
|
|
|
|
|
ax = plt.Axes(fig, [0., 0., 1., 1.])
|
|
|
|
ax = plt.Axes(fig, [0., 0., 1., 1.])
|
|
|
|
ax.set_axis_off()
|
|
|
|
ax.set_axis_off()
|
|
|
|
fig.add_axes(ax)
|
|
|
|
fig.add_axes(ax)
|
|
|
|
|
|
|
|
|
|
|
|
if self.mirror:
|
|
|
|
if self.args.mirror:
|
|
|
|
self.mirror_image()
|
|
|
|
self.mirror_image()
|
|
|
|
|
|
|
|
|
|
|
|
# Apply all filters and save image
|
|
|
|
# Apply all filters and save image
|
|
|
|
self.apply_filter()
|
|
|
|
self.apply_filter()
|
|
|
|
self.save_image(fig, ax)
|
|
|
|
self.save_image(fig, ax)
|
|
|
|
|
|
|
|
plt.close()
|
|
|
|
|
|
|
|
if self.args.stl:
|
|
|
|
def check_param(self, params, key):
|
|
|
|
self.make_lithophane()
|
|
|
|
try:
|
|
|
|
|
|
|
|
params[key] = params[key]
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
|
|
|
|
params[key] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_params(self, params):
|
|
|
|
def parse_params(self, params):
|
|
|
|
possible_params = {"h", "searchWindowSize","templateWindowSize",
|
|
|
|
possible_params = {"h", "searchWindowSize", "templateWindowSize",
|
|
|
|
"ksize", "kernel", "sigmaX" , "sigmaY",
|
|
|
|
"ksize", "kernel", "sigmaX", "sigmaY",
|
|
|
|
"sigmaColor", "sigmaSpace", "d", "anchor", "iterations", "op"}
|
|
|
|
"sigmaColor", "sigmaSpace", "d", "anchor", "iterations",
|
|
|
|
|
|
|
|
"op", "strength"}
|
|
|
|
for key in possible_params:
|
|
|
|
for key in possible_params:
|
|
|
|
self.check_param(params, key)
|
|
|
|
try:
|
|
|
|
|
|
|
|
params[key] = params[key]
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
|
|
|
|
params[key] = None
|
|
|
|
|
|
|
|
|
|
|
|
def parse_conf(self):
|
|
|
|
def parse_conf(self):
|
|
|
|
# Parse configuration file if given.
|
|
|
|
# Parse configuration file if given.
|
|
|
@ -103,59 +111,67 @@ class apply_filters:
|
|
|
|
self.parse_params(self.params[i])
|
|
|
|
self.parse_params(self.params[i])
|
|
|
|
|
|
|
|
|
|
|
|
except(KeyError):
|
|
|
|
except(KeyError):
|
|
|
|
print("Preset not found", file = sys.stderr)
|
|
|
|
print("Preset not found", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_arguments(self):
|
|
|
|
def parse_arguments(self):
|
|
|
|
|
|
|
|
|
|
|
|
# Parse arguments
|
|
|
|
# Parse arguments
|
|
|
|
parser = ap.ArgumentParser(prog = 'main.py',
|
|
|
|
parser = ap.ArgumentParser(prog='main.py',
|
|
|
|
description = 'Program for processing a 2D image into 3D fingerprint.',
|
|
|
|
description='Program for processing a 2D image into 3D fingerprint.',
|
|
|
|
usage='%(prog)s [-h] [-m | --mirror | --no-mirror] input_file output_file dpi ([-c config_file preset | --config config_file preset] | [filters ...])')
|
|
|
|
usage='%(prog)s [-h] [-m | --mirror | --no-mirror] input_file output_file dpi ([-c config_file preset | --config config_file preset] | [filters ...])')
|
|
|
|
|
|
|
|
|
|
|
|
# positional arguments
|
|
|
|
# positional arguments
|
|
|
|
parser.add_argument("input_file", type = str, help = "location with input file")
|
|
|
|
parser.add_argument("input_file", type=str,
|
|
|
|
parser.add_argument("output_file", type = str, help = "output file location")
|
|
|
|
help="location with input file")
|
|
|
|
parser.add_argument("dpi", type = int, help = "scanner dpi")
|
|
|
|
parser.add_argument("output_file", type=str,
|
|
|
|
|
|
|
|
help="output file location")
|
|
|
|
|
|
|
|
parser.add_argument("dpi", type=int, help="scanner dpi")
|
|
|
|
|
|
|
|
|
|
|
|
# boolean switch
|
|
|
|
# boolean switch
|
|
|
|
parser.add_argument('-m', "--mirror", help = "mirror input image",
|
|
|
|
parser.add_argument('-m', "--mirror", help="mirror input image",
|
|
|
|
type = bool, action = ap.BooleanOptionalAction)
|
|
|
|
type=bool, action=ap.BooleanOptionalAction)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parser.add_argument('-s', '--stl', help="make stl model from processed image",
|
|
|
|
|
|
|
|
type=bool, action=ap.BooleanOptionalAction)
|
|
|
|
|
|
|
|
|
|
|
|
# file with configuration containing presets, new preset name
|
|
|
|
# file with configuration containing presets, new preset name
|
|
|
|
# pair argument - give both or none
|
|
|
|
# pair argument - give both or none
|
|
|
|
parser.add_argument('-c','--config', nargs = 2, metavar = ('config_file', 'preset'),
|
|
|
|
parser.add_argument('-c', '--config', nargs=2, metavar=('config_file', 'preset'),
|
|
|
|
help = 'pair: name of the config file with presets, name of the preset')
|
|
|
|
help='pair: name of the config file with presets, name of the preset')
|
|
|
|
|
|
|
|
|
|
|
|
# array of unknown length, all filter names saved inside
|
|
|
|
# array of unknown length, all filter names saved inside
|
|
|
|
parser.add_argument('filters', type = str, nargs = '*',
|
|
|
|
parser.add_argument('filters', type=str, nargs='*',
|
|
|
|
help="list of filter names and their parameters in form [filter_name1 param1=value1 param2=value2 filter_name2 param1=value1...]")
|
|
|
|
help="list of filter names and their parameters in form [filter_name1 param1=value1 param2=value2 filter_name2 param1=value1...]")
|
|
|
|
|
|
|
|
|
|
|
|
self.args = parser.parse_args()
|
|
|
|
self.args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def filter_factory(self, filter_name):
|
|
|
|
def filter_factory(self, filter_name):
|
|
|
|
# selects filter method of filters library
|
|
|
|
''' 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Applying " + filter_name + " filter", file=sys.stderr)
|
|
|
|
|
|
|
|
return getattr(flt, filter_name)
|
|
|
|
|
|
|
|
|
|
|
|
def resize_image(self):
|
|
|
|
def resize_image(self):
|
|
|
|
|
|
|
|
|
|
|
|
print("Resize image", file = sys.stderr)
|
|
|
|
print("Resize image", file=sys.stderr)
|
|
|
|
self.img = self.img.resize((np.array(self.width, self.height)).astype(int))
|
|
|
|
self.img = self.img.resize(
|
|
|
|
|
|
|
|
(np.array(self.width, self.height)).astype(int))
|
|
|
|
|
|
|
|
|
|
|
|
def mirror_image(self):
|
|
|
|
def mirror_image(self):
|
|
|
|
|
|
|
|
''' Mirror image when mirroring is needed,
|
|
|
|
|
|
|
|
should be used only if we want a positive model
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
# mirror image when mirroring is needed
|
|
|
|
#TODO make this automatic for positive STL
|
|
|
|
# should be used only if we want a positive form
|
|
|
|
|
|
|
|
print("Mirroring image", file=sys.stderr)
|
|
|
|
print("Mirroring image", file=sys.stderr)
|
|
|
|
self.img = cv.flip(self.img, 1) # 1 for vertical mirror
|
|
|
|
self.img = cv.flip(self.img, 1) # 1 for vertical mirror
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_filter(self):
|
|
|
|
def apply_filter(self):
|
|
|
|
|
|
|
|
''' Apply filters to image.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Applies the filters one by one, if no filters were given, just save original image output.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
if len(self.filters) == 0:
|
|
|
|
if len(self.filters) == 0:
|
|
|
|
# No filter given, just save the image
|
|
|
|
# No filter given, just save the image
|
|
|
@ -166,17 +182,21 @@ class apply_filters:
|
|
|
|
filter = self.filter_factory(filter_name)
|
|
|
|
filter = self.filter_factory(filter_name)
|
|
|
|
filter.apply(self, self.params[i+1])
|
|
|
|
filter.apply(self, self.params[i+1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print_size(self, size):
|
|
|
|
def print_size(self, size):
|
|
|
|
print("Width: " + str(size[0]), file = sys.stderr)
|
|
|
|
print("Width: " + str(size[0]), file=sys.stderr)
|
|
|
|
print("Height: " + str(size[1]), file = sys.stderr)
|
|
|
|
print("Height: " + str(size[1]), file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_image(self, fig, ax):
|
|
|
|
def save_image(self, fig, ax):
|
|
|
|
# Save processed image.
|
|
|
|
''' Save processed image.
|
|
|
|
print("Saving image", file = sys.stderr)
|
|
|
|
Colormap set to grayscale to avoid color mismatch.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Saving image", file=sys.stderr)
|
|
|
|
ax.imshow(self.img, cmap="gray")
|
|
|
|
ax.imshow(self.img, cmap="gray")
|
|
|
|
fig.savefig(fname=self.output_file)
|
|
|
|
fig.savefig(fname=self.output_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_lithophane(self):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = apply_filters()
|
|
|
|
image = apply_filters()
|
|
|
|