36 This module contains some common routines used by other samples.
40 from __future__
import print_function
42 PY3 = sys.version_info[0] == 3
45 from functools
import reduce
52 import itertools
as it
53 from contextlib
import contextmanager
55 image_extensions = [
'.bmp',
'.jpg',
'.jpeg',
'.png',
'.tif',
'.tiff',
'.pbm',
'.pgm',
'.ppm']
59 self.__dict__.update(kw)
61 return str(self.__dict__)
64 path, fn = os.path.split(fn)
65 name, ext = os.path.splitext(fn)
66 return path, name, ext
71 return np.sqrt(
anorm2(a) )
74 xs = H[0, 0]*x + H[0, 1]*y + H[0, 2]
75 ys = H[1, 0]*x + H[1, 1]*y + H[1, 2]
76 s = H[2, 0]*x + H[2, 1]*y + H[2, 2]
82 a = (0, 0, a[0], a[1])
83 return np.array(a, np.float64).reshape(2, 2)
87 cx, cy = (dst[1] - dst[0]) / (src[1] - src[0])
88 tx, ty = dst[0] - src[0] * (cx, cy)
89 M = np.float64([[ cx, 0, tx],
95 def lookat(eye, target, up = (0, 0, 1)):
96 fwd = np.asarray(target, np.float64) - eye
98 right = np.cross(fwd, up)
100 down = np.cross(fwd, right)
101 R = np.float64([right, down, fwd])
102 tvec = -np.dot(R, eye)
106 w, u, vt = cv.SVDecomp(R - np.eye(3))
107 p = vt[0] + u[:,0]*w[0]
110 axis = np.cross(vt[0], vt[1])
111 return axis * np.arctan2(s, c)
115 cv.putText(dst, s, (x+1, y+1), cv.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness = 2, lineType=cv.LINE_AA)
116 cv.putText(dst, s, (x, y), cv.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255), lineType=cv.LINE_AA)
119 def __init__(self, windowname, dests, colors_func):
133 if event == cv.EVENT_LBUTTONDOWN:
135 elif event == cv.EVENT_LBUTTONUP:
138 if self.
prev_ptprev_pt
and flags & cv.EVENT_FLAG_LBUTTON:
140 cv.line(dst, self.
prev_ptprev_pt, pt, color, 5)
141 self.
dirtydirty =
True
147 _jet_data = {
'red': ((0., 0, 0), (0.35, 0, 0), (0.66, 1, 1), (0.89,1, 1),
149 'green': ((0., 0, 0), (0.125,0, 0), (0.375,1, 1), (0.64,1, 1),
150 (0.91,0,0), (1, 0, 0)),
151 'blue': ((0., 0.5, 0.5), (0.11, 1, 1), (0.34, 1, 1), (0.65,0, 0),
154 cmap_data = {
'jet' : _jet_data }
157 data = cmap_data[name]
158 xs = np.linspace(0.0, 1.0, n)
161 for ch_name
in [
'blue',
'green',
'red']:
162 ch_data = data[ch_name]
164 for x, y1, y2
in ch_data:
167 ch = np.interp(xs, xp, yp)
169 return np.uint8(np.array(channels).T*255)
175 return cv.getTickCount() / cv.getTickFrequency()
184 print(
"%.2f ms" % ((
clock()-start)*1000))
191 if self.
valuevalue
is None:
195 self.
valuevalue = c * self.
valuevalue + (1.0-c) * v
201 cv.setMouseCallback(win, self.
onmouseonmouse)
205 x, y = np.int16([x, y])
206 if event == cv.EVENT_LBUTTONDOWN:
210 if flags & cv.EVENT_FLAG_LBUTTON:
212 x0, y0 = np.minimum([xo, yo], [x, y])
213 x1, y1 = np.maximum([xo, yo], [x, y])
215 if x1-x0 > 0
and y1-y0 > 0:
216 self.
drag_rectdrag_rect = (x0, y0, x1, y1)
227 cv.rectangle(vis, (x0, y0), (x1, y1), (0, 255, 0), 2)
231 return self.
drag_rectdrag_rect
is not None
235 '''grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx'''
236 args = [iter(iterable)] * n
238 output = it.zip_longest(fillvalue=fillvalue, *args)
240 output = it.izip_longest(fillvalue=fillvalue, *args)
244 '''Make a grid from images.
246 w -- number of grid columns
247 imgs -- images (must have same size and format)
254 pad = np.zeros_like(img0)
255 imgs = it.chain([img0], imgs)
257 return np.vstack(map(np.hstack, rows))
264 return reduce(np.dot, args)
269 cv.circle(vis, (
int(x),
int(y)), 2, color)
def __init__(self, win, callback)
def onmouse(self, event, x, y, flags, param)
def on_mouse(self, event, x, y, flags, param)
def __init__(self, windowname, dests, colors_func)
def __init__(self, smooth_coef=0.5)
def draw_str(dst, target, s)
def make_cmap(name, n=256)
def grouper(n, iterable, fillvalue=None)
def rect2rect_mtx(src, dst)
def draw_keypoints(vis, keypoints, color=(0, 255, 255))
def lookat(eye, target, up=(0, 0, 1))