Skip to content

Instantly share code, notes, and snippets.

@farrajota
Last active December 3, 2016 20:00
Show Gist options
  • Save farrajota/ab55610652f008ba8719a5e1c5960df4 to your computer and use it in GitHub Desktop.
Save farrajota/ab55610652f008ba8719a5e1c5960df4 to your computer and use it in GitHub Desktop.
Sub-pixel precision using 1D parabola fitting
-- Use three (consecutive) points to fit a 1D parabola
-- and return its maximum value. To achieve sub-pixel
-- precision for a 2D maxima (x,y), just fit the parabola over the
-- x and y coordinates separately with two neighboring points.
local function fitParabola(x1,x2,x3,y1,y2,y3)
local x1_sqr = x1*x1
local x2_sqr = x2*x2
local x3_sqr = x3*x3
local div = (x1_sqr-x1*(x2+x3)+x2*x3)*(x2-x3)
local a = (x1*(y2-y3)-x2*(y1-y3)+x3*(y1-y2))/div
local b = (x1_sqr*(y2-y3)-x2_sqr*(y1-y3)+x3_sqr*(y1-y2))/div
return b/(2*a)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment