Skip to content

Commit 7674ff7

Browse files
committed
project modules created
1 parent f69d19d commit 7674ff7

13 files changed

+228
-60
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
images/
22
Dekel_*
33

4+
*.pyc

estimate_watermark.py

-59
This file was deleted.

main.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from src import *
2+
3+
gx, gy, gxlist, gylist = estimate_watermark('images/fotolia_processed')
4+
est, loss = poisson_reconstruct(gx, gy)
5+
cropped_gx, cropped_gy = crop_watermark(gx, gy)
6+
7+
plt.imshow(PlotImage(est))
8+
9+
est2, _ = poisson_reconstruct(cropped_gx, cropped_gy)
10+
plt.figure()
11+
plt.imshow(PlotImage(est2))
12+
plt.show()

recover.txt

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# coding: utf-8
2+
3+
foldername = "images/fotolia_processed"
4+
images = []
5+
for r, dirs, files in os.walk(foldername):
6+
# Get all the images
7+
for file in files:
8+
img = cv2.imread(os.sep.join([r, file]))
9+
if img is not None:
10+
images.append(img)
11+
else:
12+
print("%s not found."%(file))
13+
import os, sys
14+
import sys, os
15+
import cv2
16+
import numpy as np
17+
import warnings
18+
from matplotlib import pyplot as plt
19+
images = []
20+
for r, dirs, files in os.walk(foldername):
21+
# Get all the images
22+
for file in files:
23+
img = cv2.imread(os.sep.join([r, file]))
24+
if img is not None:
25+
images.append(img)
26+
else:
27+
print("%s not found."%(file))
28+
images
29+
# Compute gradients
30+
print("Computing gradients.")
31+
gradx = map(lambda x: cv2.Sobel(x, cv2.CV_64F, 1, 0, ksize=KERNEL_SIZE), images)
32+
grady = map(lambda x: cv2.Sobel(x, cv2.CV_64F, 0, 1, ksize=KERNEL_SIZE), images)
33+
# Variables
34+
KERNEL_SIZE = 3
35+
MIN_VAL = 250
36+
MAX_VAL = 255
37+
THRESHOLD = 0.4
38+
# Compute gradients
39+
print("Computing gradients.")
40+
gradx = map(lambda x: cv2.Sobel(x, cv2.CV_64F, 1, 0, ksize=KERNEL_SIZE), images)
41+
grady = map(lambda x: cv2.Sobel(x, cv2.CV_64F, 0, 1, ksize=KERNEL_SIZE), images)
42+
43+
# Compute median of grads
44+
print("Computing median gradients.")
45+
Wm_x = np.median(np.array(gradx), axis=0)
46+
Wm_y = np.median(np.array(grady), axis=0)
47+
def PlotImage(img):
48+
return (img - np.min(img))/(np.max(img)-np.min(img))
49+
plt.imshow(PlotImage(Wm_x))
50+
plt.show()
51+
uxx = cv2.Sobel(Wm_x, cv2.CV_64F, 1, 0, ksize=KERNEL_SIZE)
52+
plt.imshow(PlotImage(uxx))
53+
plt.show()
54+
uyy = cv2.Sobel(Wm_y, cv2.CV_64F, 0, 1, ksize=KERNEL_SIZE)
55+
laplacian = uxx + uyy
56+
f
57+
f = laplacian
58+
h = 0.1
59+
f
60+
plt.imshow(PlotImage(f))
61+
plt.show()
62+
f.shape
63+
uu
64+
u
65+
u = np.zeros(images[0].shape)
66+
l
67+
l = []
68+
tmp = np.random.randn(f.shape)
69+
tmp = np.random.random(f.shape)
70+
tmp
71+
tmp.shape
72+
tmp[1,-1,:]=0
73+
tmp
74+
tmp = np.random.random(f.shape)
75+
tmp[1,:,:]=0
76+
tmp
77+
tmp[-1,:,:]=0
78+
tmp
79+
tmp = np.random.random(f.shape)
80+
tmp[0,:,:]=0
81+
tmp[-1,:,:]=0
82+
tmp[:,0,:]=0
83+
tmp[:,-1,:]=0
84+
tmp
85+
plt.imshow(PlotImage(tmp))
86+
plt.show()
87+
l.append(tmp)
88+
for i in xrange(20):
89+
print(i)
90+
tmp[1:-1, 1:-1, :] = 0.25*(tmp[0:-2, 1:-1, :] + tmp[1:-1, 0:-2, :] + tmp[2:, 1:-1, :] + tmp[1:-1, 2:, :] - h*h*f[1:-1, 1:-1, :])
91+
l.append(tmp)
92+
93+
for i in xrange(20):
94+
plt.imshow(PlotImage(l[i]))
95+
plt.figure()
96+
97+
plt.show()
98+
get_ipython().magic(u'save recover')
99+
get_ipython().magic(u'save')
100+
get_ipython().magic(u'save recover.txt')
101+
get_ipython().magic(u'save recover.txt 0-56')

src/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from estimate_watermark import *
2+
from preprocess import *
3+
from image_crawler import *
4+

src/__init__.pyc

208 Bytes
Binary file not shown.

src/estimate_watermark.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import sys, os
2+
import cv2
3+
import numpy as np
4+
import warnings
5+
from matplotlib import pyplot as plt
6+
7+
# Variables
8+
KERNEL_SIZE = 3
9+
10+
def estimate_watermark(foldername):
11+
"""
12+
Given a folder, estimate the watermark (grad(W) = median(grad(J)))
13+
Also, give the list of gradients, so that further processing can be done on it
14+
"""
15+
if not os.path.exists(foldername):
16+
warnings.warn("Folder does not exist.", UserWarning)
17+
return None
18+
19+
images = []
20+
for r, dirs, files in os.walk(foldername):
21+
# Get all the images
22+
for file in files:
23+
img = cv2.imread(os.sep.join([r, file]))
24+
if img is not None:
25+
images.append(img)
26+
else:
27+
print("%s not found."%(file))
28+
29+
# Compute gradients
30+
print("Computing gradients.")
31+
gradx = map(lambda x: cv2.Sobel(x, cv2.CV_64F, 1, 0, ksize=KERNEL_SIZE), images)
32+
grady = map(lambda x: cv2.Sobel(x, cv2.CV_64F, 0, 1, ksize=KERNEL_SIZE), images)
33+
34+
# Compute median of grads
35+
print("Computing median gradients.")
36+
Wm_x = np.median(np.array(gradx), axis=0)
37+
Wm_y = np.median(np.array(grady), axis=0)
38+
39+
return (Wm_x, Wm_y, gradx, grady)
40+
41+
42+
def PlotImage(image):
43+
"""
44+
PlotImage: Give a normalized image matrix which can be used with implot, etc.
45+
Maps to [0, 1]
46+
"""
47+
return (image - np.min(image))/(np.max(image) - np.min(image))
48+
49+
50+
def poisson_reconstruct(gradx, grady, kernel_size=KERNEL_SIZE, num_iters=100, h=0.1,
51+
boundary_image=None, boundary_zero=True):
52+
"""
53+
Iterative algorithm for Poisson reconstruction.
54+
Given the gradx and grady values, find laplacian, and solve for image
55+
Also return the squared difference of every step.
56+
h = convergence rate
57+
"""
58+
fxx = cv2.Sobel(gradx, cv2.CV_64F, 1, 0, ksize=kernel_size)
59+
fyy = cv2.Sobel(grady, cv2.CV_64F, 0, 1, ksize=kernel_size)
60+
laplacian = fxx + fyy
61+
m,n,p = laplacian.shape
62+
63+
if boundary_zero == True:
64+
est = np.zeros(laplacian.shape)
65+
else:
66+
assert(boundary_image is not None)
67+
assert(boundary_image.shape == laplacian.shape)
68+
est = boundary_image
69+
70+
est[1:-1, 1:-1, :] = np.random.random((m-2, n-2, p))
71+
loss = []
72+
73+
for i in xrange(num_iters):
74+
old_est = est.copy()
75+
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, :])
76+
error = np.sum(np.square(est-old_est))
77+
loss.append(error)
78+
79+
return (est, loss)
80+
81+
82+
def image_threshold(image, threshold=0.5):
83+
'''
84+
Threshold the image to make all its elements greater than threshold*MAX = 1
85+
'''
86+
m, M = np.min(image), np.max(image)
87+
im = PlotImage(image)
88+
im[im >= threshold] = 1
89+
im[im < 1] = 0
90+
return im
91+
92+
93+
def crop_watermark(gradx, grady, threshold=0.4, boundary_size=2):
94+
"""
95+
Crops the watermark by taking the edge map of magnitude of grad(W)
96+
Assumes the gradx and grady to be in 3 channels
97+
@param: threshold - gives the threshold param
98+
@param: boundary_size - boundary around cropped image
99+
"""
100+
W_mod = np.sqrt(np.square(gradx) + np.square(grady))
101+
W_mod = PlotImage(W_mod)
102+
W_gray = image_threshold(np.average(W_mod, axis=2), threshold=threshold)
103+
x, y = np.where(W_gray == 1)
104+
105+
xm, xM = np.min(x) - boundary_size, np.max(x) + boundary_size
106+
ym, yM = np.min(y) - boundary_size, np.max(y) + boundary_size
107+
108+
return gradx[xm:xM, ym:yM, :] , grady[xm:xM, ym:yM, :]
109+

src/estimate_watermark.pyc

4.68 KB
Binary file not shown.
File renamed without changes.

src/image_crawler.pyc

5.28 KB
Binary file not shown.

preprocess.py src/preprocess.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def preprocess(foldername, size=500, suffix="_processed"):
2121
if img is not None:
2222
m,n,p = img.shape
2323
m_t, n_t = (size-m)/2, (size-n)/2
24-
final_img = np.pad(img, ((m_t, size-m-m_t), (n_t, size-n-n_t), (0, 0)), mode='edge')
24+
final_img = np.pad(img, ((m_t, size-m-m_t), (n_t, size-n-n_t), (0, 0)), mode='constant')
2525
cv2.imwrite(os.sep.join([dest_folder, file]), final_img)
2626
print("Saved to : %s"%(file))
2727
print(final_img.shape)

src/preprocess.pyc

1.25 KB
Binary file not shown.

watermark.png

66.9 KB
Loading

0 commit comments

Comments
 (0)