Skip to content

Commit 36416be

Browse files
committed
coco dataset init
1 parent 12a91b5 commit 36416be

14 files changed

+101
-41
lines changed

coco_dataset/alpha.png

5.75 KB
Loading

coco_dataset/auth.png

32.4 KB
Loading

coco_dataset/copyright.png

61.4 KB
Loading

coco_dataset/watermark.png

22.4 KB
Loading

final/168667362.jpg

129 KB
Loading

final/168668148.jpg

89 KB
Loading

final/75353065.jpg

54.3 KB
Loading

final/fotolia_168667362.jpg

128 KB
Loading

final/fotolia_168668148.jpg

87.1 KB
Loading

final/fotolia_75353065.jpg

53.4 KB
Loading

main.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
J, img_paths = get_cropped_images('images/fotolia_processed', 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]
22-
idx = idx[:50]
22+
idx = idx[:25]
2323
# Wm = (255*PlotImage(W_m))
2424
Wm = W_m - W_m.min()
2525

@@ -41,9 +41,7 @@
4141
Jt = J[idx]
4242
# now we have the values of alpha, Wm, J
4343
# Solve for all images
44-
Wk, Ik, W, alpha = solve_images(Jt, W_m, alpha, W)
45-
46-
44+
Wk, Ik, W, alpha1 = solve_images(Jt, W_m, alpha, W)
4745
# W_m_threshold = (255*PlotImage(np.average(W_m, axis=2))).astype(np.uint8)
4846
# ret, thr = cv2.threshold(W_m_threshold, 127, 255, cv2.THRESH_BINARY)
4947

main_cocoset.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'''
2+
This main file is for the Microsoft Coco dataset
3+
'''
4+
from src import *
5+
6+
IMAGE_FOLDER = "/media/rohitrango/2EC8DBB2C8DB7715/"
7+
IMG_LOC = "coco_dataset"
8+
IMG_PROCESSED_LOC = "coco_dataset_processed"
9+
10+
def get_alpha_matte(watermark, threshold=128):
11+
w = np.average(watermark, axis=2)
12+
_, w = cv2.threshold(w, threshold, 255, cv2.THRESH_BINARY_INV)
13+
return PlotImage(w)
14+
15+
def P(img,e=None):
16+
if e is None:
17+
plt.imshow(PlotImage(img)); plt.show()
18+
else:
19+
plt.imshow(PlotImage(img),'gray'); plt.show()
20+
21+
def bgr2rgb(img):
22+
return img[:,:,[2, 1, 0]]
23+
'''
24+
Ground Truth values
25+
alpha -> coco_dataset/alpha.png
26+
copyright -> coco_dataset/copyright.png
27+
c = .45
28+
29+
Experiments: Threshold for estimating initial alpha -> 153, and then subtract 1 from alpha
30+
'''
31+
if __name__ == "__main__":
32+
# watermark = cv2.imread('coco_dataset/watermark.png')
33+
# alpha = get_alpha_matte(watermark)
34+
foldername = os.path.join(IMAGE_FOLDER, IMG_PROCESSED_LOC)
35+
gx, gy, gxlist, gylist = estimate_watermark(foldername)
36+
37+
# est = poisson_reconstruct(gx, gy, np.zeros(gx.shape)[:,:,0])
38+
cropped_gx, cropped_gy = crop_watermark(gx, gy)
39+
W_m = poisson_reconstruct(cropped_gx, cropped_gy, num_iters=5000)
40+
41+
# random photo
42+
img = cv2.imread(os.path.join(foldername, '000000051008.jpg'))
43+
im, start, end = watermark_detector(img, cropped_gx, cropped_gy)
44+
num_images = len(gxlist)
45+
46+
J, img_paths = get_cropped_images(foldername, num_images, start, end, cropped_gx.shape)
47+
# get a random subset of J
48+
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]
49+
idx = idx[:25]
50+
# Wm = (255*PlotImage(W_m))
51+
Wm = W_m - W_m.min()
52+
53+
# get threshold of W_m for alpha matte estimate
54+
alph_est = estimate_normalized_alpha(J, Wm, num_images=15, threshold=125, invert=False, adaptive=False)
55+
alph = np.stack([alph_est, alph_est, alph_est], axis=2)
56+
C, est_Ik = estimate_blend_factor(J, Wm, alph)
57+
58+
alpha = alph.copy()
59+
for i in xrange(3):
60+
alpha[:,:,i] = C[i]*alpha[:,:,i]
61+
62+
# Wm = Wm + alpha*est_Ik
63+
64+
W = Wm.copy()
65+
for i in xrange(3):
66+
W[:,:,i]/=C[i]
67+
68+
Jt = J[idx]
69+
# now we have the values of alpha, Wm, J
70+
# Solve for all images
71+
Wk, Ik, W, alpha1 = solve_images(Jt, W_m, alpha, W)
72+
# W_m_threshold = (255*PlotImage(np.average(W_m, axis=2))).astype(np.uint8)
73+
# ret, thr = cv2.threshold(W_m_threshold, 127, 255, cv2.THRESH_BINARY)
74+
75+
76+

src/estimate_watermark.pyc

0 Bytes
Binary file not shown.

src/watermark_reconstruct.py

+23-37
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,15 @@ def get_xSobel_matrix(m, n, p):
110110
return coo_matrix((vals, (i, j)), shape=(size, size))
111111

112112
# get estimated normalized alpha matte
113-
def estimate_normalized_alpha(J, W_m, num_images=30):
113+
def estimate_normalized_alpha(J, W_m, num_images=30, threshold=170, invert=False, adaptive=False, adaptive_threshold=21, c2=10):
114114
_Wm = (255*PlotImage(np.average(W_m, axis=2))).astype(np.uint8)
115-
ret, thr = cv2.threshold(_Wm, 140, 255, cv2.THRESH_BINARY)
115+
if adaptive:
116+
thr = cv2.adaptiveThreshold(_Wm, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, adaptive_threshold, c2)
117+
else:
118+
ret, thr = cv2.threshold(_Wm, threshold, 255, cv2.THRESH_BINARY)
119+
120+
if invert:
121+
thr = 255-thr
116122
thr = np.stack([thr, thr, thr], axis=2)
117123

118124
num, m, n, p = J.shape
@@ -122,47 +128,13 @@ def estimate_normalized_alpha(J, W_m, num_images=30):
122128
print("Estimating normalized alpha using %d images."%(num_images))
123129
# for all images, calculate alpha
124130
for idx in xrange(num_images):
125-
# imgcopy = J[idx].copy()
126-
# for i in xrange(iterpatch):
127-
# r = np.random.randint(10)
128-
# x = np.random.randint(m-r)
129-
# y = np.random.randint(n-r)
130-
# imgcopy[x:x+r, y:y+r, :] = thr[x:x+r, y:y+r, :]
131131
imgcopy = thr
132132
alph = closed_form_matte(J[idx], imgcopy)
133133
alpha[idx] = alph
134134

135135
alpha = np.median(alpha, axis=0)
136136
return alpha
137137

138-
# estimate the blend factor C
139-
# def estimate_blend_factor(J, W_m, alph, threshold=0.01*255):
140-
# alpha_n = alph
141-
# S = J.copy()
142-
# num_images = S.shape[0]
143-
# # for i in xrange(num_images):
144-
# # S[i] = PlotImage(S[i])
145-
# # R = (S<=threshold).astype(np.float64)
146-
# R = (S>-1).astype(np.float64)
147-
148-
# est_Ik = S.copy()
149-
# # est_Ik[S>threshold] = nan
150-
# est_Ik = np.nanmedian(est_Ik, axis=0)
151-
# est_Ik[isnan(est_Ik)] = 0
152-
153-
# alpha_k = R.copy()
154-
# beta_k = R*J
155-
# for i in xrange(num_images):
156-
# alpha_k[i] *= alpha_n*est_Ik
157-
# beta_k[i] -= R[i]*W_m
158-
# beta_k = np.abs(beta_k)
159-
# # we have alpha and beta, solve for c's now
160-
# c = []
161-
# for i in range(3):
162-
# c_i = np.sum(beta_k[:,:,:,i]*alpha_k[:,:,:,i])/np.sum(np.square(alpha_k[:,:,:,i]))
163-
# c.append(c_i)
164-
# return c
165-
166138
def estimate_blend_factor(J, W_m, alph, threshold=0.01*255):
167139
K, m, n, p = J.shape
168140
Jm = (J - W_m)
@@ -219,6 +191,9 @@ def solve_images(J, W_m, alpha, W_init, gamma=1, beta=1, lambda_w=0.005, lambda_
219191
# Iterations
220192
for _ in xrange(iters):
221193

194+
print("------------------------------------")
195+
print("Iteration: %d"%(_))
196+
222197
# Step 1
223198
print("Step 1")
224199
alpha_gx = cv2.Sobel(alpha, cv2.CV_64F, 1, 0, 3)
@@ -316,4 +291,15 @@ def solve_images(J, W_m, alpha, W_init, gamma=1, beta=1, lambda_w=0.005, lambda_
316291
plt.draw()
317292
plt.pause(0.001)
318293

319-
return (Wk, Ik, W, alpha)
294+
return (Wk, Ik, W, alpha)
295+
296+
297+
def changeContrastImage(J, I):
298+
cJ1 = J[0, 0, :]
299+
cJ2 = J[-1, -1, :]
300+
301+
cI1 = I[0, 0, :]
302+
cI2 = I[-1,-1, :]
303+
304+
I_m = cJ1 + (I-cI1)/(cI2-cI1)*(cJ2-cJ1)
305+
return I_m

0 commit comments

Comments
 (0)