How to measure and show the center to center distance between lines at intersecting points
1 view (last 30 days)
Show older comments
Abdul Hannan Qureshi
on 13 May 2022
Commented: Abdul Hannan Qureshi
on 14 May 2022
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}'];
Accepted Answer
Matt J
on 14 May 2022
Edited: Matt J
on 14 May 2022
VDists=hypot( diff(XCrossings,1,1), diff(YCrossings,1,1));
HDists=hypot( diff(XCrossings,1,2), diff(YCrossings,1,2));
xlocs=conv2(XCrossings,[1,1]/2,'same');
ylocs=conv2(YCrossings,[1,1]'/2,'same');
figure(gcf);
[m,n]=size(HDists);
for i=1:m
for j=1:n
k=sub2ind([8,8],i,j);
text(xlocs(k),YCrossings(k), round(HDists(i,j))+" mm",'Color','g','FontSize',12)
end
end
[m,n]=size(VDists);
for i=1:m
for j=1:n
k=sub2ind([8,8],i,j);
text(XCrossings(k),ylocs(k), round(VDists(i,j))+" mm",'Color','r','FontSize',12)
end
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!