Added base height param to curved generation, solved naming issue.

master
Rostislav Lán 2 years ago
parent 77e0fc4b8a
commit 7e45cf4a5b

@ -71,27 +71,29 @@ class app:
if self.args.stl_file: if self.args.stl_file:
# Get stl filename # 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 # Get mode and model parameters
if self.args.planar: if self.args.planar:
self.mode = "2d" self.mode = "planar"
if len(self.args.stl_file) < 3: # TODO: add default values for planar mode, not like this
self.height_base = 10 if len(self.args.stl_file) < 2:
self.height_line = 2 self.height_line = 2
self.height_base = 10
print( print(
"Warning: Too few arguments, using default values (10mm base, 2mm lines)") "Warning: Too few arguments, using default values (10mm base, 2mm lines)")
else: else:
self.height_line = float(self.args.stl_file[1]) self.height_line = float(self.args.stl_file[0])
self.height_base = float(self.args.stl_file[2]) self.height_base = float(self.args.stl_file[1])
print("Base height:", self.height_base, print("Base height:", self.height_base,
"mm, lines depth/height:", self.height_line, "mm") "mm, lines depth/height:", self.height_line, "mm")
else: 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_line = 2
self.height_base = 10 self.height_base = 10
self.curv_rate_x = 0.5 self.curv_rate_x = 0.5
@ -99,16 +101,16 @@ class app:
print( print(
"Warning: Too few arguments, using default values (2mm lines, curvature 0.5 on x, 0.5 on y)") "Warning: Too few arguments, using default values (2mm lines, curvature 0.5 on x, 0.5 on y)")
else: else:
self.height_line = float(self.args.stl_file[1]) self.height_line = float(self.args.stl_file[0])
self.height_base = float(self.args.stl_file[2]) self.height_base = float(self.args.stl_file[1])
self.curv_rate_x = float( 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.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, 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) "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() self.run_stl()
def parse_arguments(self): def parse_arguments(self):
@ -116,8 +118,8 @@ class app:
''' '''
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 transforming 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]') 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 # positional arguments
parser.add_argument("input_file", type=str, help="input file path") 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='*', parser.add_argument('-s', '--stl_file', type=str, nargs='*',
help="create stl model from processed image") 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, parser.add_argument('-p', '--planar', type=bool, action=ap.BooleanOptionalAction,
help="make stl shape planar instead of curved one") help="make stl shape planar instead of curved one")
@ -157,7 +159,7 @@ class app:
"ksize", "kernel", "sigmaX", "sigmaY", "ksize", "kernel", "sigmaX", "sigmaY",
"sigmaColor", "sigmaSpace", "d", "anchor", "iterations", "sigmaColor", "sigmaSpace", "d", "anchor", "iterations",
"op", "strength", "amount", "radius", "weight", "channelAxis", "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: for key in possible_params:
if params.get(key) is None: if params.get(key) is None:
@ -188,7 +190,7 @@ class app:
self.error_exit("Preset not found") self.error_exit("Preset not found")
def error_exit(self, message): def error_exit(self, message):
'''Print error message and exit. '''Print error message and exit the application.
''' '''
print("ERROR:", message, file=sys.stderr) print("ERROR:", message, file=sys.stderr)
@ -204,12 +206,12 @@ class app:
self.img = cv.imread( self.img = cv.imread(
self.input_file, cv.IMREAD_GRAYSCALE).astype(np.uint8) 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 # gets empty figure and ax with dimensions of input image
self.height, self.width = self.img.shape
fig, ax = self.get_empty_figure() fig, ax = self.get_empty_figure()
print("Height: " + str(self.height) + " px and width: " # print("Height: " + str(self.height) + " px and width: "
+ str(self.width) + " px", file=sys.stderr) # + str(self.width) + " px", file=sys.stderr)
if self.mirror is True: if self.mirror is True:
self.mirror_image() self.mirror_image()
@ -269,19 +271,25 @@ class app:
self.prepare_heightmap() self.prepare_heightmap()
self.get_ID() self.get_ID()
print("Creating mesh", file=sys.stderr)
# Create a mesh using one of two modes # Create a mesh using one of two modes
if self.mode == "2d": if self.mode == "planar":
self.make_stl_planar() self.make_stl_planar()
elif self.mode == "3d": elif self.mode == "curved":
self.make_stl_curved() self.make_stl_curved()
elif self.mode == "mapped":
# TODO: find a suitable finger model, try to map the fingerprint onto it
pass
else: else:
self.error_exit("Mode not supported") self.error_exit("Mode not supported")
plt.show() plt.show()
self.save_stl() 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): def prepare_heightmap(self):
'''Modify image values to get usable height/depth values. '''Modify image values to get usable height/depth values.
@ -289,30 +297,32 @@ class app:
Prepare meshgrid. Prepare meshgrid.
''' '''
# TODO: redo, too complicated, add extra params, redo checks if self.img.dtype != np.uint8:
if self.img.dtype == np.float32 or self.img.dtype == np.float64:
print("Converting to uint8", file=sys.stderr) 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) 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: if self.height_base <= 0:
self.error_exit("Depth of plate height must be positive") self.error_exit("Depth of plate height must be positive")
if self.height_line + self.height_base <= 0: if self.height_line + self.height_base <= 0:
self.error_exit("Line depth must be less than plate thickness") self.error_exit("Line depth must be less than plate thickness")
# Transform image values to get a heightmap if self.mode == "curved":
self.img = (self.height_base + (1 - self.img/255) # still need this value later
* self.height_line) height_base = 0
# 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")
if self.mode == "3d": # Transform image values to get a heightmap
# TODO check curvature values and print info self.img = (height_base + (1 - self.img/255)
# TODO: copy pasta code, remove * self.height_line)
# Transform image values to get a heightmap
self.img = (1 - self.img/255) * self.height_line
# This sets the size of stl model and number of subdivisions / triangles # This sets the size of stl model and number of subdivisions / triangles
x = np.linspace(0, self.width * 25.4 / self.dpi, self.width) x = np.linspace(0, self.width * 25.4 / self.dpi, self.width)
@ -324,10 +334,15 @@ class app:
'''Get unique ID for the model. '''Get unique ID for the model.
Consists of pair input_file + preset_name. 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( self.id = self.input_file.split(
"/")[-1].split(".")[0] + "_" + self.preset_name "/")[-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): def append_faces(self, faces, c):
# Function to add faces to the list # Function to add faces to the list
@ -346,7 +361,7 @@ class app:
ax.plot([0, 1], [0, 1], c="black", lw=self.width) ax.plot([0, 1], [0, 1], c="black", lw=self.width)
# extract filename # extract filename
text = self.stl_file.split("/")[-1].split(".")[0] + self.id text = self.stl_path.split("/")[-1].split(".")[0] + self.id
fontsize = 20 fontsize = 20
# create text object, paint it white # create text object, paint it white
@ -371,14 +386,14 @@ class app:
plt.close() plt.close()
# TODO: maybe don't use nested for loops, use numpy? # 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 i in range(self.height):
for j in range(self.width): for j in range(self.width):
bottom_vert_arr[i][j][2] = data[i][j][0] 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 i in range(self.height):
for j in range(self.width): 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 return bottom_vert_arr
@ -495,6 +510,8 @@ class app:
'''Map fingerprint to finger model. '''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([]) z = np.array([])
for x in range(self.width): for x in range(self.width):
z = np.append(z, np.sqrt(1 - (2*x/self.width - 1)**2) z = np.append(z, np.sqrt(1 - (2*x/self.width - 1)**2)
@ -511,9 +528,11 @@ class app:
z += self.img z += self.img
vert_arr_tmp = np.vstack(list(map(np.ravel, self.meshgrid))).T vert_arr_tmp = np.vstack(list(map(np.ravel, self.meshgrid))).T
# for top side # for top side
top_vert_arr = np.concatenate((vert_arr_tmp, z), axis=1) top_vert_arr = np.concatenate((vert_arr_tmp, z), axis=1)
top_vert_arr = top_vert_arr.reshape(self.height, self.width, 3) top_vert_arr = top_vert_arr.reshape(self.height, self.width, 3)
# for bottom side # for bottom side
bottom_vert_arr = np.concatenate((vert_arr_tmp, z_cpy), axis=1) bottom_vert_arr = np.concatenate((vert_arr_tmp, z_cpy), axis=1)
bottom_vert_arr = bottom_vert_arr.reshape(self.height, self.width, 3) bottom_vert_arr = bottom_vert_arr.reshape(self.height, self.width, 3)
@ -596,9 +615,18 @@ class app:
def save_stl(self): def save_stl(self):
'''Save final mesh to stl file. '''Save final mesh to stl file.
''' '''
# TODO: add a hash function to create filename specific to input image and preset # TODO: add a hash function to create ID specific to
self.stl_file = self.stl_file.split(".")[0] + "_" + self.id + "." + self.stl_file.split(".")[1] # input image + preset from config. file or from console + input params
self.stl_model.save(self.stl_file) # 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 # run the application

Loading…
Cancel
Save