Skip to content

Commit fa14d6c

Browse files
author
CodeSaint98
committed
bug fixes
1 parent fb091c0 commit fa14d6c

File tree

5 files changed

+224
-65
lines changed

5 files changed

+224
-65
lines changed

Watermarking .ipynb

Lines changed: 186 additions & 50 deletions
Large diffs are not rendered by default.

main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from src import *
22

3-
gx, gy, gxlist, gylist = estimate_watermark('images/fotolia_processed')
3+
gx, gy, gxlist, gylist = estimate_watermark('Resized')
44

55
# est = poisson_reconstruct(gx, gy, np.zeros(gx.shape)[:,:,0])
66
cropped_gx, cropped_gy = crop_watermark(gx, gy)
77
W_m = poisson_reconstruct(cropped_gx, cropped_gy)
88

99
# random photo
10-
img = cv2.imread('images/fotolia_processed/fotolia_137840645.jpg')
10+
img = cv2.imread('Resized/image0000.jpg')
1111
im, start, end = watermark_detector(img, cropped_gx, cropped_gy)
1212

1313
# plt.imshow(im)
@@ -16,7 +16,7 @@
1616
# W_m is the cropped watermark
1717
num_images = len(gxlist)
1818

19-
J, img_paths = get_cropped_images('images/fotolia_processed', num_images, start, end, cropped_gx.shape)
19+
J, img_paths = get_cropped_images('Resized', num_images, start, end, cropped_gx.shape)
2020
# get a random subset of J
2121
idx = [389, 144, 147, 468, 423, 92, 3, 354, 196, 53, 470, 445, 314, 349, 105, 366, 56, 168, 351, 15, 465, 368, 90, 96, 202, 54, 295, 137, 17, 79, 214, 413, 454, 305, 187, 4, 458, 330, 290, 73, 220, 118, 125, 180, 247, 243, 257, 194, 117, 320, 104, 252, 87, 95, 228, 324, 271, 398, 334, 148, 425, 190, 78, 151, 34, 310, 122, 376, 102, 260]
2222
idx = idx[:25]
@@ -29,13 +29,13 @@
2929
C, est_Ik = estimate_blend_factor(J, Wm, alph)
3030

3131
alpha = alph.copy()
32-
for i in xrange(3):
32+
for i in range(3):
3333
alpha[:,:,i] = C[i]*alpha[:,:,i]
3434

3535
Wm = Wm + alpha*est_Ik
3636

3737
W = Wm.copy()
38-
for i in xrange(3):
38+
for i in range(3):
3939
W[:,:,i]/=C[i]
4040

4141
Jt = J[:25]

resize.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os,sys
2+
from cv2 import cv2
3+
import glob
4+
from PIL import Image
5+
6+
os.mkdir('Resized')
7+
path = "wildehm/"
8+
dirs = os.listdir( path )
9+
10+
def resize():
11+
i=0
12+
for item in dirs:
13+
if os.path.isfile(path+item):
14+
image = cv2.imread(path+item)
15+
imgResized = cv2.resize(image, (800,500))
16+
cv2.imwrite("Resized/image%04i.jpg" %i, imgResized)
17+
i +=1
18+
cv2.imshow('image', imgResized)
19+
cv2.waitKey(30)
20+
cv2.destroyAllWindows()
21+
22+
resize()
23+

src/estimate_watermark.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def estimate_watermark(foldername):
3131

3232
# Compute gradients
3333
print("Computing gradients.")
34-
gradx = map(lambda x: cv2.Sobel(x, cv2.CV_64F, 1, 0, ksize=KERNEL_SIZE), images)
35-
grady = map(lambda x: cv2.Sobel(x, cv2.CV_64F, 0, 1, ksize=KERNEL_SIZE), images)
34+
gradx = list(map(lambda x: cv2.Sobel(x, cv2.CV_64F, 1, 0, ksize=KERNEL_SIZE), images))
35+
grady = list(map(lambda x: cv2.Sobel(x, cv2.CV_64F, 0, 1, ksize=KERNEL_SIZE), images))
3636

3737
# Compute median of grads
3838
print("Computing median gradients.")
@@ -114,7 +114,7 @@ def poisson_reconstruct(gradx, grady, kernel_size=KERNEL_SIZE, num_iters=100, h=
114114
est[1:-1, 1:-1, :] = np.random.random((m-2, n-2, p))
115115
loss = []
116116

117-
for i in xrange(num_iters):
117+
for i in range(num_iters):
118118
old_est = est.copy()
119119
est[1:-1, 1:-1, :] = 0.25*(est[0:-2, 1:-1, :] + est[1:-1, 0:-2, :] + est[2:, 1:-1, :] + est[1:-1, 2:, :] - h*h*laplacian[1:-1, 1:-1, :])
120120
error = np.sum(np.square(est-old_est))
@@ -176,7 +176,7 @@ def watermark_detector(img, gx, gy, thresh_low=200, thresh_high=220, printval=Fa
176176
if printval:
177177
print(index)
178178

179-
x,y = (index[0]-rect[0]/2), (index[1]-rect[1]/2)
179+
x,y = (index[0]-rect[0]//2), (index[1]-rect[1]//2)
180180
im = img.copy()
181181
cv2.rectangle(im, (y, x), (y+rect[1], x+rect[0]), (255, 0, 0))
182182
return (im, (x, y), (rect[0], rect[1]))

src/watermark_reconstruct.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def estimate_normalized_alpha(J, W_m, num_images=30, threshold=170, invert=False
127127

128128
print("Estimating normalized alpha using %d images."%(num_images))
129129
# for all images, calculate alpha
130-
for idx in xrange(num_images):
130+
for idx in range(num_images):
131131
imgcopy = thr
132132
alph = closed_form_matte(J[idx], imgcopy)
133133
alpha[idx] = alph
@@ -141,7 +141,7 @@ def estimate_blend_factor(J, W_m, alph, threshold=0.01*255):
141141
gx_jm = np.zeros(J.shape)
142142
gy_jm = np.zeros(J.shape)
143143

144-
for i in xrange(K):
144+
for i in range(K):
145145
gx_jm[i] = cv2.Sobel(Jm[i], cv2.CV_64F, 1, 0, 3)
146146
gy_jm[i] = cv2.Sobel(Jm[i], cv2.CV_64F, 0, 1, 3)
147147

@@ -153,7 +153,7 @@ def estimate_blend_factor(J, W_m, alph, threshold=0.01*255):
153153
estIk_grad = np.sqrt(gx_estIk**2 + gy_estIk**2)
154154

155155
C = []
156-
for i in xrange(3):
156+
for i in range(3):
157157
c_i = np.sum(Jm_grad[:,:,:,i]*estIk_grad[:,:,i])/np.sum(np.square(estIk_grad[:,:,i]))/K
158158
print(c_i)
159159
C.append(c_i)
@@ -181,15 +181,15 @@ def solve_images(J, W_m, alpha, W_init, gamma=1, beta=1, lambda_w=0.005, lambda_
181181
sobely = get_ySobel_matrix(m, n, p)
182182
Ik = np.zeros(J.shape)
183183
Wk = np.zeros(J.shape)
184-
for i in xrange(K):
184+
for i in range(K):
185185
Ik[i] = J[i] - W_m
186186
Wk[i] = W_init.copy()
187187

188188
# This is for median images
189189
W = W_init.copy()
190190

191191
# Iterations
192-
for _ in xrange(iters):
192+
for _ in range(iters):
193193

194194
print("------------------------------------")
195195
print("Iteration: %d"%(_))
@@ -208,7 +208,7 @@ def solve_images(J, W_m, alpha, W_init, gamma=1, beta=1, lambda_w=0.005, lambda_
208208
alpha_diag = diags(alpha.reshape(-1))
209209
alpha_bar_diag = diags((1-alpha).reshape(-1))
210210

211-
for i in xrange(K):
211+
for i in range(K):
212212
# prep vars
213213
Wkx = cv2.Sobel(Wk[i], cv2.CV_64F, 1, 0, 3)
214214
Wky = cv2.Sobel(Wk[i], cv2.CV_64F, 0, 1, 3)

0 commit comments

Comments
 (0)