Browse code

Give more weight to angle alignment

Robert Cranston authored on 13/07/2025 22:10:38
Showing 10 changed files

... ...
@@ -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
 
84 88
Binary files a/results/1.jpg and b/results/1.jpg differ
85 89
Binary files a/results/2.jpg and b/results/2.jpg differ
86 90
Binary files a/results/3.jpg and b/results/3.jpg differ
87 91
Binary files a/results/4.jpg and b/results/4.jpg differ
88 92
Binary files a/results/5.jpg and b/results/5.jpg differ
89 93
Binary files a/results/6.jpg and b/results/6.jpg differ
90 94
Binary files a/results/7.jpg and b/results/7.jpg differ
91 95
Binary files a/results/8.jpg and b/results/8.jpg differ
92 96
Binary files a/results/9.jpg and b/results/9.jpg differ