Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Chequerboard Corner Detection
require 'rubygems'
require 'hornetseye_ffmpeg'
require 'hornetseye_xorg'
include Hornetseye
class Node
def nms(threshold)
self >= dilate.major(threshold)
end
def have(n, corners)
hist = mask(corners).histogram max + 1
msk = hist.eq n
if msk.inject :or
id = argmax { |i| msk.to_ubyte[i] }.first
eq id
else
nil
end
end
def corners(sigma = 5.0, size = 21)
size2 = size / 2
f1 = finalise(size, size) do |i,j|
x, y = i - size2, j - size2
x * y * Math.exp( -(x ** 2 + y ** 2) / sigma ** 2)
end
f2 = finalise(size, size) do |i,j|
x, y = i - size2, j - size2
0.5 * (x ** 2 - y ** 2) * Math.exp( -(x ** 2 + y ** 2) / sigma ** 2)
end
Math.hypot convolve(f1), convolve(f2)
end
end
CORNERS = 0.6
THRESHOLD = 90
W, H = 8, 5
W2, H2 = 0.5 * (W - 1), 0.5 * (H - 1)
N = W * H
GRID = 7
input = AVInput.new 'calibration.avi'
width, height = input.width, input.height
X11Display.show do
img = input.read_ubytergb
grey = img.to_ubyte
corners = grey.corners
nms = corners.nms CORNERS * corners.max
binary = grey > THRESHOLD
edges = binary.dilate(GRID).and binary.erode(GRID).not
components = edges.components
grid = components.have N, nms
if grid
grid.and(nms).dilate.conditional RGB(255, 255, 0), img
else
nms.dilate.conditional RGB(255, 0, 0), img
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment