mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 07:13:02 +04:00
Drop Python2 support.
This commit is contained in:
@@ -12,14 +12,6 @@ browse.py [image filename]
|
||||
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
@@ -45,7 +37,7 @@ def main():
|
||||
|
||||
|
||||
small = img
|
||||
for _i in xrange(3):
|
||||
for _i in range(3):
|
||||
small = cv.pyrDown(small)
|
||||
|
||||
def onmouse(event, x, y, flags, param):
|
||||
|
||||
@@ -22,14 +22,6 @@ Keys:
|
||||
b - toggle back-projected probability visualization
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
@@ -68,7 +60,7 @@ class App(object):
|
||||
bin_count = self.hist.shape[0]
|
||||
bin_w = 24
|
||||
img = np.zeros((256, bin_count*bin_w, 3), np.uint8)
|
||||
for i in xrange(bin_count):
|
||||
for i in range(bin_count):
|
||||
h = int(self.hist[i])
|
||||
cv.rectangle(img, (i*bin_w+2, 255), ((i+1)*bin_w-2, 255-h), (int(180.0*i/bin_count), 255, 255), -1)
|
||||
img = cv.cvtColor(img, cv.COLOR_HSV2BGR)
|
||||
|
||||
@@ -9,21 +9,13 @@ inspired by
|
||||
http://www.mia.uni-saarland.de/Publications/weickert-dagm03.pdf
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
def coherence_filter(img, sigma = 11, str_sigma = 11, blend = 0.5, iter_n = 4):
|
||||
h, w = img.shape[:2]
|
||||
|
||||
for i in xrange(iter_n):
|
||||
for i in range(iter_n):
|
||||
print(i)
|
||||
|
||||
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
|
||||
|
||||
@@ -4,14 +4,7 @@
|
||||
This module contains some common routines used by other samples.
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
from functools import reduce
|
||||
|
||||
from functools import reduce
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
@@ -202,10 +195,7 @@ class RectSelector:
|
||||
def grouper(n, iterable, fillvalue=None):
|
||||
'''grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx'''
|
||||
args = [iter(iterable)] * n
|
||||
if PY3:
|
||||
output = it.zip_longest(fillvalue=fillvalue, *args)
|
||||
else:
|
||||
output = it.izip_longest(fillvalue=fillvalue, *args)
|
||||
output = it.zip_longest(fillvalue=fillvalue, *args)
|
||||
return output
|
||||
|
||||
def mosaic(w, imgs):
|
||||
@@ -215,10 +205,7 @@ def mosaic(w, imgs):
|
||||
imgs -- images (must have same size and format)
|
||||
'''
|
||||
imgs = iter(imgs)
|
||||
if PY3:
|
||||
img0 = next(imgs)
|
||||
else:
|
||||
img0 = imgs.next()
|
||||
img0 = next(imgs)
|
||||
pad = np.zeros_like(img0)
|
||||
imgs = it.chain([img0], imgs)
|
||||
rows = grouper(w, imgs, pad)
|
||||
|
||||
@@ -9,26 +9,18 @@ Usage:
|
||||
A trackbar is put up which controls the contour level from -3 to 3
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
def make_image():
|
||||
img = np.zeros((500, 500), np.uint8)
|
||||
black, white = 0, 255
|
||||
for i in xrange(6):
|
||||
for i in range(6):
|
||||
dx = int((i%2)*250 - 30)
|
||||
dy = int((i/2.)*150)
|
||||
|
||||
if i == 0:
|
||||
for j in xrange(11):
|
||||
for j in range(11):
|
||||
angle = (j+5)*np.pi/21
|
||||
c, s = np.cos(angle), np.sin(angle)
|
||||
x1, y1 = np.int32([dx+100+j*10-80*c, dy+100-90*s])
|
||||
|
||||
+8
-22
@@ -4,8 +4,6 @@
|
||||
Sample-launcher application.
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
|
||||
# local modules
|
||||
@@ -16,13 +14,8 @@ import webbrowser
|
||||
from glob import glob
|
||||
from subprocess import Popen
|
||||
|
||||
try:
|
||||
import tkinter as tk # Python 3
|
||||
from tkinter.scrolledtext import ScrolledText
|
||||
except ImportError:
|
||||
import Tkinter as tk # Python 2
|
||||
from ScrolledText import ScrolledText
|
||||
|
||||
import tkinter as tk
|
||||
from tkinter.scrolledtext import ScrolledText
|
||||
|
||||
#from IPython.Shell import IPShellEmbed
|
||||
#ipshell = IPShellEmbed()
|
||||
@@ -117,19 +110,12 @@ class App:
|
||||
|
||||
descr = ""
|
||||
try:
|
||||
if sys.version_info[0] > 2:
|
||||
# Python 3.x
|
||||
module_globals = {}
|
||||
module_locals = {}
|
||||
with open(fn, 'r') as f:
|
||||
module_code = f.read()
|
||||
exec(compile(module_code, fn, 'exec'), module_globals, module_locals)
|
||||
descr = module_locals.get('__doc__', 'no-description')
|
||||
else:
|
||||
# Python 2
|
||||
module_globals = {}
|
||||
execfile(fn, module_globals) # noqa: F821
|
||||
descr = module_globals.get('__doc__', 'no-description')
|
||||
module_globals = {}
|
||||
module_locals = {}
|
||||
with open(fn, 'r') as f:
|
||||
module_code = f.read()
|
||||
exec(compile(module_code, fn, 'exec'), module_globals, module_locals)
|
||||
descr = module_locals.get('__doc__', 'no-description')
|
||||
except Exception as e:
|
||||
descr = str(e)
|
||||
|
||||
|
||||
@@ -13,14 +13,6 @@ Usage:
|
||||
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
@@ -45,9 +37,9 @@ def cross_validate(model_class, params, samples, labels, kfold = 3, pool = None)
|
||||
print(".", end='')
|
||||
return score
|
||||
if pool is None:
|
||||
scores = list(map(f, xrange(kfold)))
|
||||
scores = list(map(f, range(kfold)))
|
||||
else:
|
||||
scores = pool.map(f, xrange(kfold))
|
||||
scores = pool.map(f, range(kfold))
|
||||
return np.mean(scores)
|
||||
|
||||
|
||||
@@ -108,7 +100,7 @@ class App(object):
|
||||
err = cross_validate(KNearest, dict(k=k), samples, labels)
|
||||
return k, err
|
||||
best_err, best_k = np.inf, -1
|
||||
for k, err in self.run_jobs(f, xrange(1, 9)):
|
||||
for k, err in self.run_jobs(f, range(1, 9)):
|
||||
if err < best_err:
|
||||
best_err, best_k = err, k
|
||||
print('k = %d, error: %.2f %%' % (k, err*100))
|
||||
|
||||
@@ -22,11 +22,6 @@ f - change distance function
|
||||
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 as cv
|
||||
|
||||
@@ -49,10 +44,7 @@ def sample_line(p1, p2, n, noise=0.0):
|
||||
|
||||
dist_func_names = it.cycle('DIST_L2 DIST_L1 DIST_L12 DIST_FAIR DIST_WELSCH DIST_HUBER'.split())
|
||||
|
||||
if PY3:
|
||||
cur_func_name = next(dist_func_names)
|
||||
else:
|
||||
cur_func_name = dist_func_names.next()
|
||||
cur_func_name = next(dist_func_names)
|
||||
|
||||
def update(_=None):
|
||||
noise = cv.getTrackbarPos('noise', 'fit line')
|
||||
@@ -89,10 +81,7 @@ def main():
|
||||
ch = cv.waitKey(0)
|
||||
if ch == ord('f'):
|
||||
global cur_func_name
|
||||
if PY3:
|
||||
cur_func_name = next(dist_func_names)
|
||||
else:
|
||||
cur_func_name = dist_func_names.next()
|
||||
cur_func_name = next(dist_func_names)
|
||||
if ch == 27:
|
||||
break
|
||||
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
@@ -16,7 +8,7 @@ from numpy import random
|
||||
def make_gaussians(cluster_n, img_size):
|
||||
points = []
|
||||
ref_distrs = []
|
||||
for _i in xrange(cluster_n):
|
||||
for _i in range(cluster_n):
|
||||
mean = (0.1 + 0.8*random.rand(2)) * img_size
|
||||
a = (random.rand(2, 2)-0.5)*img_size*0.1
|
||||
cov = np.dot(a.T, a) + img_size*0.05*np.eye(2)
|
||||
|
||||
@@ -15,12 +15,6 @@
|
||||
Pressing any key (except ESC) will reset the tracking.
|
||||
Pressing ESC will stop the program.
|
||||
"""
|
||||
# Python 2/3 compatibility
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
long = int
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
@@ -32,7 +26,7 @@ def main():
|
||||
img_width = 500
|
||||
kalman = cv.KalmanFilter(2, 1, 0)
|
||||
|
||||
code = long(-1)
|
||||
code = -1
|
||||
num_circle_steps = 12
|
||||
while True:
|
||||
img = np.zeros((img_height, img_width, 3), np.uint8)
|
||||
|
||||
@@ -12,14 +12,6 @@ References:
|
||||
Alexander Mordvintsev 6/10/12
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
@@ -29,7 +21,7 @@ from common import nothing, getsize
|
||||
def build_lappyr(img, leveln=6, dtype=np.int16):
|
||||
img = dtype(img)
|
||||
levels = []
|
||||
for _i in xrange(leveln-1):
|
||||
for _i in range(leveln-1):
|
||||
next_img = cv.pyrDown(img)
|
||||
img1 = cv.pyrUp(next_img, dstsize=getsize(img))
|
||||
levels.append(img-img1)
|
||||
@@ -56,14 +48,14 @@ def main():
|
||||
|
||||
leveln = 6
|
||||
cv.namedWindow('level control')
|
||||
for i in xrange(leveln):
|
||||
for i in range(leveln):
|
||||
cv.createTrackbar('%d'%i, 'level control', 5, 50, nothing)
|
||||
|
||||
while True:
|
||||
_ret, frame = cap.read()
|
||||
|
||||
pyr = build_lappyr(frame, leveln)
|
||||
for i in xrange(leveln):
|
||||
for i in range(leveln):
|
||||
v = int(cv.getTrackbarPos('%d'%i, 'level control') / 5)
|
||||
pyr[i] *= v
|
||||
res = merge_lappyr(pyr)
|
||||
|
||||
@@ -12,11 +12,6 @@ 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 as cv
|
||||
|
||||
@@ -42,12 +37,8 @@ def main():
|
||||
modes = cycle(['erode/dilate', 'open/close', 'blackhat/tophat', 'gradient'])
|
||||
str_modes = cycle(['ellipse', 'rect', 'cross'])
|
||||
|
||||
if PY3:
|
||||
cur_mode = next(modes)
|
||||
cur_str_mode = next(str_modes)
|
||||
else:
|
||||
cur_mode = modes.next()
|
||||
cur_str_mode = str_modes.next()
|
||||
cur_mode = next(modes)
|
||||
cur_str_mode = next(str_modes)
|
||||
|
||||
def update(dummy=None):
|
||||
try: # do not get trackbar position while trackbar is not created
|
||||
@@ -84,15 +75,9 @@ def main():
|
||||
if ch == 27:
|
||||
break
|
||||
if ch == ord('1'):
|
||||
if PY3:
|
||||
cur_mode = next(modes)
|
||||
else:
|
||||
cur_mode = modes.next()
|
||||
cur_mode = next(modes)
|
||||
if ch == ord('2'):
|
||||
if PY3:
|
||||
cur_str_mode = next(str_modes)
|
||||
else:
|
||||
cur_str_mode = str_modes.next()
|
||||
cur_str_mode = next(str_modes)
|
||||
update()
|
||||
|
||||
print('Done')
|
||||
|
||||
@@ -21,14 +21,6 @@ Keys:
|
||||
http://www.cs.colostate.edu/~draper/papers/bolme_cvpr10.pdf
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
from common import draw_str, RectSelector
|
||||
@@ -73,7 +65,7 @@ class MOSSE:
|
||||
self.G = cv.dft(g, flags=cv.DFT_COMPLEX_OUTPUT)
|
||||
self.H1 = np.zeros_like(self.G)
|
||||
self.H2 = np.zeros_like(self.G)
|
||||
for _i in xrange(128):
|
||||
for _i in range(128):
|
||||
a = self.preprocess(rnd_warp(img))
|
||||
A = cv.dft(a, flags=cv.DFT_COMPLEX_OUTPUT)
|
||||
self.H1 += cv.mulSpectrums(self.G, A, 0, conjB=True)
|
||||
|
||||
@@ -21,14 +21,6 @@ Keys:
|
||||
Select a textured planar object to track by drawing a box with a mouse.
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
@@ -104,7 +96,7 @@ class PlaneTracker:
|
||||
matches = [m[0] for m in matches if len(m) == 2 and m[0].distance < m[1].distance * 0.75]
|
||||
if len(matches) < MIN_MATCH_COUNT:
|
||||
return []
|
||||
matches_by_id = [[] for _ in xrange(len(self.targets))]
|
||||
matches_by_id = [[] for _ in range(len(self.targets))]
|
||||
for m in matches:
|
||||
matches_by_id[m.imgIdx].append(m)
|
||||
tracked = []
|
||||
|
||||
@@ -7,20 +7,10 @@ Usage:
|
||||
qrcode.py
|
||||
'''
|
||||
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
PY3 = sys.version_info[0] == 3
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
|
||||
class QrSample:
|
||||
def __init__(self, args):
|
||||
|
||||
@@ -6,14 +6,6 @@ Simple "Square Detector" program.
|
||||
Loads several images sequentially and tries to find squares in each image.
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
@@ -26,7 +18,7 @@ def find_squares(img):
|
||||
img = cv.GaussianBlur(img, (5, 5), 0)
|
||||
squares = []
|
||||
for gray in cv.split(img):
|
||||
for thrs in xrange(0, 255, 26):
|
||||
for thrs in range(0, 255, 26):
|
||||
if thrs == 0:
|
||||
bin = cv.Canny(gray, 0, 50, apertureSize=5)
|
||||
bin = cv.dilate(bin, None)
|
||||
@@ -38,7 +30,7 @@ def find_squares(img):
|
||||
cnt = cv.approxPolyDP(cnt, 0.02*cnt_len, True)
|
||||
if len(cnt) == 4 and cv.contourArea(cnt) > 1000 and cv.isContourConvex(cnt):
|
||||
cnt = cnt.reshape(-1, 2)
|
||||
max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
|
||||
max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in range(4)])
|
||||
if max_cos < 0.1:
|
||||
squares.append(cnt)
|
||||
return squares
|
||||
|
||||
@@ -7,14 +7,6 @@ Multiscale Turing Patterns generator
|
||||
Inspired by http://www.jonathanmccabe.com/Cyclic_Symmetric_Multi-Scale_Turing_Patterns.pdf
|
||||
'''
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
xrange = range
|
||||
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
from common import draw_str
|
||||
@@ -45,7 +37,7 @@ def main():
|
||||
|
||||
def process_scale(a_lods, lod):
|
||||
d = a_lods[lod] - cv.pyrUp(a_lods[lod+1])
|
||||
for _i in xrange(lod):
|
||||
for _i in range(lod):
|
||||
d = cv.pyrUp(d)
|
||||
v = cv.GaussianBlur(d*d, (3, 3), 0)
|
||||
return np.sign(d), v
|
||||
@@ -53,10 +45,10 @@ def main():
|
||||
scale_num = 6
|
||||
for frame_i in count():
|
||||
a_lods = [a]
|
||||
for i in xrange(scale_num):
|
||||
for i in range(scale_num):
|
||||
a_lods.append(cv.pyrDown(a_lods[-1]))
|
||||
ms, vs = [], []
|
||||
for i in xrange(1, scale_num):
|
||||
for i in range(1, scale_num):
|
||||
m, v = process_scale(a_lods, i)
|
||||
ms.append(m)
|
||||
vs.append(v)
|
||||
|
||||
Reference in New Issue
Block a user