... | ... |
@@ -38,7 +38,7 @@ def vec(x, y, adjust=False): |
38 | 38 |
|
39 | 39 |
# Load image |
40 | 40 |
gray = np.array(PIL.Image.open(INPUT).convert('L'), dtype='float32') |
41 |
-sigma = np.hypot(*gray.shape) / 30 |
|
41 |
+sigma = np.hypot(*gray.shape) / 50 |
|
42 | 42 |
|
43 | 43 |
# Calculate gradients |
44 | 44 |
dy, dx = np.gradient(gray) |
... | ... |
@@ -56,15 +56,19 @@ dy = blur(dy, sigma) |
56 | 56 |
show("Gradient, blurred", vec(dx, dy, True)) |
57 | 57 |
|
58 | 58 |
# Calculate dot product with maximal blurred gradient, normalized |
59 |
-mag = dx*dx + dy*dy |
|
59 |
+mag = np.sqrt(dx*dx + dy*dy) |
|
60 |
+dx /= mag |
|
61 |
+dy /= mag |
|
60 | 62 |
ind = np.unravel_index(np.argmax(mag), mag.shape) |
61 | 63 |
mdx = dx[ind] |
62 | 64 |
mdy = dy[ind] |
63 |
-weight = (dx*mdx + dy*mdy) / (mdx*mdx + mdy*mdy) |
|
65 |
+mag /= mag[ind] |
|
66 |
+weight = mag * np.power(np.maximum(0, dx*mdx + dy*mdy), 100) |
|
67 |
+weight = blur(weight, sigma) |
|
64 | 68 |
show("Weight", weight) |
65 | 69 |
|
66 | 70 |
# Mask and display |
67 |
-mask = weight > 2/3 |
|
71 |
+mask = weight > 1/2 |
|
68 | 72 |
im = np.array(PIL.Image.open(INPUT)) |
69 | 73 |
im[mask] = (im[mask] + HIGHLIGHT) / 2 |
70 | 74 |
show("Mask", im) |
... | ... |
@@ -76,8 +80,8 @@ mask = scipy.ndimage.rotate(mask, angle) |
76 | 80 |
my, mx = np.nonzero(mask) |
77 | 81 |
mx = np.min(mx), np.max(mx) |
78 | 82 |
my = np.min(my), np.max(my) |
79 |
-cx = int((mx[1] - mx[0]) * 0.2) |
|
80 |
-cy = int((my[1] - my[0]) * 0.3) |
|
83 |
+cx = int((mx[1] - mx[0]) * 0.1) |
|
84 |
+cy = int((my[1] - my[0]) * 0.4) |
|
81 | 85 |
crop = gray[my[0]+cy:my[1]-cy, mx[0]-cx:mx[1]+cx] |
82 | 86 |
show("Rotated, cropped", crop, 'gray') |
83 | 87 |
|