How to measure and show the center to center distance between lines at intersecting points
Show older comments
I am thankful to this platform, and due to courtesy of experts here I have been provided with assistance to attain following code:
IIraster;
[rows, columns, numberOfColorChannels] = size(IIraster)
barsH = medfilt2(IIraster, [1, 115]);
props = regionprops(barsH, 'Area', 'PixelList');
numLinesH = length(props);
hold on;
for k = 1 : numLinesH
x = props(k).PixelList(:, 1);
y = props(k).PixelList(:, 2);
coefficientsH{k} = polyfit(x, y, 2);
xFitH{k} = 1 : columns;
yFitH{k} = polyval(coefficientsH{k}, xFitH{k});
plot(xFitH{k}, yFitH{k}, 'r-', 'LineWidth', 2);
end
barsV = medfilt2(IIraster, [115, 1]);
props = regionprops(barsV, 'Area', 'PixelList');
numLinesV = length(props);
hold on;
for k = 1 : numLinesV
x = props(k).PixelList(:, 2);
y = props(k).PixelList(:, 1);
coefficientsV{k} = polyfit(x, y, 2);
xFitV{k} = 1 : rows;
yFitV{k} = polyval(coefficientsV{k}, xFitV{k});
[xFitV{k}, yFitV{k}] = deal(yFitV{k}, xFitV{k});
plot(xFitV{k}, yFitV{k}, 'r-', 'LineWidth', 2);
end
figure;
imshow(zeros(rows, columns, 'uint8'));
impixelinfo;
hold on;
axis('on', 'image')
XCrossings = zeros(numLinesH, numLinesV);
YCrossings = zeros(numLinesH, numLinesV);
for k1 = 1 : numLinesH
fprintf('Finding where vertical lines cross horizontal line #%d.\n', k1);
xyH = [xFitH{k1}', yFitH{k1}'];
plot(xyH(:, 1), xyH(:, 2), 'g-', 'LineWidth', 2);
for k2 = 1 : numLinesV
xyV = [xFitV{k2}', yFitV{k2}'];
plot(xyV(:, 1), xyV(:, 2), 'r-', 'LineWidth', 2);
distances = pdist2(xyH, xyV);
minDistance = min(distances(distances > 0));
[minRowH, minRowV] = find(distances == minDistance);
xCrossing = mean([xyH(minRowH, 1), xyV(minRowV, 1)]);
yCrossing = mean([xyH(minRowH, 2), xyV(minRowV, 2)]);
XCrossings(k1, k2) = xCrossing;
YCrossings(k1, k2) = yCrossing;
plot(XCrossings(k1, k2), YCrossings(k1, k2), 'c.', 'MarkerSize', 20);
drawnow;
end
end
plot(XCrossings(:), YCrossings(:), 'c.', 'MarkerSize', 20);
title('Crossings Found', 'FontSize', 12);
and getting following outcome

Kindly advise how can I measure and show distances between bars as an outcome, i.e., center to center distance of horizantal and vertical intersection points, as shown below:

I am using a raster image file so number of pixels = units mm. Need assistance for this query.
IIraster.mat file is enclosed for reference.
2 Comments
Matt J
on 13 May 2022
Your code fails to run with the message
Finding where vertical lines cross horizontal line #1.
Undefined variable xFitH.
Error in test (line 18)
xyH = [xFitH{k1}', yFitH{k1}'];
Abdul Hannan Qureshi
on 14 May 2022
Accepted Answer
More Answers (0)
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!