-
-
Save Pythonsegmenter/0705940ef30bcfd78030121ffd0a25b9 to your computer and use it in GitHub Desktop.
A script to turn a spline into a vessel in mimics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mimics | |
import math | |
import numpy as np | |
import time | |
from tkinter import * | |
from tkinter import messagebox | |
#Parameters | |
closing_pixels=1 | |
# <editor-fold desc="helper functions"> | |
def GetArray(mask): | |
return np.array(mask.get_voxel_buffer()) | |
def SetMask(mask, array): | |
return mask.set_voxel_buffer(memoryview(array)) | |
def Coo2Ind(pt): | |
return mimics.data.images[0].get_voxel_indexes(point_coordinates=pt) | |
def Ind2Coo(pt): | |
return mimics.data.images[0].get_voxel_center(tuple(pt)) | |
def GetValueArr(arr, coo): | |
return arr[coo[0]][coo[1]][coo[2]] | |
def SetValueArr(arr, coo, value=1): | |
arr[coo[0]][coo[1]][coo[2]] = value | |
return arr | |
def addition(u, v): | |
return [u[i] + v[i] for i in range(len(u))] | |
def multiplication(u, k): | |
return [u[i] * k for i in range(len(u))] | |
def subtraction(u, v): | |
return [u[i] - v[i] for i in range(len(u))] | |
def dot_product(u, v): | |
return sum(u[i] * v[i] for i in range(len(u))) | |
def cross_product(a, b): | |
assert len(a) == len(b) == 3, 'For 3D vectors only' | |
a1, a2, a3 = a | |
b1, b2, b3 = b | |
return [a2 * b3 - a3 * b2, a3 * b1 - a1 * b3, a1 * b2 - a2 * b1] | |
def magnitude(v): | |
return math.sqrt(sum(v[i] * v[i] for i in range(len(v)))) | |
def distance(u, v): | |
d = 0 | |
for i in range(len(u)): | |
dc = u[i] - v[i] | |
d += dc * dc | |
return math.sqrt(d) | |
def point_to_line_projection(pt, origin, direction): | |
ap = subtraction(pt, origin) | |
dp = dot_product(ap, direction) | |
return addition(origin, multiplication(direction, dp)) | |
def Ind2CooFast(pt, ratio_diff_ind2coo, ref_point_coo, ref_point_ind): | |
# assert type(pt[0]) == int, 'Error, you are trying to convert coordinate to coordinate!' | |
coo = [0, 0, 0] | |
for i in range(0, 3): | |
coo[i] = ref_point_coo[i] + ratio_diff_ind2coo[i] * (pt[i] - ref_point_ind[i]) | |
return coo | |
def Coo2IndFast(pt, ratio_diff_coo2ind, ref_point_coo, ref_point_ind): | |
# assert type(pt[0]) == float, 'Error, you are trying to convert index to index!' | |
ind = [0, 0, 0] | |
for i in range(0, 3): | |
ind[i] = int(ref_point_ind[i] + ratio_diff_coo2ind[i] * (pt[i] - ref_point_coo[i])) | |
return ind | |
def find_closest_point(point, points_list): | |
"""Find the closest point in a list to a certain point""" | |
min_distance = 10000 # Taking a super large distance so it's certainly not the smallest | |
ind = 0 | |
for pt in points_list: | |
dist_betw_pts = distance(point, pt) | |
if dist_betw_pts < min_distance: | |
min_point = pt | |
min_distance = dist_betw_pts | |
min_ind = ind | |
ind = ind + 1 | |
return min_point, min_ind | |
def find_min_distance(point, points_list): | |
"""Find the closest point in a list to a certain point""" | |
min_distance = 10000 # Taking a super large distance so it's certainly not the smallest | |
ind = 0 | |
for pt in points_list: | |
dist_betw_pts = distance(point, pt) | |
if dist_betw_pts < min_distance: | |
min_point = pt | |
min_distance = dist_betw_pts | |
min_ind = ind | |
ind = ind + 1 | |
return min_distance, min_point | |
def corresponding_points(man_pt_list, curve_pt_list): | |
"""Find the indices of the points in curve_pt_list that are closest to the points in man_pt_list""" | |
correspondance_pt = [] | |
correspondance_ind = [] | |
for index_man in range(1, len(man_pt_list) - 1): | |
dist_result = find_closest_point(man_pt_list[index_man], curve_pt_list) | |
correspondance_pt.append(dist_result[0]) | |
correspondance_ind.append(dist_result[1]) | |
return correspondance_pt, correspondance_ind | |
def corresponding_points_full_list(man_pt_list, curve_pt_list): | |
"""Find the indices of the points in curve_pt_list that are closest to the points in man_pt_list""" | |
correspondance_pt = [] | |
correspondance_ind = [] | |
for index_man in range(0, len(man_pt_list)): | |
dist_result = find_closest_point(man_pt_list[index_man], curve_pt_list) | |
correspondance_pt.append(dist_result[0]) | |
correspondance_ind.append(dist_result[1]) | |
return correspondance_pt, correspondance_ind | |
def create_vessel(man_points, geometry_points, diameter_list, vessel_arr, fill=True): | |
# Get the start and end points | |
start_point = geometry_points[0] | |
second_point = geometry_points[1] | |
forelast_point = geometry_points[-2] | |
end_point = geometry_points[-1] | |
# Get the first and last points' size | |
[start_man_pt, start_man_ind] = find_closest_point(start_point, man_points) | |
[end_man_pt, end_man_ind] = find_closest_point(end_point, man_points) | |
# find the corresponding indices and points | |
[correspondance_pt, correspondance_ind] = corresponding_points(man_points, geometry_points) | |
# Get the amount of points | |
points_in_spline = len(geometry_points) | |
# Initializing diameters | |
previous_diameter = diameter_list[0] | |
next_diameter = diameter_list[1] | |
current_diameter_index = 0 | |
corr_ind = 0 | |
if len(correspondance_ind)==0: #In case there are only 2 points | |
next_diameter_index=len(geometry_points)-1 | |
else: | |
next_diameter_index = correspondance_ind[corr_ind] | |
# Start looping over every point in the spline | |
for index in range(0, points_in_spline - 1): | |
# Get the 2 points that define the boundaries of the cylinder axis | |
point1 = geometry_points[index] | |
point2 = geometry_points[index + 1] | |
# Get the normalized direction | |
direction = multiplication(subtraction(point2, point1), 1 / magnitude(subtraction(point2, point1))) | |
# Update current and next diameter once the next diameter index is reached | |
if next_diameter_index == index: | |
current_diameter_index = next_diameter_index | |
previous_diameter = next_diameter | |
corr_ind = corr_ind + 1 # Keeps track of where we are in the correspondance_ind list | |
# Special case for the last diameter | |
if corr_ind == len(correspondance_ind): | |
next_diameter_index = len(geometry_points) | |
next_diameter = diameter_list[-1] | |
# Normal case, update index and diameter | |
else: | |
next_diameter_index = correspondance_ind[corr_ind] | |
next_diameter = diameter_list[corr_ind + 1] | |
# Calculate diameter | |
diff_next_curr_dia = (next_diameter - previous_diameter) | |
diff_ind = (next_diameter_index - current_diameter_index) | |
diameter = previous_diameter + diff_next_curr_dia * (index - current_diameter_index) / diff_ind | |
# Define the boundaries for the distance calculation | |
min_x = min([point1[0], point2[0]]) - diameter / 2 | |
min_y = min([point1[1], point2[1]]) - diameter / 2 | |
min_z = min([point1[2], point2[2]]) - diameter / 2 | |
min_coo = [min_x, min_y, min_z] | |
min_ind = Coo2IndFast(min_coo, ratio_diff_coo2ind, ref_pt_coo, ref_pt_ind) | |
max_x = max([point1[0], point2[0]]) + diameter / 2 | |
max_y = max([point1[1], point2[1]]) + diameter / 2 | |
max_z = max([point1[2], point2[2]]) + diameter / 2 | |
max_coo = [max_x, max_y, max_z] | |
max_ind = Coo2IndFast(max_coo, ratio_diff_coo2ind, ref_pt_coo, ref_pt_ind) | |
for x in range(min_ind[0], max_ind[0] + 1): | |
for y in range(min_ind[1], max_ind[1] + 1): | |
for z in range(min_ind[2], max_ind[2] + 1): | |
# Define the point under investigation | |
point = (x, y, z) | |
point_coo = Ind2CooFast(point, ratio_diff_ind2coo, ref_pt_coo, ref_pt_ind) | |
# Search the projection of this point on the cylinder axis | |
proj_point = point_to_line_projection(point_coo, point1, direction) | |
# Look if it lies within the required distance of the axis | |
if distance(point_coo, proj_point) < diameter / 2: | |
try: # This is done to prevent errors from out of image assignments | |
SetValueArr(vessel_arr, point, fill) | |
except: | |
excepted = True | |
pass | |
return vessel_arr | |
def find_start_index(diameter_list, index): | |
diameter = diameter_list[index] | |
while index > 0: | |
if diameter_list[index] != diameter: | |
adapt_1_extra=True #cause diameter gradually enlarges | |
break | |
index = index - 1 | |
if index==0: | |
adapt_1_extra=False #cause diameter gradually enlarges | |
return index,adapt_1_extra | |
def find_end_index(diameter_list, index): | |
diameter = diameter_list[index] | |
while index < len(diameter_list): | |
if diameter != diameter_list[index]: | |
break | |
index = index + 1 | |
if index==len(diameter_list): | |
adapt_1_extra=True | |
return index-1,adapt_1_extra | |
else: | |
adapt_1_extra=False | |
return index,adapt_1_extra | |
# </editor-fold> | |
# <editor-fold desc="Messages"> | |
def create_spline_warning(): | |
# All the below is necessary because tkinter is acting like a bitch | |
root = Tk() | |
root.withdraw() | |
root.attributes('-topmost', True) | |
ans = messagebox.showinfo("Create", | |
"1. Start creating a new spline and create the first point of your spline (Analyze>Spline>Draw)\n2. Press OK when this is done to input your starting diameter.\n\n" | |
"IMPORTANT NOTE: Use the \'Done drawing\' button in the following dialog instead of create spline or double-click to finish your spline.") | |
root.destroy() | |
return ans | |
def verify_results_warning(): | |
# All the below is necessary because tkinter is acting like a bitch | |
root = Tk() | |
root.withdraw() | |
root.attributes('-topmost', True) | |
ans = messagebox.showinfo("Verify results", | |
"1. A part as well as a mask \'vessels has been created\'. Please verify the results now. \n" | |
"2. In the following step you'll be able to adapt the diameters one last time.") | |
root.destroy() | |
return ans | |
def create_other_spline_warning(): | |
# All the below is necessary because tkinter is acting like a bitch | |
root = Tk() | |
root.withdraw() | |
root.attributes('-topmost', True) | |
ans = messagebox.showinfo("Create other spline", | |
"Please make sure to first finish the current spline by clicking \'create spline\' in the popup spline window.") | |
root.destroy() | |
return ans | |
def other_spline_question(): | |
root = Tk() | |
root.withdraw() | |
root.attributes('-topmost', True) | |
ans = messagebox.askyesno("Other spline?", | |
"Would you like to create another spline/vessel?") | |
root.destroy() | |
return ans | |
def adapt_question(): | |
root = Tk() | |
root.withdraw() | |
root.attributes('-topmost', True) | |
ans = messagebox.askyesno("Adapt diameter", | |
"Would you like to adapt the diameter of a spline/vessel?") | |
root.destroy() | |
return ans | |
def quit_warning(): | |
root = Tk() | |
root.withdraw() | |
root.attributes('-topmost', True) | |
ans = messagebox.askyesno("Are you sure?", | |
"Do you really want to quit drawing and save the points?") | |
root.destroy() | |
return ans | |
def non_valid_diameter_warning(): | |
root = Tk() | |
root.withdraw() | |
root.attributes('-topmost', True) | |
ans = messagebox.showerror("Invalid diameter", | |
"Error, please input a valid non-zero diameter.") | |
root.destroy() | |
return ans | |
def info(): | |
# All the below is necessary because tkinter is acting like a bitch | |
ans = messagebox.showinfo("Help", | |
"How it works:\n\n" | |
"1. Start creating a new spline. The algorithm takes the last diameter you put in and remembers this for all of the future points that you create on your spline," | |
" untill you give it a new diameter. Once your done drawing create your spline by clicking on the \'Done drawing\' button.\n \n" | |
"2. Now you can start creating the next spline if you would want to. If not we're going to step 3.\n \n" | |
"3. The algorith will create a tube-shaped mask around your spline, however this means that it its limited to the pixel resolution. So putting a diameter of 0.5mm\n" | |
"is pointless, since pixel-resolution is most often 0.7mm.\n \n" | |
"4. The mask vessels will be created. You can now further adapt this mask, calculate a part or boolean join it with your other work.\n \n" | |
"NOTE: If you do not change the name of the created splines then they will be deleted. \n\n\n" | |
"Questions or did you find any bugs?\n" | |
"Mail it to jeroenvancraen12@gmail.com\n \n" | |
"Thanks for using this script, I hope you enjoy it!!\n \n" | |
"Jeroen") | |
return ans | |
def final_warning(): | |
root = Tk() | |
root.withdraw() | |
root.attributes('-topmost', True) | |
ans = messagebox.showinfo("Goodluck and goodbye!", | |
"A mask and a part \"Vessels\" has been created. You can now adapt the mask further or boolean join it to another mask.") | |
root.destroy() | |
return ans | |
# </editor-fold> | |
# <editor-fold desc="GUI"> | |
# definitions | |
def on_close(): | |
master.quit() | |
master.destroy() | |
global cause_error | |
cause_error = 1 | |
raise KeyboardInterrupt('User cancelled execution.') | |
def get_diameter(event=None): | |
global diameter | |
global current_diameter_str | |
diameter = float(input_entry.get()) | |
try: | |
diameter = float(input_entry.get()) | |
current_diameter_str = input_entry.get() | |
except: | |
diameter = 0 # This means no diameter was put ing | |
global position | |
position = [master.winfo_x(), master.winfo_y()] | |
master.destroy() | |
def done(): | |
global destroy | |
destroy = True | |
global position | |
position = [master.winfo_x(), master.winfo_y()] | |
master.destroy() | |
# </editor-fold> | |
##########################START CREATING SPLINES############################# | |
# Start message | |
create_spline_warning() | |
# Initialize some parameters | |
diameter_list = [] | |
diameter_matrix = [] | |
spline_matrix = [] | |
diameter = 0 | |
destroy = False | |
previous_points_length = 0 | |
previous_diameter = None | |
first_loop = True # Tels us if we're in the first loop | |
cause_error = 0 # Variable that tells us if the user clicked the close window button | |
current_diameter_str = '-' # The 'string-diameter' that will be used in the GUI | |
while True: # the break in the if of destroy will take us out of the loop | |
# <editor-fold desc="Create GUI"> | |
# Create the master | |
master = Tk() | |
master.title('Diameter input') | |
# get screen width and height | |
ws = master.winfo_screenwidth() # width of the screen | |
hs = master.winfo_screenheight() # height of the screen | |
# calculate x and y coordinates for the Tk master window | |
if first_loop: | |
x = (ws / 2) | |
y = (0.2 * hs) | |
else: # Get previous position (maybe user moved the window) | |
x = position[0] | |
y = position[1] | |
# set root position | |
master.geometry('+' + str(int(x)) + '+' + str(int(y))) | |
master.protocol('WM_DELETE_WINDOW', on_close) # Change the action upon clicking the 'X' of the window | |
master.attributes('-topmost', True) | |
# Create text | |
Label(master, text="Current:").grid(row=0, column=0) | |
Label(master, text=current_diameter_str).grid(row=0, column=1) | |
Label(master, text="Input:").grid(row=1, column=0) | |
Label(master, text="[mm]").grid(row=0, column=2) | |
Label(master, text="[mm]").grid(row=1, column=2) | |
# Create entry field | |
input_entry = Entry(master) | |
input_entry.grid(row=1, column=1) | |
# create buttons | |
input_button = Button(master, text="Input diameter", | |
command=get_diameter, bg='green') | |
input_button.grid(row=2, column=1, sticky=EW, padx=3, pady=3) | |
input_button.focus_set() | |
master.bind('<Return>', get_diameter) | |
done_button = Button(master, text="Done drawing", | |
command=done, bg='cyan') | |
done_button.grid(row=2, column=2, sticky=EW, padx=3, pady=3) | |
help_button=Button(master, text="?", | |
command=info, bg='pink') | |
help_button.grid(row=2, column=0, sticky=EW, padx=3, pady=3) | |
mainloop() | |
if cause_error == True: # For if the user clicks the 'X' button | |
raise KeyboardInterrupt('User cancelled execution.') | |
# </editor-fold> | |
# Find all splines | |
sp_list = mimics.data.splines.filter('.*', regex=True) | |
# Setting last spline equal to spline for first iteration | |
if len(sp_list) != 0: | |
sp = sp_list[-1] # Take the last spline | |
if first_loop: # This prevents last_sp to be undefined | |
last_sp = sp | |
first_loop = False | |
# No good diameter was put in | |
if diameter == 0: | |
destroy = False | |
non_valid_diameter_warning() | |
# No spline exists yet | |
elif len(sp_list) == 0: | |
previous_diameter = diameter | |
# A valid new point is added | |
else: | |
# Spline changed without clicking done drawing | |
if last_sp != sp: | |
diameter_list = [] | |
previous_points_length = 0 | |
# Getting some data | |
man_points = sp.points | |
points_length = len(man_points) | |
difference = points_length - previous_points_length | |
# Append the previous diameter the amount of times that was clicked since previous diameter change | |
for i in range(0, difference - 1): | |
if previous_diameter == None: | |
diameter_list.append(diameter) | |
else: | |
diameter_list.append(previous_diameter) | |
# Append the new diameter just for the last point | |
if difference > 0: | |
diameter_list.append(diameter) | |
# Print the changes that have been made | |
i = 0 | |
for dia in diameter_list[previous_points_length:]: | |
print("Point " + str(i + previous_points_length) + " got diameter: " + str(dia)) | |
i = i + 1 | |
# Update some parameters | |
previous_points_length = points_length | |
previous_diameter = diameter | |
last_sp = sp | |
# Check if the user wants to stop drawing | |
if destroy == True: | |
# Ask if he wants to draw another spline | |
if other_spline_question(): | |
# Saving the data | |
spline_matrix.append(sp.points) | |
diameter_matrix.append(diameter_list) | |
# Cancel the active tool (to prevent user from not finishing spline) | |
mimics.cancel_active_tool() | |
sp = mimics.analyze.create_spline(spline_matrix[-1]) | |
spline_matrix.pop(-1) | |
spline_matrix.append(sp) | |
sp.name = 'CHANGE NAME TO KEEP' | |
# resetting parameters act as if first loop | |
# Initialize some parameters | |
diameter_list = [] | |
diameter = 0 | |
destroy = False | |
previous_points_length = 0 | |
previous_diameter = None | |
first_loop = True # Tels us if we're in the first loop | |
cause_error = 0 # Variable that tells us if the user clicked the close window button | |
current_diameter_str = '-' # The 'string-diameter' that will be used in the GUI | |
# Verify that the user wants to stop | |
elif quit_warning() == 1: | |
# Saving the data | |
spline_matrix.append(sp.points) | |
diameter_matrix.append(diameter_list) | |
# Cancel the active tool (to prevent user from not finishing spline) | |
mimics.cancel_active_tool() | |
sp = mimics.analyze.create_spline(spline_matrix[-1]) | |
spline_matrix.pop(-1) | |
spline_matrix.append(sp) | |
sp.name = 'CHANGE NAME TO KEEP' | |
# Cancel the active tool | |
mimics.cancel_active_tool() | |
break | |
# Temporary for development purposes | |
print("List of diameters: " + str(diameter_list)) | |
#############################END CREATING SPLINES################################""""" | |
# Verify that splines were made | |
assert len(spline_matrix) != 0, "Error, no splines were indicated!" | |
# <editor-fold desc="Create parameters for index to point conversion"> | |
# Creating values for index to point conversion, globals cause otherwise we have to give them to every function. | |
global ref_pt_ind | |
global ref_pt_coo | |
global ratio_diff_coo2ind | |
global ratio_diff_ind2coo | |
print(spline_matrix[0]) | |
geometry_points = spline_matrix[0].geometry_points[0:2] | |
point1 = geometry_points[0] | |
point2 = geometry_points[1] | |
ref_pt_ind = list(Coo2Ind(point1)) | |
point2_ind = Coo2Ind(point2) | |
for i in range(0, 3): | |
if ref_pt_ind[i] == point2_ind[i]: | |
try: | |
ref_pt_ind[i] = ref_pt_ind[ | |
i] + 1 # If they lie in the same plane in one direction then we would have no data | |
except: | |
ref_pt_ind[i] = ref_pt_ind[i] - 1 | |
ref_pt_coo = Ind2Coo(ref_pt_ind) | |
point2_coo = Ind2Coo(point2_ind) | |
ratio_diff_ind2coo = [0, 0, 0] | |
for i in range(0, 3): | |
ind_diff = ref_pt_ind[i] - point2_ind[i] | |
coo_diff = ref_pt_coo[i] - point2_coo[i] | |
ratio_diff_ind2coo[i] = abs(coo_diff / ind_diff) | |
ratio_diff_coo2ind = [0, 0, 0] | |
for i in range(0, 3): | |
ratio_diff_coo2ind[i] = 1 / ratio_diff_ind2coo[i] | |
# </editor-fold> | |
# Initializing some stuff before we start computing | |
vessel_mask = mimics.segment.create_mask() | |
vessel_arr = GetArray(vessel_mask) | |
# Telling user that we're gonna calculate masks | |
print('Calculating mask, please wait...') | |
#start time | |
tstart = time.clock() | |
# Start looping over all the splines in the spline matrix | |
for master_index in range(0, len(spline_matrix)): | |
diameter_list = diameter_matrix[master_index] | |
sp = spline_matrix[master_index] | |
# Verify that there are diameters in the list | |
if len(diameter_list) != 0: | |
man_points=sp.points | |
geometry_points=sp.geometry_points | |
vessel_arr=create_vessel(man_points, geometry_points, diameter_list, vessel_arr, fill=True) | |
# Put the values in the mask | |
SetMask(vessel_mask, vessel_arr) | |
#Create the closed mask | |
final_mask = mimics.segment.morphology_operations(vessel_mask, 'Close', closing_pixels) | |
final_mask.name='Vessels' | |
vessel_mask.visible=False | |
#Report timing | |
tend = time.clock() - tstart | |
print("done in " + str(tend) + 'seconds') | |
part=mimics.segment.calculate_part(final_mask) | |
vessels=mimics.tools.smooth(part, 0.5, iterations=5, compensate_shrinkage=False, keep_originals=False) | |
vessels.name='Vessels' | |
verify_results_warning() | |
user_point=None | |
if adapt_question(): | |
first_loop = True | |
while True: | |
if first_loop: | |
first_loop=False | |
else: | |
if not adapt_question(): | |
break | |
#Delete the part, it's not good :( | |
try: | |
mimics.delete(part) | |
except: | |
pass | |
if user_point is not None: | |
mimics.data.points.delete(user_point) | |
#Ask the user to point the problem out | |
user_point = mimics.analyze.indicate_point( | |
message='Please indicate a point on the part of the spline you want to adapt.', show_message_box=True, | |
confirm=False, title=None) | |
#Search for the involved spline and diameter list | |
min_dist = 10000 | |
for index in range(0, len(spline_matrix)): | |
spline = spline_matrix[index] | |
geometry_points = spline.geometry_points | |
[dist, point] = find_min_distance(user_point, geometry_points) | |
if dist < min_dist: | |
min_index = index #Determines the index in the list of splines | |
min_dist = dist | |
min_point = point | |
spline = spline_matrix[min_index] | |
diameter_list = diameter_matrix[min_index] | |
print('min_index: ' +str(min_index)) | |
#Search the diameter at that point | |
man_points=spline.points | |
geometry_points=spline.geometry_points | |
[correspondance_pt, correspondance_ind] = corresponding_points_full_list([min_point],man_points) | |
print('min_point: ' +str(min_point)) | |
print('man_points: '+str(man_points)) | |
print('correspondance_ind: ' + str(correspondance_ind)) | |
dia_index = correspondance_ind[0] | |
current_diameter_str = str(diameter_list[dia_index]) | |
#Search the start and end indexes of the adaptation | |
print('diameter_list: ' +str(diameter_list)) | |
print('diameter_index: ' +str(dia_index)) | |
[start_index_man,adapt_1_extra_start] = find_start_index(diameter_list, dia_index) | |
[end_index_man, adapt_1_extra_end] = find_end_index(diameter_list, dia_index) | |
[correspondance_pt,correspondance_ind] = corresponding_points_full_list([man_points[start_index_man],man_points[end_index_man]],geometry_points) | |
print('start_index_man: ' +str(start_index_man)) | |
print('end_index_man: ' + str(end_index_man)) | |
#Limit adaptations to the affected region | |
diameter_list_adapt = diameter_list[start_index_man: end_index_man+1] | |
man_points_adapt = man_points[start_index_man:end_index_man+1] | |
geometry_points_adapt = geometry_points[correspondance_ind[0]:correspondance_ind[1]+1] | |
print('man_points_adapt: ' +str(man_points_adapt)) | |
#reset exit variable | |
destroy = False | |
while True: | |
# Make the gui again | |
# <editor-fold desc="Create GUI"> | |
# Create the master | |
master = Tk() | |
master.title('Diameter input') | |
# get screen width and height | |
ws = master.winfo_screenwidth() # width of the screen | |
hs = master.winfo_screenheight() # height of the screen | |
# calculate x and y coordinates for the Tk master window | |
if first_loop: | |
x = (ws / 2) | |
y = (0.2 * hs) | |
else: # Get previous position (maybe user moved the window) | |
x = position[0] | |
y = position[1] | |
# set root position | |
master.geometry('+' + str(int(x)) + '+' + str(int(y))) | |
master.protocol('WM_DELETE_WINDOW', on_close) # Change the action upon clicking the 'X' of the window | |
master.attributes('-topmost', True) | |
# Create text | |
Label(master, text="Current:").grid(row=0, column=0) | |
Label(master, text=current_diameter_str).grid(row=0, column=1) | |
Label(master, text="Input:").grid(row=1, column=0) | |
Label(master, text="[mm]").grid(row=0, column=2) | |
Label(master, text="[mm]").grid(row=1, column=2) | |
# Create entry field | |
input_entry = Entry(master) | |
input_entry.grid(row=1, column=1) | |
# create buttons | |
input_button = Button(master, text="Input diameter", | |
command=get_diameter, bg='green') | |
input_button.grid(row=2, column=1, sticky=EW, padx=3, pady=3) | |
input_button.focus_set() | |
master.bind('<Return>', get_diameter) | |
done_button = Button(master, text="I'm happy", | |
command=done, bg='cyan') | |
done_button.grid(row=2, column=2, sticky=EW, padx=3, pady=3) | |
mainloop() | |
if cause_error == True: # For if the user clicks the 'X' button | |
raise KeyboardInterrupt('User cancelled execution.') | |
# Exit if user is happy | |
if destroy == True: | |
break | |
# Deleting the closed mask | |
mimics.data.masks.delete(final_mask) | |
# </editor-fold> | |
# First clear the mask############################ | |
print(man_points_adapt) | |
print(diameter_list_adapt) | |
print(len(geometry_points)) | |
print(len(geometry_points_adapt)) | |
vessel_arr = create_vessel(man_points_adapt, geometry_points_adapt, diameter_list_adapt, vessel_arr, fill=False) | |
# Calculate the array again but this time with the new diameter | |
for i in range(start_index_man+adapt_1_extra_start,end_index_man+adapt_1_extra_end): #start_index_man+1 | |
diameter_list_adapt[i-start_index_man]=diameter # i-start_index_man | |
vessel_arr = create_vessel(man_points_adapt, geometry_points_adapt, diameter_list_adapt, vessel_arr, fill=True) | |
# Create a closed mask | |
SetMask(vessel_mask,vessel_arr) | |
final_mask = mimics.segment.morphology_operations(vessel_mask, 'Close', closing_pixels) | |
final_mask.name='Vessels' | |
vessel_mask.visible=False | |
part = mimics.segment.calculate_part(final_mask) | |
vessels = mimics.tools.smooth(part, 0.5, iterations=5, compensate_shrinkage=False, keep_originals=False) | |
vessels.name = 'Vessels' | |
#Deleting the last user point | |
if user_point is not None: | |
mimics.data.points.delete(user_point) | |
mimics.data.masks.delete(vessel_mask) | |
vessel_mask=final_mask | |
final_mask.name = 'Vessels' | |
#Delete generated splines | |
spline_matrix = mimics.data.splines.filter('CHANGE NAME TO KEEP') | |
for sp in spline_matrix: | |
mimics.data.splines.delete(sp) | |
final_warning() | |
print('Done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment