我正在做一个图像处理项目,基本上是使用图像处理技术矢量化手绘图像。我在我的项目中使用 RANSAC。我面临的挑战是该算法没有按要求执行最佳拟合,但它使用任意两个随机点并绘制一条连接它们的线,如下图所示。
RANSAC 结果
在矢量化手绘图像的算法中,我还使用形态运算符进行了灰度缩放、图像阈值化(图像二值化)和骨架化。
我正在为我的项目使用 MATLAB。
以下是我到目前为止所做的代码
% Line fitting using RANSAC
[x, y] =size(skeleton_image);
point =[];
count =1;
% figure; imshow(~data); hold on
for n =1:x
for m =1:y
if skeleton_image(n,m)==1
point(count,1)=m;
point(count,2)=n;
count= count+1;
end
end
end
data = point';
number = size(data,2); % Total number of points
X = 1:number;
iter=100; num=2; thresh = 1000;count_inlines=103; best_count=0; best_line=[];
for i=1:iter
% Randomly select 2 points
ind = randi(number,num); % randperm(number,num);
rnd_points= data(:,ind);
% Fitting line
Gradient = (rnd_points(2,2)-rnd_points(2,1))/(rnd_points(1,2)-rnd_points(1,1));
Constant = rnd_points(2,1)-Gradient*rnd_points(1,1);
Line = Gradient*X+Constant; [j,k]=size(Line);
% How many pixels are in the line?
for i=1:number
Distance = sqrt((Line(:,i)-data(1,i)).^2)+(Line(:,i)-data(2,i)).^2);
if Distance<=thresh
inlines = data(:,i);
count_inlines=countinlines+1;
best_line=Line;
end