Home > Enterprise >  Linefitting how to deal with continuous values?
Linefitting how to deal with continuous values?

Time:01-20

I'm trying to fit a line using quadratic poly, but because the fit results in continuous values, the integer conversion (for CartesianIndex) rounds it off, and I loose data at that pixel.

I tried the method enter image description here whereas I was expecting it to be continuous line as it was in pre-processing

enter image description here

CodePudding user response:

Do you expect the following:

Raw Image Fitted Polynomial Superposition
enter image description here enter image description here enter image description here

Code:

using Images, Polynomials

img = load("img.png");
img = Gray.(img)

fx(data, dCoef, cCoef, bCoef, aCoef) = @. data^3 *aCoef   data^2 *bCoef   data*cCoef   dCoef;

function fit_poly(img::Array{<:Gray, 2})
  img = img[end:-1:1, :]
  nodes = findall(img.>0)
  xdata = map(p->p[2], nodes)
  ydata = map(p->p[1], nodes)
  f = fit(xdata, ydata, 3)
  xdt = unique(xdata)
  xdt, fx(xdt, f.coeffs...)
end;


function draw_poly!(X, y)
  the_min = minimum(y)
  if the_min<0
    y .-= the_min - 1
  end
  initialized_img = Gray.(zeros(maximum(X), maximum(y)))
  initialized_img[CartesianIndex.(X, y)] .= 1
  dif = diff(y)
  for i in eachindex(dif)
    the_dif = dif[i]
    if abs(the_dif) >= 2
      segment = the_dif ÷ 2
      initialized_img[i, y[i]:y[i] segment] .= 1
      initialized_img[i 1, y[i] segment 1:y[i 1]-1] .= 1
    end
  end
  rotl90(initialized_img)
end;

X, y = fit_poly(img);
y = convert(Vector{Int64}, round.(y));
draw_poly!(X, y)
  • Related