Skip to content

Commit 1cd0ff3

Browse files
committed
fix issues with idisp with opencv, and block option
1 parent 71451ca commit 1cd0ff3

File tree

3 files changed

+74
-40
lines changed

3 files changed

+74
-40
lines changed

machinevisiontoolbox/BundleAdjust.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ def graph(self):
950950
"""
951951
return self.g
952952

953-
def plot(self, camera={}, ax=None, **kwargs):
953+
def plot(self, camera={}, block=None, ax=None, **kwargs):
954954
"""
955955
Plot the scene graph
956956
@@ -965,9 +965,8 @@ def plot(self, camera={}, ax=None, **kwargs):
965965
966966
:seealso: :meth:`graph`
967967
"""
968-
if ax is None:
969-
# plt.clf() # causes spurious 2d plot with Jupyter
970-
ax = base.plotvol3()
968+
# plt.clf() # causes spurious 2d plot with Jupyter
969+
ax = base.plotvol3(ax=ax)
971970
self.g.plot(**kwargs) # edge=dict(color=0.8*np.r_[1, 1, 1]), **kwargs)
972971
# ax.set_aspect('equal')
973972

@@ -985,6 +984,9 @@ def plot(self, camera={}, ax=None, **kwargs):
985984
ax.set_zlabel("Z (m)")
986985
plt.grid(True)
987986

987+
if block is not None:
988+
plt.show(block=block)
989+
988990
def __repr__(self):
989991
"""
990992
String representation

machinevisiontoolbox/base/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"iread",
4646
"iwrite",
4747
"convert",
48+
"cv_destroy_window",
4849
# types
4950
"int_image",
5051
"float_image",

machinevisiontoolbox/base/imageio.py

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
from spatialmath.base import islistof
2121

2222
__last_windowname = None
23-
23+
__last_window_number = 0
2424

2525
def idisp(
2626
im,
2727
colororder="RGB",
2828
matplotlib=True,
29-
block=False,
29+
block=None,
3030
fps=None,
3131
fig=None,
3232
ax=None,
@@ -41,7 +41,7 @@ def idisp(
4141
badcolor=None,
4242
undercolor=None,
4343
overcolor=None,
44-
title="Machine Vision Toolbox for Python",
44+
title=None,
4545
grid=False,
4646
axes=True,
4747
gui=True,
@@ -208,8 +208,8 @@ def idisp(
208208
axes = False
209209
frame = False
210210

211-
if fps is not None:
212-
block = 1 / fps
211+
# if fps is not None:
212+
# block = 1 / fps
213213

214214
# if we are running in a Jupyter notebook, print to matplotlib,
215215
# otherwise print to opencv imshow/new window. This is done because
@@ -231,6 +231,9 @@ def idisp(
231231
# im = np.hstack(imcl)
232232
# # else just plot the regular image - only one channel
233233

234+
if title is None:
235+
title = "Machine Vision Toolbox for Python"
236+
234237
if len(plt.get_fignums()) == 0:
235238
# there are no figures, create one
236239
fig, ax = plt.subplots() # fig creates a new window
@@ -251,12 +254,12 @@ def idisp(
251254
except:
252255
pass
253256

254-
if block is None:
255-
pass
256-
elif isinstance(block, bool):
257+
if fps is not None:
258+
print("pausing", 1.0 / fps)
259+
plt.pause(1.0 / fps)
260+
261+
if block is not None:
257262
plt.show(block=block)
258-
else:
259-
plt.pause(block)
260263

261264
return
262265

@@ -580,27 +583,30 @@ def format_coord(u, v):
580583
# don't display data
581584
h.format_cursor_data = lambda x: ""
582585

583-
if block is None:
584-
pass
585-
elif isinstance(block, bool):
586+
if fps is not None:
587+
print("pausing", 1.0 / fps)
588+
plt.pause(1.0 / fps)
589+
590+
if block is not None:
586591
plt.show(block=block)
587-
else:
588-
plt.pause(block)
592+
589593
return h
590594
else:
591595
## display using OpenCV
592-
global __last_windowname
596+
global __last_window_number
593597

594-
if reuse:
595-
if __last_windowname is not None:
596-
title = __last_windowname
597-
else:
598-
title = "idisp"
599-
else:
600-
__last_windowname = title
598+
if not reuse and title is None:
599+
# create a unique window name for each call
600+
title = "idisp." + str(__last_window_number)
601+
__last_window_number += 1
601602

602603
cv.namedWindow(title, cv.WINDOW_AUTOSIZE)
603604
cv.imshow(title, im) # make sure BGR format image
605+
cv.waitKey(1)
606+
607+
if fps is not None:
608+
# wait one frame time
609+
cv.waitKey(round(1000.0 / fps))
604610

605611
if block is True:
606612
while True:
@@ -609,15 +615,24 @@ def format_coord(u, v):
609615
cv.destroyWindow(title)
610616
cv.waitKey(1)
611617
break
612-
elif isinstance(block, (int, float)):
613-
cv.waitKey(round(block * 1000))
614618

615619
# TODO fig, ax equivalent for OpenCV? how to print/plot to the same
616620
# window/set of axes?
617-
fig = None
618-
ax = None
619621

620-
return fig, ax
622+
623+
624+
def cv_destroy_window(title=None, block=True):
625+
626+
if title == "all":
627+
cv.destroyAllWindows()
628+
else:
629+
if block:
630+
while True:
631+
k = cv.waitKey(delay=0) # wait forever for keystroke
632+
if k == ord("q"):
633+
break
634+
cv.destroyWindow(title)
635+
cv.waitKey(1) # run the event loop
621636

622637

623638
def _isnotebook():
@@ -968,20 +983,36 @@ def click_event(event, x, y, flags, params):
968983

969984

970985
if __name__ == "__main__":
986+
from machinevisiontoolbox import *
987+
from machinevisiontoolbox.base import *
988+
989+
images = ImageCollection("seq/*.png")
990+
991+
im, file = iread("street.png", dtype="float")
992+
idisp(im, matplotlib=False)
993+
idisp(im, matplotlib=False)
994+
995+
for image in images:
996+
image.disp(
997+
title="sequence", reuse=True, fps=5, matplotlib=False
998+
) # do some operation
999+
1000+
# type 'q' in the image animation window to close it
1001+
cv_destroy_window("sequence", block=True)
9711002

9721003
# filename = "~/code/machinevision-toolbox-python/machinevisiontoolbox/images/campus/*.png"
9731004

9741005
# im = iread(filename)
9751006
# print(im[0])
9761007

977-
from machinevisiontoolbox import VideoCamera, Image
978-
from machinevisiontoolbox.base import idisp
979-
import numpy as np
1008+
# from machinevisiontoolbox import VideoCamera, Image
1009+
# from machinevisiontoolbox.base import idisp
1010+
# import numpy as np
9801011

981-
c = VideoCamera(0, rgb=False)
982-
x = c.grab()
1012+
# c = VideoCamera(0, rgb=False)
1013+
# x = c.grab()
9831014

984-
x.disp(block=True)
1015+
# x.disp(block=True)
9851016
# x.disp(block=True)
9861017
# idisp(x.image, colororder="BGR", block=True)
9871018

@@ -1017,5 +1048,5 @@ def click_event(event, x, y, flags, params):
10171048
# idisp(im, matplotlib=True, block=False, colormap='grey', xydata=np.r_[10,20,30,40], title='grey')
10181049
# idisp(im, matplotlib=True, block=True, colormap='grey', ynormal=True, title='grey')
10191050

1020-
# im, file = iread('street.png', dtype='float')
1021-
# idisp(im, title='Boo!', block=True)
1051+
# im, file = iread("street.png", dtype="float")
1052+
# idisp(im, title="Boo!", block=True)

0 commit comments

Comments
 (0)