How to measure length and angle of (nearly) linear shapes in a binary image?
Show older comments
Hello. I have the image below that contains multiple cross cutting lines and I want to i) measure the length of each individual line and ii) the angle of each individual line. The lines are not perfectly linear, so task iii) would be to try and figure out a way to approximate a linearity from the lines.

I have tried using regionprops (see below - sorry for any novice-based coding inelegance) but it only gives me one angle at the centre of the image. I assume it has something to do with the thickness of the lines, or the fact that the lines are cutting each other.
Any guidance on the issues with regionprops, or any alternative ways of doing this would be greatly valued!
fname01 = 'Angle_Test';
[grayImage,map] = imread([fname01, '.png']);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = grayImage(:, :, 1);
end
for i=1:100
for j =1:100
if grayImage(i,j) == 0;
grayImage(i,j) = 1;
end
end
end
for i=1:100
for j =1:100
if grayImage(i,j) == 255;
grayImage(i,j) = 0;
end
end
end
props = regionprops(grayImage, 'Orientation','Centroid');
figure(1)
imagesc(grayImage)
allOrientations = [props.Orientation]
hold on;
% Plot crosses over centroid.
for k = 1 : length(props)
x = props(k).Centroid(1);
y = props(k).Centroid(2);
plot(x, y, 'r+', 'MarkerSize', 30, 'LineWidth', 2);
caption = sprintf(' Angle = %.3f', props(k).Orientation);
text(x, y, caption, 'Color', 'r', 'FontSize', 18, 'FontWeight', 'bold');
end
Accepted Answer
More Answers (0)
Categories
Find more on Numerical Integration and Differentiation 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!


