2013-03-06 14:41:02 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-24 02:57:22 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
''' An example of Laplacian Pyramid construction and merging.
|
|
|
|
|
|
|
|
Level : Intermediate
|
|
|
|
|
|
|
|
Usage : python lappyr.py [<video source>]
|
|
|
|
|
|
|
|
References:
|
|
|
|
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.54.299
|
|
|
|
|
|
|
|
Alexander Mordvintsev 6/10/12
|
|
|
|
'''
|
|
|
|
|
2015-09-14 00:00:22 +08:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
PY3 = sys.version_info[0] == 3
|
|
|
|
|
|
|
|
if PY3:
|
|
|
|
xrange = range
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
import numpy as np
|
2017-12-11 17:55:03 +08:00
|
|
|
import cv2 as cv
|
2019-03-20 02:03:58 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
import video
|
|
|
|
from common import nothing, getsize
|
|
|
|
|
|
|
|
def build_lappyr(img, leveln=6, dtype=np.int16):
|
|
|
|
img = dtype(img)
|
|
|
|
levels = []
|
2017-08-25 00:45:14 +08:00
|
|
|
for _i in xrange(leveln-1):
|
2017-12-11 17:55:03 +08:00
|
|
|
next_img = cv.pyrDown(img)
|
|
|
|
img1 = cv.pyrUp(next_img, dstsize=getsize(img))
|
2012-10-17 07:18:30 +08:00
|
|
|
levels.append(img-img1)
|
|
|
|
img = next_img
|
|
|
|
levels.append(img)
|
|
|
|
return levels
|
|
|
|
|
|
|
|
def merge_lappyr(levels):
|
|
|
|
img = levels[-1]
|
|
|
|
for lev_img in levels[-2::-1]:
|
2017-12-11 17:55:03 +08:00
|
|
|
img = cv.pyrUp(img, dstsize=getsize(lev_img))
|
2012-10-17 07:18:30 +08:00
|
|
|
img += lev_img
|
|
|
|
return np.uint8(np.clip(img, 0, 255))
|
|
|
|
|
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
def main():
|
2012-10-17 07:18:30 +08:00
|
|
|
import sys
|
|
|
|
|
2013-03-06 14:41:02 +08:00
|
|
|
try:
|
|
|
|
fn = sys.argv[1]
|
|
|
|
except:
|
|
|
|
fn = 0
|
2012-10-17 07:18:30 +08:00
|
|
|
cap = video.create_capture(fn)
|
|
|
|
|
|
|
|
leveln = 6
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.namedWindow('level control')
|
2012-10-17 07:18:30 +08:00
|
|
|
for i in xrange(leveln):
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.createTrackbar('%d'%i, 'level control', 5, 50, nothing)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
while True:
|
|
|
|
ret, frame = cap.read()
|
|
|
|
|
|
|
|
pyr = build_lappyr(frame, leveln)
|
|
|
|
for i in xrange(leveln):
|
2017-12-11 17:55:03 +08:00
|
|
|
v = int(cv.getTrackbarPos('%d'%i, 'level control') / 5)
|
2012-10-17 07:18:30 +08:00
|
|
|
pyr[i] *= v
|
|
|
|
res = merge_lappyr(pyr)
|
|
|
|
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.imshow('laplacian pyramid filter', res)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2017-12-11 17:55:03 +08:00
|
|
|
if cv.waitKey(1) == 27:
|
2012-10-17 07:18:30 +08:00
|
|
|
break
|
2019-03-20 02:03:58 +08:00
|
|
|
|
|
|
|
print('Done')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print(__doc__)
|
|
|
|
main()
|
|
|
|
cv.destroyAllWindows()
|