|
|
@ -1,13 +1,12 @@
|
|
|
|
"""! @file main.py
|
|
|
|
"""! @file main.py
|
|
|
|
@brief Main file for the application
|
|
|
|
@brief Main file for the application
|
|
|
|
@author xlanro00
|
|
|
|
@author xlanro00
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
# Import basic libraries
|
|
|
|
# Import basic libraries
|
|
|
|
import argparse as ap
|
|
|
|
import argparse as ap
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
#from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Libraries for image processing
|
|
|
|
# Libraries for image processing
|
|
|
|
import numpy as np
|
|
|
|
import numpy as np
|
|
|
@ -33,7 +32,6 @@ class apply_filters:
|
|
|
|
self.config_file = self.args.config[0]
|
|
|
|
self.config_file = self.args.config[0]
|
|
|
|
self.preset_name = self.args.config[1]
|
|
|
|
self.preset_name = self.args.config[1]
|
|
|
|
self.config = json.load(open(self.config_file))
|
|
|
|
self.config = json.load(open(self.config_file))
|
|
|
|
print("Config loaded")
|
|
|
|
|
|
|
|
self.parse_conf()
|
|
|
|
self.parse_conf()
|
|
|
|
|
|
|
|
|
|
|
|
# If no config file given, expect filters in command line
|
|
|
|
# If no config file given, expect filters in command line
|
|
|
@ -63,11 +61,12 @@ class apply_filters:
|
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
def run(self):
|
|
|
|
# read as numpy.array
|
|
|
|
# read as numpy.array
|
|
|
|
self.img = cv.imread(self.input_file, cv.IMREAD_GRAYSCALE)
|
|
|
|
self.img = cv.imread(
|
|
|
|
|
|
|
|
self.input_file, cv.IMREAD_GRAYSCALE).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
self.print_size(self.img.shape)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
@ -86,21 +85,27 @@ class apply_filters:
|
|
|
|
if self.args.stl:
|
|
|
|
if self.args.stl:
|
|
|
|
self.make_lithophane()
|
|
|
|
self.make_lithophane()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_params(self, params):
|
|
|
|
def parse_params(self, params):
|
|
|
|
|
|
|
|
''' Parse parameters of filters.
|
|
|
|
|
|
|
|
Set to None if not given.
|
|
|
|
|
|
|
|
They are later set in the filter method.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
possible_params = {"h", "searchWindowSize", "templateWindowSize",
|
|
|
|
possible_params = {"h", "searchWindowSize", "templateWindowSize",
|
|
|
|
"ksize", "kernel", "sigmaX", "sigmaY",
|
|
|
|
"ksize", "kernel", "sigmaX", "sigmaY",
|
|
|
|
"sigmaColor", "sigmaSpace", "d", "anchor", "iterations",
|
|
|
|
"sigmaColor", "sigmaSpace", "d", "anchor", "iterations",
|
|
|
|
"op", "strength"}
|
|
|
|
"op", "strength", "amount", "radius", "weight", "channelAxis"}
|
|
|
|
for key in possible_params:
|
|
|
|
for key in possible_params:
|
|
|
|
try:
|
|
|
|
if params.get(key) is None:
|
|
|
|
params[key] = params[key]
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
|
|
|
|
params[key] = None
|
|
|
|
params[key] = None
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
params[key] = params[key]
|
|
|
|
|
|
|
|
|
|
|
|
def parse_conf(self):
|
|
|
|
def parse_conf(self):
|
|
|
|
# Parse configuration file if given.
|
|
|
|
''' Parse configuration file if one was given and store filters with their parameters
|
|
|
|
try:
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.preset_name in self.config:
|
|
|
|
filter_array = self.config[self.preset_name]
|
|
|
|
filter_array = self.config[self.preset_name]
|
|
|
|
for i, filter in enumerate(range(len(filter_array)), start=1):
|
|
|
|
for i, filter in enumerate(range(len(filter_array)), start=1):
|
|
|
|
self.filters.append(filter_array[filter]["name"])
|
|
|
|
self.filters.append(filter_array[filter]["name"])
|
|
|
@ -109,13 +114,15 @@ class apply_filters:
|
|
|
|
if attribute != "name":
|
|
|
|
if attribute != "name":
|
|
|
|
self.params[i][attribute] = value
|
|
|
|
self.params[i][attribute] = value
|
|
|
|
self.parse_params(self.params[i])
|
|
|
|
self.parse_params(self.params[i])
|
|
|
|
|
|
|
|
print("Loaded preset: " + self.preset_name +
|
|
|
|
except(KeyError):
|
|
|
|
" from file: " + self.config_file)
|
|
|
|
|
|
|
|
else:
|
|
|
|
print("Preset not found", file=sys.stderr)
|
|
|
|
print("Preset not found", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_arguments(self):
|
|
|
|
def parse_arguments(self):
|
|
|
|
|
|
|
|
''' Parse arguments from command line
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
# 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 ...])')
|
|
|
@ -127,10 +134,11 @@ class apply_filters:
|
|
|
|
help="output file location")
|
|
|
|
help="output file location")
|
|
|
|
parser.add_argument("dpi", type=int, help="scanner dpi")
|
|
|
|
parser.add_argument("dpi", type=int, help="scanner dpi")
|
|
|
|
|
|
|
|
|
|
|
|
# boolean switch
|
|
|
|
# boolean switch argument
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# another boolean switch argument
|
|
|
|
parser.add_argument('-s', '--stl', help="make stl model from processed image",
|
|
|
|
parser.add_argument('-s', '--stl', help="make stl model from processed image",
|
|
|
|
type=bool, action=ap.BooleanOptionalAction)
|
|
|
|
type=bool, action=ap.BooleanOptionalAction)
|
|
|
|
|
|
|
|
|
|
|
@ -149,7 +157,7 @@ class apply_filters:
|
|
|
|
''' Selects filter method of filters library.
|
|
|
|
''' Selects filter method of filters library.
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
print("Applying " + filter_name + " filter", file=sys.stderr)
|
|
|
|
print("Applying " + filter_name + " filter ", end='')
|
|
|
|
return getattr(flt, filter_name)
|
|
|
|
return getattr(flt, filter_name)
|
|
|
|
|
|
|
|
|
|
|
|
def resize_image(self):
|
|
|
|
def resize_image(self):
|
|
|
@ -163,16 +171,16 @@ class apply_filters:
|
|
|
|
should be used only if we want a positive model
|
|
|
|
should be used only if we want a positive model
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
#TODO make this automatic for positive STL
|
|
|
|
# TODO make this automatic for positive STL
|
|
|
|
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.
|
|
|
|
''' Apply filters to image.
|
|
|
|
|
|
|
|
|
|
|
|
Applies the filters one by one, if no filters were given, just save original image output.
|
|
|
|
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
|
|
|
|
pass
|
|
|
|
pass
|
|
|
@ -180,11 +188,13 @@ class apply_filters:
|
|
|
|
# Apply all filters
|
|
|
|
# Apply all filters
|
|
|
|
for i, filter_name in enumerate(self.filters):
|
|
|
|
for i, filter_name in enumerate(self.filters):
|
|
|
|
filter = self.filter_factory(filter_name)
|
|
|
|
filter = self.filter_factory(filter_name)
|
|
|
|
|
|
|
|
# print(self.img.dtype)
|
|
|
|
filter.apply(self, self.params[i+1])
|
|
|
|
filter.apply(self, self.params[i+1])
|
|
|
|
|
|
|
|
# print(self.img.dtype)
|
|
|
|
|
|
|
|
|
|
|
|
def print_size(self, size):
|
|
|
|
def print_size(self, size):
|
|
|
|
print("Width: " + str(size[0]), file=sys.stderr)
|
|
|
|
print("Height: " + str(size[0]), file=sys.stderr)
|
|
|
|
print("Height: " + str(size[1]), file=sys.stderr)
|
|
|
|
print("Width: " + str(size[1]), file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
def save_image(self, fig, ax):
|
|
|
|
def save_image(self, fig, ax):
|
|
|
|
''' Save processed image.
|
|
|
|
''' Save processed image.
|
|
|
@ -196,7 +206,6 @@ class apply_filters:
|
|
|
|
fig.savefig(fname=self.output_file)
|
|
|
|
fig.savefig(fname=self.output_file)
|
|
|
|
|
|
|
|
|
|
|
|
def make_lithophane(self):
|
|
|
|
def make_lithophane(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
'''After processing image, make a lithophane from it.
|
|
|
|
'''After processing image, make a lithophane from it.
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
@ -208,15 +217,21 @@ class apply_filters:
|
|
|
|
self.save_model()
|
|
|
|
self.save_model()
|
|
|
|
|
|
|
|
|
|
|
|
def make_meshgrid(self):
|
|
|
|
def make_meshgrid(self):
|
|
|
|
|
|
|
|
''' Create numpy meshgrid.
|
|
|
|
|
|
|
|
Modify image values to get more usable depth values.
|
|
|
|
|
|
|
|
Add zero padding to image to make sides of the plate.
|
|
|
|
|
|
|
|
'''
|
|
|
|
# Modify image to make it more suitable depth
|
|
|
|
# Modify image to make it more suitable depth
|
|
|
|
# values1 = (1 + (1 - self.img/255)/6) * 255/10 # this works
|
|
|
|
# values1 = (1 + (1 - self.img/255)/6) * 255/10 # this works
|
|
|
|
# values2 = (1 - (1 - self.img/255)/6) * 255/10 # TODO: i dont know how to make white surrounding be extruded
|
|
|
|
# values2 = (1 - (1 - self.img/255)/6) * 255/10 #
|
|
|
|
|
|
|
|
# TODO: i dont know how to make white surrounding be extruded
|
|
|
|
|
|
|
|
|
|
|
|
values1better = 28.05 - 0.01*self.img
|
|
|
|
values1better = 28.05 - 0.01*self.img
|
|
|
|
#values2better = 22.95 - 0.01*self.img
|
|
|
|
#values2better = 22.95 - 0.01*self.img
|
|
|
|
# (np.around(values2[::300],3))
|
|
|
|
# (np.around(values2[::300],3))
|
|
|
|
|
|
|
|
|
|
|
|
# Add zero padding to image to make sides of the plate
|
|
|
|
# Add zero padding to image
|
|
|
|
|
|
|
|
# TODO this better be done in the next function to keep dimensions intact
|
|
|
|
self.height = self.img.shape[0] + 2
|
|
|
|
self.height = self.img.shape[0] + 2
|
|
|
|
self.width = self.img.shape[1] + 2
|
|
|
|
self.width = self.img.shape[1] + 2
|
|
|
|
self.img = np.zeros([self.height, self.width])
|
|
|
|
self.img = np.zeros([self.height, self.width])
|
|
|
@ -228,6 +243,11 @@ class apply_filters:
|
|
|
|
self.meshgrid = np.meshgrid(verticesX, verticesY)
|
|
|
|
self.meshgrid = np.meshgrid(verticesX, verticesY)
|
|
|
|
|
|
|
|
|
|
|
|
def make_mesh(self):
|
|
|
|
def make_mesh(self):
|
|
|
|
|
|
|
|
''' Create mesh from image.
|
|
|
|
|
|
|
|
Create vertices from meshgrid, add depth values from image.
|
|
|
|
|
|
|
|
Create faces from vertices.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
# Convert meshgrid and image matrix to array of 3D points
|
|
|
|
# Convert meshgrid and image matrix to array of 3D points
|
|
|
|
vertice_arr = np.vstack(list(map(np.ravel, self.meshgrid))).T
|
|
|
|
vertice_arr = np.vstack(list(map(np.ravel, self.meshgrid))).T
|
|
|
|
z = (self.img / 10).reshape(-1, 1)
|
|
|
|
z = (self.img / 10).reshape(-1, 1)
|
|
|
@ -278,7 +298,10 @@ class apply_filters:
|
|
|
|
self.model.vectors[i][j] = vertices[face[j], :]
|
|
|
|
self.model.vectors[i][j] = vertices[face[j], :]
|
|
|
|
|
|
|
|
|
|
|
|
def save_model(self):
|
|
|
|
def save_model(self):
|
|
|
|
print("Saving stl model", file=sys.stderr)
|
|
|
|
''' Save final model to stl file.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Saving lithophane to stl file", file=sys.stderr)
|
|
|
|
self.model.save('res/test.stl')
|
|
|
|
self.model.save('res/test.stl')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|