Skip to content

Instantly share code, notes, and snippets.

@jcboyd
Created February 13, 2024 06:46
Show Gist options
  • Save jcboyd/6fa35c19b43fcbcf800c6d1393051cf4 to your computer and use it in GitHub Desktop.
Save jcboyd/6fa35c19b43fcbcf800c6d1393051cf4 to your computer and use it in GitHub Desktop.
Visualisation of Harris detector
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.patches import Circle, Rectangle, FancyArrowPatch
import matplotlib.lines as mlines
from scipy.ndimage import gaussian_filter
def draw_background(ax, size=7):
background = 0.5 * np.ones((size, size))
corner = size // 2 - 1
background[corner:, corner:] = 0.75
background[corner:, corner:] = 0.75
background[corner:, corner] = 0.25
background[corner, corner:] = 0.25
background = gaussian_filter(background, sigma=0.5)
ax.imshow(background, cmap='Greys_r', vmin=0, vmax=1)
for i in range(size):
l = mlines.Line2D([-0.5, size-0.5], [i-0.5, i-0.5],
color='black', alpha=0.5, linewidth=0.2)
ax.add_line(l)
l = mlines.Line2D([i-0.5, i-0.5], [-0.5, size-0.5],
color='black', alpha=0.5, linewidth=0.2)
ax.add_line(l)
ax.set_xticks([])
ax.set_yticks([])
ax.set_xticklabels([])
ax.set_yticklabels([])
return background
offset_dict = {1 : (1, 0), 2 : (1, 1), 3 : (0, 1), 4 : (-1, 1), 5 : (-1, 0),
6 : (-1, -1), 7 : (0, -1), 8 : (1, -1)}
size = 8
E_dict = {}
colours = ["#F16A70", "#B1D877", "#8CDCDA"]
for i in range(3):
E_dict[i] = []
for j in range(10):
fig, axes = plt.subplots(ncols=2)
ax = axes[1]
background = draw_background(ax, size)
corner_x, corner_y = size // 2 - 2, size // 2
rect = Rectangle((corner_x - 0.5,
corner_y - 0.5 - i),
width=3,
height=3,
fill=None,
edgecolor='#4D4D4D',
linewidth=1.5,
linestyle='-.',
alpha=1)
ax.add_patch(rect)
crop = background[corner_y - i:corner_y - i + 3, corner_x:corner_x + 3]
if not j % 10 == 0 and not j % 10 == 9:
dx, dy = offset_dict[j]
rect = Rectangle((corner_x - 0.5 + dx,
corner_y - 0.5 - i + dy),
width=3,
height=3,
fill=None,
edgecolor=colours[i],
linewidth=1.5,
linestyle='--',
alpha=1)
ax.add_patch(rect)
offset_crop = background[corner_y + dy - i:corner_y + dy - i + 3,
corner_x + dx:corner_x + dx + 3]
E_dict[i].append(np.sum((offset_crop - crop) ** 2))
for di in range(3):
for dj in range(3):
mag = np.linalg.norm([dx, dy])
arr = FancyArrowPatch(
(corner_x + di, corner_y + dj - i),
(corner_x + di + dx / mag, corner_y + dj + dy / mag - i),
arrowstyle='->,head_width=.1',
edgecolor=colours[i],
linewidth=1,
mutation_scale=10,
alpha=1)
ax.add_patch(arr)
ax = axes[0]
sns.set_palette(sns.color_palette(colours))
sns.swarmplot(list(E_dict.values()), ax=ax, size=5)
ax.set_ylabel('E')
ax.set_xlim([-0.5, 2.5])
ax.set_ylim([-0.05, 0.7])
ax.set_xticks([0, 1, 2])
ax.set_xticklabels(['$1^{st}$', '$2^{nd}$', '$3^{rd}$'])
asp = np.diff(ax.get_xlim()) / np.diff(ax.get_ylim())
ax.set_aspect(asp)
fig.tight_layout()
fig.savefig('figs/%04d.pdf' % (10 * i + j), bbox_inches='tight')
plt.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment