How to create centerline between to lines in image

조회 수: 2 (최근 30일)
Dekel Mashiach
Dekel Mashiach 2022년 5월 5일
댓글: Dekel Mashiach 2022년 5월 7일
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

답변 (1개)

yanqi liu
yanqi liu 2022년 5월 7일
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
Dekel Mashiach
Dekel Mashiach 2022년 5월 7일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Segmentation and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by