Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Draws objects on videos.
4"""
5from cv2 import blur as cv_blur, rectangle as cv_rectangle # pylint: disable=E0401
8def blur(img, p1, p2, frac=0.333, kernel_size=None):
9 """
10 Blurs a part of a picture.
11 Uses `blur <https://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html?highlight=blur#blur>`_.
13 @param img image (:epkg:`numpy:array`)
14 @param p1 (x1,y1)
15 @param p2 (x2, y2)
16 @param frac if not None, if *kernel_size* is equal to this fraction
17 of the original frame
18 @param kernel_size kernel size for the bluring (wins over *frac*)
20 It modifies the original picture.
21 """
22 h, w, _ = img.shape
23 x1, y1 = p1
24 x2, y2 = p2
25 x1, x2 = max(0, x1), min(x2, w)
26 y1, y2 = max(0, y1), min(y2, h)
27 dx, dy = x2 - x1, y2 - y1
29 if kernel_size is not None:
30 blur_size = kernel_size
31 else:
32 blur_size = (int(frac * dx), int(frac * dy))
33 orig = img[y1:y2, x1:x2]
34 zone = cv_blur(orig, blur_size)
35 img[y1:y2, x1:x2] = zone
38def rectangle(img, p1, p2, color=(255, 255, 0)):
39 """
40 Draws a rectangle.
41 Uses `blur <https://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html?highlight=blur#blur>`_.
43 @param img image (:epkg:`numpy:array`)
44 @param p1 (x1,y1)
45 @param p2 (x2, y2)
46 @param kernel_size kernel size for the bluring.
48 It modifies the original picture.
49 """
50 h, w, _ = img.shape
51 x1, y1 = p1
52 x2, y2 = p2
53 x1, x2 = max(0, x1), min(x2, w)
54 y1, y2 = max(0, y1), min(y2, h)
55 cv_rectangle(img, (x1, y1), (x2, y2), color)