How to extract boundary coordinates of multiple objects in an image?

Hi, I am working on a path planning project which requires me to find the boundary (in the form of coordinates) of three black regions in the picture given. When I use convhull(after finding the coordinates of the black portion), it gives me the entire region enclosed within the boundary. Is there a way to get the boundary coordinates of the black portion (separately). Thank you! P.S- I don't require the red dot.
Matlab code is appreciated.

 Accepted Answer

Yes. Use bwboundaries().
% Extract red channel and threshold it.
binaryImage = rgbImage(:,:, 1) < 128;
% Extract the 3 largest blobs ONLY.
binaryImage = bwareafilt(binaryImage, 3);
% Find the boundaries. Each boundary is in a cell.
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
% Display each boundary over the image in red.
hold on;
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Pull N by 2 array from k'th cell.
% Get x and y from the N-by-2 array.
x = thisBoundary(:,2);
y = thisBoundary(:,1);
plot(x, y, 'r', 'LineWidth', 2); % Plot outline over blob.
end
hold off;

1 Comment

Thank you Image Analyst, the code works fine. Is it possible to separate the coordinates? i.e. What I need is something like, suppose the blobs are named blob1, blob2, blob3, I need the coordinates blob1.x and blob1.y; blob2.x and blob2.y; ... respectively. So, three separate coordinate lists for the three blobs(or more, if considering a general case of n blobs) Thank you.

Sign in to comment.

More Answers (1)

OK, here's the full demo:
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;
% Check that user has the specified Toolbox installed and licensed.
hasLicenseForToolbox = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasLicenseForToolbox
% User does not have the toolbox installed, or if it is, there is no available license for it.
% For example, there is a pool of 10 licenses and all 10 have been checked out by other people already.
ver % List what toolboxes the user has licenses available for.
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 gray scale demo image.
folder = pwd;
baseFileName = 'oilspill.jpg';
% 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
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo();
% 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(rgbImage);
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(rgbImage);
% 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 = rgbImage(:, :, 1); % Take red channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Display the image.
subplot(2, 2, 2);
imshow(grayImage, []);
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 3);
bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Extract red channel and threshold it.
binaryImage = grayImage < 128;
% Extract the 3 largest blobs ONLY.
binaryImage = bwareafilt(binaryImage, 3);
% Fill holes
binaryImage = imfill(binaryImage, 'holes');
% Display the image.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Original Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Find the boundaries. Each boundary is in a cell.
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
% Display each boundary over the image in red.
hold on;
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Pull N by 2 array from k'th cell.
% Get x and y from the N-by-2 array.
x = thisBoundary(:,2);
y = thisBoundary(:,1);
plot(x, y, 'r', 'LineWidth', 2); % Plot outline over blob.
end
hold off;

5 Comments

Thank you again, but is it possible to separate the data of the three blobs? i.e. What I need is something like, suppose the blobs are named blob1, blob2, blob3, I need the coordinates blob1.x and blob1.y; blob2.x and blob2.y; ... respectively. So, three separate coordinate lists for the three blobs(or more, if considering a general case of n blobs). Thanks in advance.
The blobs have been labeled. All the properties you tell it to measure will be fields of the structure array. So props(1) has all the information about blob #1, props(2) has all the info on blob #2, and props(3) has all the info on blob #3. Numbering is top to bottom, left to right. If you want the blobs named in some different order, then you can create a label matrix with bwlabel() and reassign the default labels to whatever you want. Is there anything wrong with just using the default numbering? If so, why, and how do you want them to be numbered.
If you want a name "string" to be added to the structure array, you can do that, but I'm not sure why you would. Something like
props(1).blobName = 'My Blob';
props(2).blobName = 'Your Blob';
props(3).blobName = 'Thanks Image Analyst!';
Again, not sure you'd want to do this - it's rarely, if ever, done.
Sorry!, I want them separate because I want to assign k different robots distributed to each blob depending on their boundary size. So I need their properties separately.
Again, they are separate. Each blob's properties is in a single structure of an array of such structures. Why do you act like they're somehow all mixed together?
I was confused sorry. Its all cleared now.

Sign in to comment.

Products

Asked:

on 19 Dec 2017

Commented:

on 20 Dec 2017

Community Treasure Hunt

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

Start Hunting!