forked from dexman545/automatic-watermark-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
132 lines (94 loc) · 3.26 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import math
import os
import warnings
import cv2
import numpy
import numpy as np
import scipy
import scipy.fftpack
import matplotlib.pyplot as plt
import tensorflow as tf
from src.estimate_watermark import *
from src.watermark_reconstruct import *
from constants import constants
from time import sleep
##
# 1. Reading Images In
##
FOLDERNAME = './images/fotolia_processed'
assert os.path.exists(FOLDERNAME), "Folder does not exist."
images = []
for r, dirs, files in os.walk(FOLDERNAME):
# Get all the images
for file in files:
img = cv2.imread(os.sep.join([r, file]))
if img is not None:
images.append(img)
height, width = img.shape[:2]
else:
print("%s not found." % (file))
images = np.array(images)
images.shape
##
# 2. Inital Watermark Detection
##
gx, gy = estimate_watermark(images)
est = poisson_reconstruct(gx, gy)
gx_manual_crop = gx[(height-constants.wmY):height, (width-constants.wmX):width]
gy_manual_crop = gy[(height-constants.wmY):height, (width-constants.wmX):width]
est_manual_crop = poisson_reconstruct(gx_manual_crop, gy_manual_crop)
cropped_gx, cropped_gy = crop_watermark(gx_manual_crop, gy_manual_crop)
est_auto_crop = poisson_reconstruct(cropped_gx, cropped_gy)
with open('cropped.npz', 'wb') as f:
np.savez(f, cropped_gx=cropped_gx, cropped_gy=cropped_gy)
img = cv2.imread('images/fotolia_processed/25_3_018.jpg')
start, rect = watermark_detector(img, cropped_gx, cropped_gy)
im = img.copy()
cv2.rectangle(im, (start[1], start[0]), (start[1] + rect[1], start[0] + rect[0]), (255, 0, 0))
#plt.figure(figsize=(12, 12), dpi= 80, facecolor='w', edgecolor='k')
#plt.imshow(im)
#plt.show()
images_cropped = images[:, start[0]:start[0] + rect[0], start[1]:start[1] + rect[1]]
# Display watermark guesses
#plt.figure()
#for i in range(0,100):
# plt.imshow(images_cropped[i])
# plt.show()
# sleep(1)
# CHECKED
##
# 3. Multi-Image Matting and Reconstruction
##
# Print some random indices extracted
N = 4
random_indices = np.random.randint(images_cropped.shape[0], size=(N*N,))
fig, axes = plt.subplots(N, N, figsize=(12, 8))
for i, val in enumerate(random_indices):
axes[i//N, i%N].imshow(images_cropped[val])
J = images_cropped
W_m = est_auto_crop
# Wm = (255*PlotImage(W_m))
Wm = W_m - W_m.min()
# get threshold of W_m for alpha matte estimate
alph_est = estimate_normalized_alpha(J, Wm, num_images=110)
alph = np.stack([alph_est, alph_est, alph_est], axis=2)
C, est_Ik = estimate_blend_factor(J, Wm, alph)
alpha = alph.copy()
for i in range(3):
alpha[:, :, i] = C[i] * alpha[:, :, i]
Wm = Wm + alpha * est_Ik
W = Wm.copy()
for i in range(3):
W[:, :, i] /= C[i]
img = cv2.imread('images/fotolia_processed/25_3_018.jpg')[None]
Jt = img[:, start[0]:start[0] + rect[0], start[1]:start[1] + rect[1]]
# now we have the values of alpha, Wm, J
# Solve for all images
Wk, Ik, W, alpha1 = solve_images(Jt, W_m, alpha, W)
# W_m_threshold = (255*PlotImage(np.average(W_m, axis=2))).astype(np.uint8)
# ret, thr = cv2.threshold(W_m_threshold, 127, 255, cv2.THRESH_BINARY)
img[:, start[0] + 2: start[0] + rect[0] - 2, start[1] + 2: start[1] + rect[1] - 2] = Ik[:, 2:-2, 2:-2]
img = img.astype(np.uint8)
plt.figure(figsize=(12, 12), dpi= 80, facecolor='w', edgecolor='k')
plt.imshow(img[0])
plt.show()