1
1
# -*- coding: utf-8 -*-
2
2
3
+ '''
4
+ 图像 字塔的一个应用是图像 合。例如 在图像缝合中 你 将两幅 图叠在一 但是由于 接区域图像像素的不 续性 整幅图的效果看 来会 很差。 时图像 字塔就可以排上用场了 他可以帮你实现无缝 接。 的 一个经典案例就是将两个水果 合成一个 看看下图也 你就明白我在 什么 了。
5
+
6
+
7
+ 实现上 效果的步 如下
8
+ 1. 入两幅图像 苹果和句子
9
+ 2. 构建苹果和橘子的 斯 字塔 6 层
10
+ 3. 根据 斯 字塔 算拉普拉斯 字塔
11
+ 4. 在拉普拉斯的每一层 图像 合 苹果的左 与橘子的右 合 5. 根据 合后的图像 字塔 建原始图像。
12
+ '''
13
+
3
14
import cv2
4
- import numpy as np ,sys
15
+ import numpy as np , sys
16
+
5
17
A = cv2 .imread ('apple.jpg' )
6
18
B = cv2 .imread ('orange.jpg' )
19
+
7
20
# generate Gaussian pyramid for A
8
21
G = A .copy ()
9
22
gpA = [G ]
10
- for i in xrange (6 ):
23
+ for i in range (6 ):
11
24
G = cv2 .pyrDown (G )
12
25
gpA .append (G )
26
+
13
27
# generate Gaussian pyramid for B
14
28
G = B .copy ()
15
29
gpB = [G ]
16
- for i in xrange (6 ):
30
+ for i in range (6 ):
17
31
G = cv2 .pyrDown (G )
18
32
gpB .append (G )
33
+
19
34
# generate Laplacian Pyramid for A
20
35
lpA = [gpA [5 ]]
21
- for i in xrange (5 ,0 , - 1 ):
36
+ for i in range (5 , 0 , - 1 ):
22
37
GE = cv2 .pyrUp (gpA [i ])
23
- L = cv2 .subtract (gpA [i - 1 ],GE )
38
+ L = cv2 .subtract (gpA [i - 1 ], GE )#TODO error
24
39
lpA .append (L )
25
40
26
41
# generate Laplacian Pyramid for B
27
42
lpB = [gpB [5 ]]
28
- for i in xrange (5 ,0 , - 1 ):
43
+ for i in range (5 , 0 , - 1 ):
29
44
GE = cv2 .pyrUp (gpB [i ])
30
- L = cv2 .subtract (gpB [i - 1 ],GE )
45
+ L = cv2 .subtract (gpB [i - 1 ], GE )
31
46
lpB .append (L )
47
+
32
48
# Now add left and right halves of images in each level
33
- #numpy.hstack(tup)
34
- #Take a sequence of arrays and stack them horizontally
35
- #to make a single array.
49
+ # numpy.hstack(tup)
50
+ # Take a sequence of arrays and stack them horizontally
51
+ # to make a single array.
36
52
LS = []
37
- for la ,lb in zip (lpA ,lpB ):
38
- rows ,cols ,dpt = la .shape
39
- ls = np .hstack ((la [:,0 :cols / 2 ], lb [:,cols / 2 :]))
53
+ for la , lb in zip (lpA , lpB ):
54
+ rows , cols , dpt = la .shape
55
+ ls = np .hstack ((la [:, 0 :cols / 2 ], lb [:, cols / 2 :]))
40
56
LS .append (ls )
57
+
41
58
# now reconstruct
42
59
ls_ = LS [0 ]
43
- for i in xrange (1 ,6 ):
60
+ for i in range (1 , 6 ):
44
61
ls_ = cv2 .pyrUp (ls_ )
45
62
ls_ = cv2 .add (ls_ , LS [i ])
46
63
47
64
# image with direct connecting each half
48
- real = np .hstack ((A [:,:cols / 2 ],B [:,cols / 2 :]))
65
+ real = np .hstack ((A [:, :cols / 2 ], B [:, cols / 2 :]))
49
66
50
- cv2 .imwrite ('Pyramid_blending2.jpg' ,ls_ )
51
- cv2 .imwrite ('Direct_blending.jpg' ,real )
67
+ cv2 .imwrite ('Pyramid_blending2.jpg' , ls_ )
68
+ cv2 .imwrite ('Direct_blending.jpg' , real )
0 commit comments