MATLAB Answers

Image Edges: Calculate complexity of a line segment?

6 views (last 30 days)
Philip
Philip on 6 Apr 2012
Can anyone suggest a good way to calculate the 'complexity' of a line segment (obtained from 'bwlabel' after 'canny' edge detection).
I want to define 'complexity' in 2 parts:
  1. How straight the line is
  2. How long the line is
and would like to weight each line segment such that a long and straight line is more favourable than a short and wavy line.
It is ok for the line to change direction so long as they do not change direction regularly (for example, a large square is fine because it is made up of 4 'simple' straight lines). However, edges corresponding to leaves on a tree for example are not good because they change direction too much.
As mentioned, I have used 'bwlabel' after 'canny' edge detection. I then want to iterate through each bwlabel number independently and rank them in order of how (predominately) straight and long they are.
Many thanks for your help!

Answers (3)

Ashish Uthama
Ashish Uthama on 6 Apr 2012
(I'll try to flesh this soon) How about:
  • Obtain the PixelList property of each line using regionprops
  • Compute the difference in location between pixels (gives you an idea of how much the line wiggles)
  • Maybe take the mean of this difference
  • And weight it with the number of pixels in this segment
  1 Comment
Walter Roberson
Walter Roberson on 6 Apr 2012
In a line that curves, you can get pixel lists where the list does not follow the "outside" (or "inside") of the curve. For example,
.x..
xxx.
x..x
then the ordering of the pixels in the section that touches itself is not necessarily going to "thread" the line.
It might make more sense to use http://www.mathworks.com/help/toolbox/images/ref/bwtraceboundary.html as at least then you know the order of the tracing.

Sign in to comment.


Image Analyst
Image Analyst on 6 Apr 2012
Usually the tortuosity ( http://en.wikipedia.org/wiki/Tortuosity) is what is used. I think it's sometimes called "curl" when dealing with fibers. It is the ratio of the length to the distance between the endpoints. A number of alternative definitions are also discussed on that web page.
You can also use fractal dimension ( http://en.wikipedia.org/wiki/Fractal_dimension).
  3 Comments
Philip
Philip on 11 Apr 2012
x1 and x2 are obtained from the output of 'bwtraceboundary.' x1 = route(1,1); x2 = route(end,1); y1 = route(1,2); y2 = route(end,2);
Sorry, can I ask you to elaborate on what you mean when you say you would use 'bwmorph' to get the endpoints? And also, what do you mean by "then divide them"? Do you mean divide the lengths of each line with the corresponding distance between endpoints? or something else?

Sign in to comment.


Image Analyst
Image Analyst on 11 Apr 2012
Philip: Alright, try this code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\Philip\Documents\Temporary';
baseFileName = 'edgese.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
binaryImage = grayImage > 0;
[labeledImage numberOfLines] = bwlabel(binaryImage);
fprintf('numberOfLines = %d\n', numberOfLines);
endpoints = bwmorph(binaryImage, 'endpoints');
subplot(2, 2, 2);
imshow(endpoints, []);
title('Endpoints Image', 'FontSize', fontSize);
% Number of Endpoints should = 2 * numberOfLines because each line will have two endpoints.
numberOfEndpoints = sum(endpoints(:));
fprintf('numberOfEndpoints = %d\n', numberOfEndpoints);
% Find the length of each line
lineLengths = histc(labeledImage(:), 1:numberOfLines)
% Find the endpoints for each
% Then find the distance and the tortuosity.
for lineNumber = 1 : numberOfLines
theseEndpoints = (labeledImage .* endpoints) == lineNumber;
subplot(2, 2, 3);
imshow(theseEndpoints, []);
caption = sprintf('Endpoints for line #%d', lineNumber);
title(caption, 'FontSize', fontSize);
drawnow;
[rows columns] = find(theseEndpoints);
distanceBetweenEndpoints = sqrt((rows(1)-rows(2))^2 + (columns(1)-columns(2))^2);
tortuosity(lineNumber) = lineLengths(lineNumber) / distanceBetweenEndpoints;
fprintf('For line #%d, curve length = %.2f, distance = %.2f,tortuosity = %.3f\n',...
lineNumber, lineLengths(lineNumber), distanceBetweenEndpoints, tortuosity(lineNumber));
end
  9 Comments
Philip
Philip on 12 Apr 2012
Thanks, I have dropped that code into your previous demo, and it seems to be very fast and reliable. I'm trying to see if I can use the 'perimeter' data to help with measuring straight vs. diagonal lines (as per the example above). It seems that the perimeter data returns odd results for more complex lines, which may not be useful for some images that are processed.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!