Various Python samples updated for Python 2/3 compatibility.
This commit is contained in:
@@ -12,12 +12,17 @@ Keys:
|
||||
ESC - exit
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
import numpy as np
|
||||
import cv2
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print __doc__
|
||||
print(__doc__)
|
||||
|
||||
import sys
|
||||
from itertools import cycle
|
||||
@@ -31,15 +36,20 @@ if __name__ == '__main__':
|
||||
img = cv2.imread(fn)
|
||||
|
||||
if img is None:
|
||||
print 'Failed to load image file:', fn
|
||||
print('Failed to load image file:', fn)
|
||||
sys.exit(1)
|
||||
|
||||
cv2.imshow('original', img)
|
||||
|
||||
modes = cycle(['erode/dilate', 'open/close', 'blackhat/tophat', 'gradient'])
|
||||
str_modes = cycle(['ellipse', 'rect', 'cross'])
|
||||
cur_mode = modes.next()
|
||||
cur_str_mode = str_modes.next()
|
||||
|
||||
if PY3:
|
||||
cur_mode = next(modes)
|
||||
cur_str_mode = next(str_modes)
|
||||
else:
|
||||
cur_mode = modes.next()
|
||||
cur_str_mode = str_modes.next()
|
||||
|
||||
def update(dummy=None):
|
||||
sz = cv2.getTrackbarPos('op/size', 'morphology')
|
||||
@@ -73,8 +83,14 @@ if __name__ == '__main__':
|
||||
if ch == 27:
|
||||
break
|
||||
if ch == ord('1'):
|
||||
cur_mode = modes.next()
|
||||
if PY3:
|
||||
cur_mode = next(modes)
|
||||
else:
|
||||
cur_mode = modes.next()
|
||||
if ch == ord('2'):
|
||||
cur_str_mode = str_modes.next()
|
||||
if PY3:
|
||||
cur_str_mode = next(str_modes)
|
||||
else:
|
||||
cur_str_mode = str_modes.next()
|
||||
update()
|
||||
cv2.destroyAllWindows()
|
||||
|
Reference in New Issue
Block a user