MATLAB: I have image with black background and white lines. How to find co-ordinate (x,y) of end points of white lines on that image

how can i find co-ordinate (xImage Processing Toolboxy) of end points of white lines in the image?

Best Answer

It looks to me like my code works, as long as you skeletonize your lines before finding branchpoints and endpoints. See code below and image below that it creates.
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 gray scale demo image.
folder = pwd;
baseFileName = 'image.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
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.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.



subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
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')
% Unfortunately the image was saved as color so we need to get the circle only.
% Threshold the image at 128 to binarize it.
binaryImage = grayImage >= 128;
% Crop out the center part, getting rid of the 4 pixel wide border that's there for some reason.
binaryImage = binaryImage(4:end-4, 4:end-4);
% Make sure it's skeletonized to 8-connected, not 4-connected like it is originally
binaryImage = bwmorph(binaryImage, 'skel', inf);
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
branchpoints = bwmorph(binaryImage, 'branchpoints');
[rowsBP, columnsBP] = find(branchpoints)
% Display the image.
subplot(2, 2, 3);
imshow(branchpoints, []);
axis on;
title('Branchpoints Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Plot circles over the branchpoints because we can't see all of them due to subsampling for display on the monitor.
hold on;
for k = 1 : length(rowsBP);
plot(columnsBP(k), rowsBP(k), 'ro', 'MarkerSize', 12, 'LineWidth', 2);
end
% You can find the endpoints where a single line hits the edge of the image using the 'endpoints' option instead of branchpoints.
endpoints = bwmorph(binaryImage, 'endpoints');
[rowsEP, columnsEP] = find(endpoints);
% Display the image.
subplot(2, 2, 4);
imshow(endpoints, []);
axis on;
title('Endpoints Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Plot circles over the endpoints because we can't see all of them due to subsampling for display on the monitor.
hold on;
for k = 1 : length(rowsEP);
plot(columnsEP(k), rowsEP(k), 'ro', 'MarkerSize', 12, 'LineWidth', 2);
end
% Tell user we're done
message = sprintf('Done!');
uiwait(helpdlg(message));