Linefitting如何处理连续值?

问题描述 投票:0回答:1

我正在尝试使用二次多项式拟合一条线,但由于拟合会产生连续值,因此整数转换(对于笛卡尔指数)将其四舍五入,并且我丢失了该像素处的数据。

我尝试过这个方法 这里。所以我得到新的 y 值作为

using Images, Polynomials, Plots,ImageView

img = load("jTjYb.png")
img = Gray.(img)
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, 2)
ydata_new .= round.(Int, f.(xdata)
new_line_fitted_img=zeros(size(img))
new_line_fitted_img[xdata,ydata_new].=1
imshow(new_line_fitted_img)

导致如下所示的断线

而我希望它是连续的线,因为它在预处理中

image-processing julia
1个回答
1
投票

您是否期望以下内容:

原始图像 拟合多项式 叠加
enter image description here enter image description here enter image description here

代码:

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)
© www.soinside.com 2019 - 2024. All rights reserved.