[英]Matlab Least Squares approximation with Constraints for Two independent variables (x,y coordinates)
我有幾張室外道路的二值圖像,並且需要獲得道路的精確輪廓,但是,由於仍然殘留有噪點,因此我無法追蹤道路的准確輪廓。 我通過Matlab
houghlines
函數計算了起點和終點之間的中點,並且已經能夠分離出道路的左點和右點。 因此,我的想法是擬合左,右點的線性近似,以便能夠移除兩側的嘈雜像素並干凈地分離出道路區域。
問題:如您在圖片中看到的那樣,適合的直線不在沿着分散的(x,y)點(紅色)的最長軸上,因此不合適。 道路的某些側面非常合適。 polyfitn或polyfit
函數的輸出近似值從較短的散射區域開始。 我找到了幾個帶有約束(開始和結束)的最小二乘逼近的代碼示例,但僅發現了一維自變量類型問題的示例。
這是我用來產生輸出的Matlab代碼。
imshow(binarizedimage320x240); % Binary 320x240 image
rightPoints % show rightPoint (x,y) coordinates from Hough Transform lines
sizeRt = size(rightPoints, 1);
x = zeros(sizeRt, 1);
y = zeros(sizeRt, 1);
for k = 1:sizeRt
x(k) = rightPoints(k,1);
y(k) = rightPoints(k,2);
end
x % show x values
y % show y values
% Linear fitting
p = polyfitn(x,y,'constant x+y') % show p
xi = 1:2:300;
yi = polyvaln(p, xi);
plot(x, y, 'ro', xi, yi, 'b-', 'LineWidth', 3);
變量:
rightPoints =
205.0000 108.0000
205.0000 162.5000
205.0000 276.0000
229.0000 123.5000
229.0000 254.5000
204.0000 87.5000
215.0000 265.0000
216.0000 121.0000
216.0000 255.0000
x =
205
215
216
229
205
229
216
205
204
y =
276.0000
265.0000
255.0000
254.5000
162.5000
123.5000
121.0000
108.0000
87.5000
p =
ModelTerms: [2x1 double]
Coefficients: [-152.6022 1.5730]
ParameterVar: [3.7729e+05 8.2396]
ParameterStd: [614.2421 2.8705]
R2: 0.0411
AdjustedR2: -0.0958
RMSE: 71.6719
VarNames: {'x+y'}
任何幫助將不勝感激。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.