How to create a centerline between lines in image?
Ältere Kommentare anzeigen
Hi
I'm trying to convert the image to binary and create a centerline between the two white lines. Can someone please help me?
clc;
clearvars;
close all;
workspace;
grayImage = imread('road.png');
if ndims(grayImage) == 3
% It's color. Take the red channel.
grayImage = grayImage(:, :, 1);
end
figure
imshow(grayImage, []);
impixelinfo;
mask = logical(grayImage > 140 & grayImage < 255);
mask = bwareafilt(mask, 2); % Make sure we have only two lines.
mask = bwskel(mask);
figure
imshow(mask);
labeledImage = bwlabel(mask);
line1 = ismember(labeledImage, 1);
line2 = ismember(labeledImage, 2);
% Get rows and columns of each line.
[r1, c1] = find(line1);
[r2, c2] = find(line2);
for k = 1 : length(r1)
distances = ((r1(k) + r2) / 2);
[minDistance, index] = min(distances);
% Find the midPoint
midX = mean([c1(k), c2(index)]);
midY = mean([r1(k), r2(index)]);
% Burn into mask
mask(round(midY), round(midX)) = true;
% Optionally drop a marker there
hold on;
plot(midX, midY, 'g.', 'MarkerSize', 10);
end
% Need to add a small amount of noise to x to make the values unique.
midX = midX + 0.001 * rand(size(midX));
midY = midY + 0.001 * rand(size(midY));
% Interpolate x and y to make sure there are no gaps.
kVec = 1 : length(midX);
kFit = linspace(1, kVec(end), 10000);
xFit = interp1(kVec, midX, kFit, 'linear');
yFit = interp1(kVec, midY, kFit, 'linear');
% Remove duplicate values
xy = unique(round([xFit(:), yFit(:)]), "rows");
% Extract individual x and y.
midX = xy(:, 1);
midY = xy(:, 2);
hold on;
plot(midX, midY, 'g.', 'MarkerSize', 10);
Akzeptierte Antwort
Weitere Antworten (2)
Image Analyst
am 3 Mai 2022
0 Stimmen
Here are the stps I would try
- Crop the image to get rid of that white frame.
- Threshold the image to get the white lines.
- Call bwareafilt(bw, 2) to take only the 2 largest blobs.
- Scan down line-by-line using find() to get the first and last white pixel on each line.
- The center line is the average of the first column and the last column.
- If you want, you can fit a straight line through the centerline data to remove noise and smooth the line.
7 Kommentare
Dekel Mashiach
am 3 Mai 2022
Image Analyst
am 3 Mai 2022
I posted something about finding the centerline between two lines of different lengths about a week ago or so. Look for it. I need to get going very soon. I'm traveling over the next 4 days and won't be able to answer until Saturday.
Dekel Mashiach
am 3 Mai 2022
Bearbeitet: Dekel Mashiach
am 3 Mai 2022
Abdul Hannan Qureshi
am 3 Mai 2022
@Image Analyst just assisted me few days back almost same topic. here is the link
Dekel Mashiach
am 4 Mai 2022
Image Analyst
am 7 Mai 2022
@Abdul Hannan Qureshi that wasn't the post I was thinking of, but thanks. @Dekel Mashiach I just got back from 4 days of traveling. You probably have it solved by now, but if not, write back. I'd probably modify my steps 4-6 above and just fit a line on each side, then get the average x point for each fitted y value.
Dekel Mashiach
am 8 Mai 2022
Bearbeitet: Dekel Mashiach
am 8 Mai 2022
yanqi liu
am 7 Mai 2022
0 Stimmen
yes,sir,which is the target line,may be check this

Kategorien
Mehr zu Graphics Performance finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
