• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3''' An example of Laplacian Pyramid construction and merging.
4
5Level : Intermediate
6
7Usage : python lappyr.py [<video source>]
8
9References:
10  http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.54.299
11
12Alexander Mordvintsev 6/10/12
13'''
14
15import numpy as np
16import cv2
17import video
18from common import nothing, getsize
19
20def build_lappyr(img, leveln=6, dtype=np.int16):
21    img = dtype(img)
22    levels = []
23    for i in xrange(leveln-1):
24        next_img = cv2.pyrDown(img)
25        img1 = cv2.pyrUp(next_img, dstsize=getsize(img))
26        levels.append(img-img1)
27        img = next_img
28    levels.append(img)
29    return levels
30
31def merge_lappyr(levels):
32    img = levels[-1]
33    for lev_img in levels[-2::-1]:
34        img = cv2.pyrUp(img, dstsize=getsize(lev_img))
35        img += lev_img
36    return np.uint8(np.clip(img, 0, 255))
37
38
39if __name__ == '__main__':
40    import sys
41    print __doc__
42
43    try:
44        fn = sys.argv[1]
45    except:
46        fn = 0
47    cap = video.create_capture(fn)
48
49    leveln = 6
50    cv2.namedWindow('level control')
51    for i in xrange(leveln):
52        cv2.createTrackbar('%d'%i, 'level control', 5, 50, nothing)
53
54    while True:
55        ret, frame = cap.read()
56
57        pyr = build_lappyr(frame, leveln)
58        for i in xrange(leveln):
59            v = cv2.getTrackbarPos('%d'%i, 'level control') / 5
60            pyr[i] *= v
61        res = merge_lappyr(pyr)
62
63        cv2.imshow('laplacian pyramid filter', res)
64
65        if cv2.waitKey(1) & 0xFF == 27:
66            break
67