Skip to content

Commit d9efabc

Browse files
committed
L20
1 parent 0de2413 commit d9efabc

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

L20_image_blending.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,62 @@
11
import cv2, numpy as np
22

3+
# Goal: blend (merge), half of the apple with half of the orange
4+
35
apple = cv2.imread('./opencv/samples/data/apple.jpg')
46
orange = cv2.imread('./opencv/samples/data/orange.jpg')
57

68
print(apple.shape)
79
print(orange.shape)
810

11+
# Metodo base (no blending): unisce le immagini come matrici (tupla)
12+
apple_orange = np.hstack((apple[:,:256], orange[:,256:]))
13+
14+
# 1) Generate Gaussian pyramids
15+
apple_copy = apple.copy()
16+
gp_apple = [apple_copy]
17+
for i in range(6):
18+
apple_copy = cv2.pyrDown(apple_copy)
19+
gp_apple.append(apple_copy)
20+
21+
orange_copy = orange.copy()
22+
gp_orange = [orange_copy]
23+
for i in range(6):
24+
orange_copy = cv2.pyrDown(orange_copy)
25+
gp_orange.append(orange_copy)
26+
27+
# 2) Generate Laplacian pyramids
28+
apple_copy = gp_apple[5]
29+
lp_apple = [apple_copy]
30+
for i in range(5,0,-1):
31+
gaussian_extended = cv2.pyrUp(gp_apple[i])
32+
laplacian = cv2.subtract(gp_apple[i-1], gaussian_extended)
33+
lp_apple.append(laplacian)
34+
35+
orange_copy = gp_orange[5]
36+
lp_orange = [orange_copy]
37+
for i in range(5, 0, -1):
38+
gaussian_extended = cv2.pyrUp(gp_orange[i])
39+
laplacian = cv2.subtract(gp_orange[i-1], gaussian_extended)
40+
lp_orange.append(laplacian)
41+
42+
# 3) Join the halves of images in each level
43+
apple_orange_pyramid = []
44+
n = 0
45+
for apple_lap, orange_lap in zip(lp_apple, lp_orange):
46+
n += 1
47+
cols, rows, ch = apple_lap.shape
48+
laplacian = np.hstack((apple_lap[:,0:int(cols/2)], orange_lap[:,int(cols/2):]))
49+
apple_orange_pyramid.append(laplacian)
50+
51+
# 4) Reconstruction of the image
52+
apple_orange_reconstruct = apple_orange_pyramid[0]
53+
for i in range(1,6):
54+
apple_orange_reconstruct = cv2.pyrUp(apple_orange_reconstruct)
55+
apple_orange_reconstruct = cv2.add(apple_orange_pyramid[i], apple_orange_reconstruct)
956

1057
cv2.imshow('Apple', apple)
1158
cv2.imshow('Orange', orange)
59+
cv2.imshow('Apple_Orange', apple_orange)
60+
cv2.imshow('Apple_Orange_Reconstruct', apple_orange_reconstruct)
1261
cv2.waitKey(0)
13-
cv2.destroyAllWindows()
62+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)