Center to center distances between lines: Error in vertical distances

7 views (last 30 days)
I am working on center-to-center distance between lines using following script:
IIraster = imclearborder(Irasterl);
props = regionprops(IIraster, 'Area');
allAreas = sort([props.Area])
IIraster = ~bwareaopen(~IIraster, 500);
IIraster = bwareafilt(IIraster, 1);
[rows, columns, numberOfColorChannels] = size(IIraster)
% Get horizantal bars
barsH = medfilt2(IIraster, [1, 11]);
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
% Get vertical bars
barsV = medfilt2(IIraster, [11, 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
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);
morespace=-5;
HDists=hypot( diff(XCrossings,1,2), diff(YCrossings,1,2));
xlocs=conv2(XCrossings,[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)+morespace,YCrossings(k)+morespace, round(HDists(i,j))+" cm",'Color','g','FontSize',12)
end
end
VDists=hypot( diff(XCrossings,1,1), diff(YCrossings,1,1));
ylocs=conv2(YCrossings,[1,1]'/2,'same');
morespace=2;
[m,n]=size(VDists);
for i=1:m
for j=1:n
k=sub2ind([8,8],i,j);
text(XCrossings(k)+morespace,ylocs(k)+morespace, round(VDists(i,j))+" cm",'Color','r','FontSize',12)
end
end
horizantal center-to-center distances are coming okay.
However, I am unable to adjust middle vertical center-to-center distances (highlighted in blue). these two rows are adding upper and lower lines and showing collective distances.
Irasterl.mat file is enclosed. Kindly advise.

Answers (1)

Matt J
Matt J on 5 Jun 2022
  1 Comment
Abdul Hannan Qureshi
Abdul Hannan Qureshi on 6 Jun 2022
Dear @Matt J thank you for your reply. I am following same script. Only difference is, I have changed the image to low resolution raster image. I have also reduced the parameters values inside as this image is low resolution.
barsH = medfilt2(IIraster, [1, 11]);
barsV = medfilt2(IIraster, [11, 1]);
Horizantal values are coming ok, but for vertical distances it is mixing central two rows. I have tried alot changing different parameters but for low resolution image such error have been observed again and again. I am unable to understand the problem.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!