Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeSaint98 committed Nov 3, 2020
1 parent fb091c0 commit fa14d6c
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 65 deletions.
236 changes: 186 additions & 50 deletions Watermarking .ipynb

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from src import *

gx, gy, gxlist, gylist = estimate_watermark('images/fotolia_processed')
gx, gy, gxlist, gylist = estimate_watermark('Resized')

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

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

# plt.imshow(im)
Expand All @@ -16,7 +16,7 @@
# W_m is the cropped watermark
num_images = len(gxlist)

J, img_paths = get_cropped_images('images/fotolia_processed', num_images, start, end, cropped_gx.shape)
J, img_paths = get_cropped_images('Resized', num_images, start, end, cropped_gx.shape)
# get a random subset of J
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]
idx = idx[:25]
Expand All @@ -29,13 +29,13 @@
C, est_Ik = estimate_blend_factor(J, Wm, alph)

alpha = alph.copy()
for i in xrange(3):
for i in range(3):
alpha[:,:,i] = C[i]*alpha[:,:,i]

Wm = Wm + alpha*est_Ik

W = Wm.copy()
for i in xrange(3):
for i in range(3):
W[:,:,i]/=C[i]

Jt = J[:25]
Expand Down
23 changes: 23 additions & 0 deletions resize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os,sys
from cv2 import cv2
import glob
from PIL import Image

os.mkdir('Resized')
path = "wildehm/"
dirs = os.listdir( path )

def resize():
i=0
for item in dirs:
if os.path.isfile(path+item):
image = cv2.imread(path+item)
imgResized = cv2.resize(image, (800,500))
cv2.imwrite("Resized/image%04i.jpg" %i, imgResized)
i +=1
cv2.imshow('image', imgResized)
cv2.waitKey(30)
cv2.destroyAllWindows()

resize()

8 changes: 4 additions & 4 deletions src/estimate_watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def estimate_watermark(foldername):

# Compute gradients
print("Computing gradients.")
gradx = map(lambda x: cv2.Sobel(x, cv2.CV_64F, 1, 0, ksize=KERNEL_SIZE), images)
grady = map(lambda x: cv2.Sobel(x, cv2.CV_64F, 0, 1, ksize=KERNEL_SIZE), images)
gradx = list(map(lambda x: cv2.Sobel(x, cv2.CV_64F, 1, 0, ksize=KERNEL_SIZE), images))
grady = list(map(lambda x: cv2.Sobel(x, cv2.CV_64F, 0, 1, ksize=KERNEL_SIZE), images))

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

for i in xrange(num_iters):
for i in range(num_iters):
old_est = est.copy()
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, :])
error = np.sum(np.square(est-old_est))
Expand Down Expand Up @@ -176,7 +176,7 @@ def watermark_detector(img, gx, gy, thresh_low=200, thresh_high=220, printval=Fa
if printval:
print(index)

x,y = (index[0]-rect[0]/2), (index[1]-rect[1]/2)
x,y = (index[0]-rect[0]//2), (index[1]-rect[1]//2)
im = img.copy()
cv2.rectangle(im, (y, x), (y+rect[1], x+rect[0]), (255, 0, 0))
return (im, (x, y), (rect[0], rect[1]))
12 changes: 6 additions & 6 deletions src/watermark_reconstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def estimate_normalized_alpha(J, W_m, num_images=30, threshold=170, invert=False

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

for i in xrange(K):
for i in range(K):
gx_jm[i] = cv2.Sobel(Jm[i], cv2.CV_64F, 1, 0, 3)
gy_jm[i] = cv2.Sobel(Jm[i], cv2.CV_64F, 0, 1, 3)

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

C = []
for i in xrange(3):
for i in range(3):
c_i = np.sum(Jm_grad[:,:,:,i]*estIk_grad[:,:,i])/np.sum(np.square(estIk_grad[:,:,i]))/K
print(c_i)
C.append(c_i)
Expand Down Expand Up @@ -181,15 +181,15 @@ def solve_images(J, W_m, alpha, W_init, gamma=1, beta=1, lambda_w=0.005, lambda_
sobely = get_ySobel_matrix(m, n, p)
Ik = np.zeros(J.shape)
Wk = np.zeros(J.shape)
for i in xrange(K):
for i in range(K):
Ik[i] = J[i] - W_m
Wk[i] = W_init.copy()

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

# Iterations
for _ in xrange(iters):
for _ in range(iters):

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

for i in xrange(K):
for i in range(K):
# prep vars
Wkx = cv2.Sobel(Wk[i], cv2.CV_64F, 1, 0, 3)
Wky = cv2.Sobel(Wk[i], cv2.CV_64F, 0, 1, 3)
Expand Down

0 comments on commit fa14d6c

Please sign in to comment.