Allow to use h also and lock the code. msg box not here anymore.
This commit is contained in:
parent
9e4ec13934
commit
bc497fdf37
@ -914,6 +914,7 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
|
||||
# If 'c', allows retrieving imgp_confirmed by clicking them on the image
|
||||
scat = ax.scatter([], [], s=100, marker='+', color='g')
|
||||
plt.connect('button_press_event', on_click)
|
||||
plt.connect('key_press_event', on_click)
|
||||
# If objp is given, display 3D object points in black
|
||||
if len(objp) != 0 and not plt.fignum_exists(2):
|
||||
fig_3d = plt.figure()
|
||||
@ -933,34 +934,6 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
|
||||
ax_3d.view_init(elev=-90, azim=0)
|
||||
fig_3d.show()
|
||||
|
||||
if event.key == 'h':
|
||||
# If 'h', indicates that one of the objp is not visible on image
|
||||
# Displays it in red on 3D plot
|
||||
if len(objp) != 0 and 'ax_3d' in globals():
|
||||
count = [0 if 'count' not in globals() else count+1][0]
|
||||
if 'events' not in globals():
|
||||
# retrieve first objp_confirmed_notok and plot 3D
|
||||
events = [event]
|
||||
objp_confirmed_notok = objp[count]
|
||||
ax_3d.scatter(*objp_confirmed_notok, marker='o', color='r')
|
||||
fig_3d.canvas.draw()
|
||||
elif count == len(objp)-1:
|
||||
# if all objp have been clicked or indicated as not visible, close all
|
||||
objp_confirmed = np.array([[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed+[objp[count]]][0])[:-1]
|
||||
imgp_confirmed = np.array(np.expand_dims(scat.get_offsets(), axis=1), np.float32)
|
||||
plt.close('all')
|
||||
for var_to_delete in ['events', 'count', 'scat', 'fig_3d', 'ax_3d', 'objp_confirmed_notok']:
|
||||
if var_to_delete in globals():
|
||||
del globals()[var_to_delete]
|
||||
else:
|
||||
# retrieve other objp_confirmed_notok and plot 3D
|
||||
events.append(event)
|
||||
objp_confirmed_notok = objp[count]
|
||||
ax_3d.scatter(*objp_confirmed_notok, marker='o', color='r')
|
||||
fig_3d.canvas.draw()
|
||||
else:
|
||||
pass
|
||||
|
||||
|
||||
def on_click(event):
|
||||
'''
|
||||
@ -971,17 +944,29 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
|
||||
global imgp_confirmed, objp_confirmed, objp_confirmed_notok, scat, ax_3d, fig_3d, events, count, xydata, in_prompt
|
||||
# To avoid to much click during the prompt
|
||||
in_prompt_not_exist = "in_prompt" not in globals()
|
||||
# This part is done because to allow to manage in the same function button_press_event and key_press_event
|
||||
is_button_one = False
|
||||
is_button_three = False
|
||||
is_key_h = False
|
||||
if event.name == 'button_press_event':
|
||||
if event.button == 1:
|
||||
is_button_one = True
|
||||
elif event.button == 3:
|
||||
is_button_three = True
|
||||
elif event.name == 'key_press_event':
|
||||
if event.key == "h":
|
||||
is_key_h = True
|
||||
|
||||
if in_prompt_not_exist or not in_prompt:
|
||||
# Left click: Add clicked point to imgp_confirmed
|
||||
# Display it on image and on 3D plot
|
||||
if event.button == 1:
|
||||
if is_button_one or is_key_h:
|
||||
# To remember the event to cancel after right click
|
||||
if 'events' in globals():
|
||||
events.append(event)
|
||||
else:
|
||||
events = [event]
|
||||
|
||||
if is_button_one:
|
||||
# Add clicked point to image
|
||||
xydata = scat.get_offsets()
|
||||
new_xydata = np.concatenate((xydata, [[event.xdata, event.ydata]]))
|
||||
@ -995,25 +980,40 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
|
||||
if count == 0:
|
||||
# retrieve objp_confirmed and plot 3D
|
||||
objp_confirmed = [objp[count]]
|
||||
if is_button_one:
|
||||
ax_3d.scatter(*objp[count], marker='o', color='g')
|
||||
elif is_key_h:
|
||||
ax_3d.scatter(*objp_confirmed_notok, marker='o', color='r')
|
||||
fig_3d.canvas.draw()
|
||||
print(count)
|
||||
|
||||
elif count == len(objp) - 1:
|
||||
in_prompt = True
|
||||
if is_button_one:
|
||||
objp_confirmed = \
|
||||
[[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed + [objp[count]]][0]
|
||||
elif is_key_h:
|
||||
objp_confirmed = np.array([[objp[
|
||||
count]] if 'objp_confirmed' not in globals() else objp_confirmed + [
|
||||
objp[count]]][0])[:-1]
|
||||
ax_3d.scatter(*objp[count], marker='o', color='g')
|
||||
fig_3d.canvas.draw()
|
||||
# Small bit of code to not have the basic windows of tkinter when using the askquestion messsage box
|
||||
msg = tkinter.Tk()
|
||||
msg.withdraw()
|
||||
msg_box = tkinter.messagebox.askquestion(
|
||||
"Validate Calibration", "Are you satisfied with point positioning ? ", icon="warning"
|
||||
)
|
||||
|
||||
if msg_box.lower() == "yes":
|
||||
# close all
|
||||
plt.close('all')
|
||||
# retrieve objp_confirmed
|
||||
# objp_confirmed = np.array([[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed+[objp[count]]][0])
|
||||
if is_button_one:
|
||||
imgp_confirmed = np.array(imgp_confirmed, np.float32)
|
||||
elif is_key_h:
|
||||
# if all objp have been clicked or indicated as not visible, close all
|
||||
imgp_confirmed = np.array(np.expand_dims(scat.get_offsets(), axis=1), np.float32)
|
||||
# delete all
|
||||
plt.close('all')
|
||||
in_prompt = False
|
||||
for var_to_delete in ['events', 'count', 'scat', 'scat_3d', 'fig_3d', 'ax_3d',
|
||||
'objp_confirmed_notok']:
|
||||
@ -1040,19 +1040,36 @@ def imgp_objp_visualizer_clicker(img, imgp=[], objp=[], img_path=''):
|
||||
ax_3d.collections[-1].remove()
|
||||
fig_3d.canvas.draw()
|
||||
|
||||
# If last event was 'h' key
|
||||
elif events[-1].key == 'h':
|
||||
if len(objp) != 0:
|
||||
if count >= 1: count -= 1
|
||||
# Remove last point from objp_confirmed_notok
|
||||
objp_confirmed_notok = objp_confirmed_notok[:-1]
|
||||
# remove from plot
|
||||
if len(ax_3d.collections) > len(objp):
|
||||
ax_3d.collections[-1].remove()
|
||||
fig_3d.canvas.draw()
|
||||
|
||||
else:
|
||||
# retrieve objp_confirmed and plot 3D
|
||||
if is_button_one:
|
||||
objp_confirmed = \
|
||||
[[objp[count]] if 'objp_confirmed' not in globals() else objp_confirmed + [objp[count]]][0]
|
||||
ax_3d.scatter(*objp[count], marker='o', color='g')
|
||||
fig_3d.canvas.draw()
|
||||
print(count)
|
||||
elif is_key_h:
|
||||
# retrieve other objp_confirmed_notok and plot 3D
|
||||
events.append(event)
|
||||
objp_confirmed_notok = objp[count]
|
||||
ax_3d.scatter(*objp_confirmed_notok, marker='o', color='r')
|
||||
fig_3d.canvas.draw()
|
||||
|
||||
|
||||
# Right click:
|
||||
# If last event was left click, remove last point and if objp given, from objp_confirmed
|
||||
# If last event was 'H' and objp given, remove last point from objp_confirmed_notok
|
||||
elif event.button == 3: # right click
|
||||
elif is_button_three: # right click
|
||||
if 'events' in globals():
|
||||
# If last event was left click:
|
||||
if 'button' in dir(events[-1]):
|
||||
|
Loading…
Reference in New Issue
Block a user