How to create centerline between to lines in image

Hi, I'm trying to create a centerline between the two white lines, I hope someone can help me fix it.
fullFileName = 'road.png';
grayImage = imread(fullFileName);
grayImage = rgb2gray(grayImage);
figure
imshow(grayImage)
grayImage(grayImage<170)=0;
figure
imshow(grayImage, []);
impixelinfo;
mask = logical(grayImage > 140 & grayImage < 255);
mask = bwareafilt(mask, 2);
mask = bwskel(mask);
figure
imshow(mask);
labeledImage = bwlabel(mask);
line1 = ismember(labeledImage, 1);
line2 = ismember(labeledImage, 2);
[r1, c1] = find(line1);
[r2, c2] = find(line2);
same_idx_r1 = ismember(r1,r2);
same_idx_r2 = ismember(r2,r1);
% b=(r1(same_idx_r1)==r2(same_idx_r2));
for k = 1 : length(r1)
% if same_idx==1
% distances = sqrt((r1(k) - r2) .^ 2 + (c1(k) - c2) .^ 2);
distances = ((c2+c1(k)) / 2);
[minDistance, index] = min(distances);
midX(k) = mean([c1(k), c2(index)]);
midY(k) = mean([r1(k), r2(index)]);
% Burn into mask
mask(round(midY(k)), round(midX(k))) = true;
hold on;
plot(midX, midY, 'g.', 'MarkerSize', 10);
end

Réponses (1)

yes,sir,may be check the center line,such as
fullFileName = 'https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/988570/road.png';
rgbImage = imread(fullFileName);
grayImage = rgb2gray(rgbImage);
figure
imshow(grayImage)
grayImage(grayImage<170)=0;
figure
imshow(grayImage, []);
%impixelinfo;
mask = logical(grayImage > 140 & grayImage < 255);
mask = bwareafilt(mask, 2);
mask = bwskel(mask);
mask2 = imopen(mask, strel('line', 3, 0));
mask(mask2) = 0;
mask = logical(mask);
figure
imshow(mask);
% make center line
[r,c] = find(mask);
rs = sort(unique(r));
cens = [];
for i = 1 : length(rs)
mi = mask(rs(i),:);
[~,ci] = find(mi);
if max(ci) - min(ci) < 1e1
continue;
end
cens = [cens; mean([max(ci) min(ci)]) rs(i)];
end
% make line
p = polyfit(cens(:,2), cens(:,1), 2);
yt = linspace(min(rs), max(rs), 1e3);
xt = polyval(p, yt);
hold on; plot(xt, yt, 'g-', 'LineWidth',3)
figure; imshow(rgbImage);
hold on; plot(xt, yt, 'g-', 'LineWidth',3)

1 commentaire

Hi; thanks for the reply, it works but I want to perform the calculation using the average, do you have any idea how I can fix my code?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by