|
|
|
@ -7,7 +7,7 @@
|
|
|
|
|
import argparse as ap
|
|
|
|
|
import sys
|
|
|
|
|
import json
|
|
|
|
|
import math
|
|
|
|
|
import math
|
|
|
|
|
from os.path import exists
|
|
|
|
|
|
|
|
|
|
# Libraries for image processing
|
|
|
|
@ -71,27 +71,29 @@ class app:
|
|
|
|
|
if self.args.stl_file:
|
|
|
|
|
|
|
|
|
|
# Get stl filename
|
|
|
|
|
self.stl_file = self.args.stl_file[0]
|
|
|
|
|
self.stl_path = self.output_file.rsplit('/', 1)[0] + '/'
|
|
|
|
|
|
|
|
|
|
# Get mode and model parameters
|
|
|
|
|
if self.args.planar:
|
|
|
|
|
self.mode = "2d"
|
|
|
|
|
|
|
|
|
|
if len(self.args.stl_file) < 3:
|
|
|
|
|
self.height_base = 10
|
|
|
|
|
self.mode = "planar"
|
|
|
|
|
|
|
|
|
|
# TODO: add default values for planar mode, not like this
|
|
|
|
|
if len(self.args.stl_file) < 2:
|
|
|
|
|
self.height_line = 2
|
|
|
|
|
self.height_base = 10
|
|
|
|
|
print(
|
|
|
|
|
"Warning: Too few arguments, using default values (10mm base, 2mm lines)")
|
|
|
|
|
else:
|
|
|
|
|
self.height_line = float(self.args.stl_file[1])
|
|
|
|
|
self.height_base = float(self.args.stl_file[2])
|
|
|
|
|
self.height_line = float(self.args.stl_file[0])
|
|
|
|
|
self.height_base = float(self.args.stl_file[1])
|
|
|
|
|
print("Base height:", self.height_base,
|
|
|
|
|
"mm, lines depth/height:", self.height_line, "mm")
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
self.mode = "3d"
|
|
|
|
|
self.mode = "curved"
|
|
|
|
|
|
|
|
|
|
if len(self.args.stl_file) < 5:
|
|
|
|
|
# TODO: add default values for curved mode, not like this
|
|
|
|
|
if len(self.args.stl_file) < 4:
|
|
|
|
|
self.height_line = 2
|
|
|
|
|
self.height_base = 10
|
|
|
|
|
self.curv_rate_x = 0.5
|
|
|
|
@ -99,16 +101,16 @@ class app:
|
|
|
|
|
print(
|
|
|
|
|
"Warning: Too few arguments, using default values (2mm lines, curvature 0.5 on x, 0.5 on y)")
|
|
|
|
|
else:
|
|
|
|
|
self.height_line = float(self.args.stl_file[1])
|
|
|
|
|
self.height_base = float(self.args.stl_file[2])
|
|
|
|
|
self.height_line = float(self.args.stl_file[0])
|
|
|
|
|
self.height_base = float(self.args.stl_file[1])
|
|
|
|
|
self.curv_rate_x = float(
|
|
|
|
|
self.args.stl_file[3]) # finger depth
|
|
|
|
|
self.args.stl_file[2]) # finger depth
|
|
|
|
|
self.curv_rate_y = float(
|
|
|
|
|
self.args.stl_file[4]) # finger depth
|
|
|
|
|
self.args.stl_file[3]) # finger depth
|
|
|
|
|
print("Line height:", self.height_line, "mm, base height: ", self.height_base,
|
|
|
|
|
"mm, x axis curvature: ", self.curv_rate_x, ", y axis curvature:", self.curv_rate_y)
|
|
|
|
|
|
|
|
|
|
print(self.mode, "mode selected")
|
|
|
|
|
print("Stl generation in ", self.mode)
|
|
|
|
|
self.run_stl()
|
|
|
|
|
|
|
|
|
|
def parse_arguments(self):
|
|
|
|
@ -116,8 +118,8 @@ class app:
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
parser = ap.ArgumentParser(prog='main.py',
|
|
|
|
|
description='Program for processing a 2D image into 3D fingerprint.',
|
|
|
|
|
usage='%(prog)s [-h] [-m | --mirror | --no-mirror] [-p] input_file output_file dpi ([-c config_file preset | --config config_file preset] | [filters ...]) [-s stl_file | --stl stl_file height_line height_base | --stl_file stl_file height_line curv_rate_x curv_rate_y]')
|
|
|
|
|
description='Program for transforming a 2D image into 3D fingerprint.',
|
|
|
|
|
usage='%(prog)s [-h] [-m | --mirror | --no-mirror] [-p] input_file output_file dpi ([-c | --config config_file preset] | [filters ...]) [-s | --stl_file height_line height_base | --stl_file height_line curv_rate_x curv_rate_y]')
|
|
|
|
|
|
|
|
|
|
# positional arguments
|
|
|
|
|
parser.add_argument("input_file", type=str, help="input file path")
|
|
|
|
@ -132,7 +134,7 @@ class app:
|
|
|
|
|
parser.add_argument('-s', '--stl_file', type=str, nargs='*',
|
|
|
|
|
help="create stl model from processed image")
|
|
|
|
|
|
|
|
|
|
# another boolean switch argument, this enables 2d mode
|
|
|
|
|
# another boolean switch argument, this enables planar mode
|
|
|
|
|
parser.add_argument('-p', '--planar', type=bool, action=ap.BooleanOptionalAction,
|
|
|
|
|
help="make stl shape planar instead of curved one")
|
|
|
|
|
|
|
|
|
@ -157,7 +159,7 @@ class app:
|
|
|
|
|
"ksize", "kernel", "sigmaX", "sigmaY",
|
|
|
|
|
"sigmaColor", "sigmaSpace", "d", "anchor", "iterations",
|
|
|
|
|
"op", "strength", "amount", "radius", "weight", "channelAxis",
|
|
|
|
|
"theta", "sigma", "lambda", "gamma", "psi", "shape"}
|
|
|
|
|
"theta", "sigma", "lambda", "gamma", "psi", "shape", "percent", "threshold"}
|
|
|
|
|
|
|
|
|
|
for key in possible_params:
|
|
|
|
|
if params.get(key) is None:
|
|
|
|
@ -188,7 +190,7 @@ class app:
|
|
|
|
|
self.error_exit("Preset not found")
|
|
|
|
|
|
|
|
|
|
def error_exit(self, message):
|
|
|
|
|
'''Print error message and exit.
|
|
|
|
|
'''Print error message and exit the application.
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
print("ERROR:", message, file=sys.stderr)
|
|
|
|
@ -204,12 +206,12 @@ class app:
|
|
|
|
|
self.img = cv.imread(
|
|
|
|
|
self.input_file, cv.IMREAD_GRAYSCALE).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
self.height, self.width = self.img.shape
|
|
|
|
|
# gets empty figure and ax with dimensions of input image
|
|
|
|
|
self.height, self.width = self.img.shape
|
|
|
|
|
fig, ax = self.get_empty_figure()
|
|
|
|
|
|
|
|
|
|
print("Height: " + str(self.height) + " px and width: "
|
|
|
|
|
+ str(self.width) + " px", file=sys.stderr)
|
|
|
|
|
# print("Height: " + str(self.height) + " px and width: "
|
|
|
|
|
# + str(self.width) + " px", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
if self.mirror is True:
|
|
|
|
|
self.mirror_image()
|
|
|
|
@ -267,21 +269,27 @@ class app:
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
self.prepare_heightmap()
|
|
|
|
|
self.get_ID()
|
|
|
|
|
self.get_ID()
|
|
|
|
|
|
|
|
|
|
print("Creating mesh", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
# Create a mesh using one of two modes
|
|
|
|
|
if self.mode == "2d":
|
|
|
|
|
if self.mode == "planar":
|
|
|
|
|
self.make_stl_planar()
|
|
|
|
|
|
|
|
|
|
elif self.mode == "3d":
|
|
|
|
|
elif self.mode == "curved":
|
|
|
|
|
self.make_stl_curved()
|
|
|
|
|
|
|
|
|
|
elif self.mode == "mapped":
|
|
|
|
|
# TODO: find a suitable finger model, try to map the fingerprint onto it
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
self.error_exit("Mode not supported")
|
|
|
|
|
|
|
|
|
|
plt.show()
|
|
|
|
|
self.save_stl()
|
|
|
|
|
print(f"Saving model to ", self.stl_file, file=sys.stderr)
|
|
|
|
|
print(f"Saving model to ", self.stl_path, file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
def prepare_heightmap(self):
|
|
|
|
|
'''Modify image values to get usable height/depth values.
|
|
|
|
@ -289,30 +297,32 @@ class app:
|
|
|
|
|
Prepare meshgrid.
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
# TODO: redo, too complicated, add extra params, redo checks
|
|
|
|
|
|
|
|
|
|
if self.img.dtype == np.float32 or self.img.dtype == np.float64:
|
|
|
|
|
if self.img.dtype != np.uint8:
|
|
|
|
|
print("Converting to uint8", file=sys.stderr)
|
|
|
|
|
self.img = self.img * 255
|
|
|
|
|
self.img = self.img / np.max(self.img) * 255
|
|
|
|
|
self.img = self.img.astype(np.uint8)
|
|
|
|
|
print("Creating mesh", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
if self.mode == "2d":
|
|
|
|
|
if self.mode == "planar":
|
|
|
|
|
# just renamed it for easier use
|
|
|
|
|
height_base = self.height_base
|
|
|
|
|
|
|
|
|
|
if self.height_base <= 0:
|
|
|
|
|
self.error_exit("Depth of plate height must be positive")
|
|
|
|
|
|
|
|
|
|
if self.height_line + self.height_base <= 0:
|
|
|
|
|
self.error_exit("Line depth must be less than plate thickness")
|
|
|
|
|
|
|
|
|
|
# Transform image values to get a heightmap
|
|
|
|
|
self.img = (self.height_base + (1 - self.img/255)
|
|
|
|
|
* self.height_line)
|
|
|
|
|
if self.mode == "curved":
|
|
|
|
|
# still need this value later
|
|
|
|
|
height_base = 0
|
|
|
|
|
|
|
|
|
|
if self.mode == "3d":
|
|
|
|
|
# TODO check curvature values and print info
|
|
|
|
|
# TODO: copy pasta code, remove
|
|
|
|
|
# Transform image values to get a heightmap
|
|
|
|
|
self.img = (1 - self.img/255) * self.height_line
|
|
|
|
|
# Don't need to check curvature, check only heights
|
|
|
|
|
if self.height_base <= 0 or self.height_line <= 0:
|
|
|
|
|
self.error_exit("Base and line height must both be positive")
|
|
|
|
|
|
|
|
|
|
# Transform image values to get a heightmap
|
|
|
|
|
self.img = (height_base + (1 - self.img/255)
|
|
|
|
|
* self.height_line)
|
|
|
|
|
|
|
|
|
|
# This sets the size of stl model and number of subdivisions / triangles
|
|
|
|
|
x = np.linspace(0, self.width * 25.4 / self.dpi, self.width)
|
|
|
|
@ -324,10 +334,15 @@ class app:
|
|
|
|
|
'''Get unique ID for the model.
|
|
|
|
|
Consists of pair input_file + preset_name.
|
|
|
|
|
'''
|
|
|
|
|
# TODO: somehow compress this to fit it onto the model, maybe zlib
|
|
|
|
|
|
|
|
|
|
# TODO: somehow compress this to fit it onto the model
|
|
|
|
|
self.id = self.input_file.split(
|
|
|
|
|
"/")[-1].split(".")[0] + "_" + self.preset_name
|
|
|
|
|
print(self.id)
|
|
|
|
|
# TODO: hash is not unique, find a better way
|
|
|
|
|
# TODO: stl file format has 80 chars for header, use that space to store info
|
|
|
|
|
# python generates a random value for security reasons, it has to be turned off
|
|
|
|
|
self.id = str(hash(self.id))
|
|
|
|
|
#print(self.id)
|
|
|
|
|
|
|
|
|
|
def append_faces(self, faces, c):
|
|
|
|
|
# Function to add faces to the list
|
|
|
|
@ -346,7 +361,7 @@ class app:
|
|
|
|
|
ax.plot([0, 1], [0, 1], c="black", lw=self.width)
|
|
|
|
|
|
|
|
|
|
# extract filename
|
|
|
|
|
text = self.stl_file.split("/")[-1].split(".")[0] + self.id
|
|
|
|
|
text = self.stl_path.split("/")[-1].split(".")[0] + self.id
|
|
|
|
|
fontsize = 20
|
|
|
|
|
|
|
|
|
|
# create text object, paint it white
|
|
|
|
@ -371,14 +386,14 @@ class app:
|
|
|
|
|
plt.close()
|
|
|
|
|
|
|
|
|
|
# TODO: maybe don't use nested for loops, use numpy?
|
|
|
|
|
if self.mode == "2d":
|
|
|
|
|
if self.mode == "planar":
|
|
|
|
|
for i in range(self.height):
|
|
|
|
|
for j in range(self.width):
|
|
|
|
|
bottom_vert_arr[i][j][2] = data[i][j][0]
|
|
|
|
|
elif self.mode == "3d":
|
|
|
|
|
elif self.mode == "curved":
|
|
|
|
|
for i in range(self.height):
|
|
|
|
|
for j in range(self.width):
|
|
|
|
|
bottom_vert_arr[i][j][2] += data[i][j][0] - self.height_base
|
|
|
|
|
bottom_vert_arr[i][j][2] += data[i][j][0] - self.height_base/10
|
|
|
|
|
|
|
|
|
|
return bottom_vert_arr
|
|
|
|
|
|
|
|
|
@ -413,7 +428,7 @@ class app:
|
|
|
|
|
endloop
|
|
|
|
|
endfacet
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Add the image matrix to the 2D meshgrid and create 1D array of 3D points
|
|
|
|
|
top_vert_arr = np.vstack(list(map(np.ravel, self.meshgrid))).T
|
|
|
|
|
z = (self.img / 10).reshape(-1, 1)
|
|
|
|
@ -495,6 +510,8 @@ class app:
|
|
|
|
|
'''Map fingerprint to finger model.
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
# TODO: this might be done in a better way
|
|
|
|
|
# instead of summing up the values, use their product - 0 ?
|
|
|
|
|
z = np.array([])
|
|
|
|
|
for x in range(self.width):
|
|
|
|
|
z = np.append(z, np.sqrt(1 - (2*x/self.width - 1)**2)
|
|
|
|
@ -511,9 +528,11 @@ class app:
|
|
|
|
|
z += self.img
|
|
|
|
|
|
|
|
|
|
vert_arr_tmp = np.vstack(list(map(np.ravel, self.meshgrid))).T
|
|
|
|
|
|
|
|
|
|
# for top side
|
|
|
|
|
top_vert_arr = np.concatenate((vert_arr_tmp, z), axis=1)
|
|
|
|
|
top_vert_arr = top_vert_arr.reshape(self.height, self.width, 3)
|
|
|
|
|
|
|
|
|
|
# for bottom side
|
|
|
|
|
bottom_vert_arr = np.concatenate((vert_arr_tmp, z_cpy), axis=1)
|
|
|
|
|
bottom_vert_arr = bottom_vert_arr.reshape(self.height, self.width, 3)
|
|
|
|
@ -596,9 +615,18 @@ class app:
|
|
|
|
|
def save_stl(self):
|
|
|
|
|
'''Save final mesh to stl file.
|
|
|
|
|
'''
|
|
|
|
|
# TODO: add a hash function to create filename specific to input image and preset
|
|
|
|
|
self.stl_file = self.stl_file.split(".")[0] + "_" + self.id + "." + self.stl_file.split(".")[1]
|
|
|
|
|
self.stl_model.save(self.stl_file)
|
|
|
|
|
# TODO: add a hash function to create ID specific to
|
|
|
|
|
# input image + preset from config. file or from console + input params
|
|
|
|
|
# TODO: add the full parameters and filters to a file inside output dir.
|
|
|
|
|
# TODO: somehow add the full params to the stl file header if possible.
|
|
|
|
|
# TODO: add the ID to backplate
|
|
|
|
|
# TODO: add the ID to stl file name
|
|
|
|
|
|
|
|
|
|
# for now only path + id(input filename + preset name) + .stl is used
|
|
|
|
|
# TODO: add output filename to the filename, hash the ID
|
|
|
|
|
# stl_filename = self.stl_path.rsplit("/")[0] + self.output_file.split("/"))[-1] + "_" + self.id + ".stl"
|
|
|
|
|
stl_filename = self.output_file.split(".")[0] + "_" + self.id + ".stl"
|
|
|
|
|
self.stl_model.save(stl_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# run the application
|
|
|
|
|