# How to create centerline between to lines in image

17 views (last 30 days)
Dekel Mashiach on 5 May 2022
Commented: Dekel Mashiach on 7 May 2022
Hi, I'm trying to create a centerline between the two white lines, I hope someone can help me fix it.
grayImage = rgb2gray(grayImage);
figure
imshow(grayImage)
grayImage(grayImage<170)=0;
figure
imshow(grayImage, []);
impixelinfo;
mask = logical(grayImage > 140 & grayImage < 255);
figure
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)]);
hold on;
plot(midX, midY, 'g.', 'MarkerSize', 10);
end

yanqi liu on 7 May 2022
yes，sir，may be check the center line，such as
grayImage = rgb2gray(rgbImage);
figure
imshow(grayImage)
grayImage(grayImage<170)=0;
figure
imshow(grayImage, []);
%impixelinfo;
mask = logical(grayImage > 140 & grayImage < 255);
figure
% make center line
rs = sort(unique(r));
cens = [];
for i = 1 : length(rs)
[~,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)
Dekel Mashiach on 7 May 2022
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?