MATLAB: Finding distances between irregular vertical lines for all pixel rows and outputting values

image processingImage Processing Toolboxobject sizepixel distance

I need to find the pixel distance row-by-row between two irregular vertical lines in a binary image (obtained via the Canny edge detection method). I would like the distance in pixels and the corresponding row number (y axis) to be output. Essentially looking for the width of the object for all values of 'y'.
I have the coordinates of all the pixels making the edges (edges are black pixels) via: [y,x] = find(MYIMAGE == 0);
Ideally I would subtract all 'x' values that have the same y value from this matrix and output the differences with the corresponding y values. Because there is a strong curvature to the lines near the top, there will be cases where there are multiple pixels in each line but on the same row – in these cases I need the average distance value.
Any thoughts or advice would be hugely appreciated as I'm very inexperienced with matlab!
Best, Chris

Best Answer

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%===============================================================================
% Read in a standard MATLAB gray scale demo image.
folder = pwd;
baseFileName = '160ulpmin_1 Export.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.

subplot(2, 2, 1);
imshow(grayImage);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Get a binary image of the black lines
binaryImage = grayImage > 128;
[labeledImage, numBlobs] = bwlabel(binaryImage);
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Find width at every row
widths = zeros(rows, 1);
for row = 1 : rows
thisRow = binaryImage(row, :);
leftPixel = find(thisRow, 1, 'first');
rightPixel = find(thisRow, 1, 'last');
if isempty(leftPixel) || isempty(rightPixel)
continue; % Skip lines where there is no white pixel.
end
widths(row) = rightPixel - leftPixel; % Add 1 if you want whole pixels instead of pixel center-to-pixel center.
end
% Display the plot.
subplot(2, 2, 3:4);
plot(widths, 'b-', 'LineWidth', 2);
grid on;
xlabel('Row', 'FontSize', fontSize);
ylabel('Width in pixels', 'FontSize', fontSize);
title('Widths', 'FontSize', fontSize);