Perfect for the beginner, this demo illustrates simple object detection (segmentation, feature extraction), measurement, and filtering. Requires the Image Processing Toolbox (IPT) because it demonstrates some functions supplied by that toolbox, plus it uses the "coins" demo image supplied with that toolbox. If you have the IPT (you can check by typing ver on the command line), you should be able to run this demo code simply by copying and pasting this code into a new editor window, and then clicking the green "run" triangle on the toolbar. First finds all the objects, then filters results to pick out objects of certain sizes. The basic concepts of thresholding, labeling, and regionprops are demonstrated with a simple example. It's a good tutorial for those users new to MATLAB's image processing capabilities to learn on, before they go on to more sophisticated algorithms.

tic; % Start timer.
clc; % Clear command window.
clearvars; % Get rid of variables from prior run of this m-file.
fprintf('Running BlobsDemo.m...\n'); % Message sent to command window.
workspace; % Make sure the workspace panel with all the variables is showing.
imtool close all; % Close all imtool figures.
format long g;
format compact;
captionFontSize = 14;

"` 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.

end "`

"` baseFileName = ‘coins.png'; folder = fileparts(which(baseFileName)); % Determine where demo folder is (works with all versions). fullFileName = fullfile(folder, baseFileName); if ~exist(fullFileName, ‘file')

% It doesn't exist in the current folder.
% Look on the search path.
if ~exist(baseFileName, 'file')
	% It doesn't exist on the search path either.
	% Alert user that we can't find the image.
	warningMessage = sprintf('Error: the input image file\n%s\nwas not found.\nClick OK to exit the demo.', fullFileName);
	fprintf(1, 'Finished running BlobsDemo.m.\n');
% Found it on the search path.  Construct the file name.
fullFileName = baseFileName; % Note: don't prepend the folder.

end % If we get here, we should have found the image file. originalImage = imread(fullFileName); "`

"` [rows, columns, numberOfColorChannels] = size(originalImage); if numberOfColorChannels > 1

promptMessage = sprintf('Your image file has %d color channels.\nThis demo was designed for grayscale images.\nDo you want me to convert it to grayscale for you so you can continue?', numberOfColorChannels);
button = questdlg(promptMessage, 'Continue', 'Convert and Continue', 'Cancel', 'Convert and Continue');
if strcmp(button, 'Cancel')
	fprintf(1, 'Finished running BlobsDemo.m.\n');
% Do the conversion using standard book formula
originalImage = rgb2gray(originalImage);

end "`

[pixelCount, grayLevels] = imhist(originalImage);
subplot(3, 3, 2);
title('Histogram of original image', 'FontSize', captionFontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
grid on;

Method #1: using im2bw() normalizedThresholdValue = 0.4; % In range 0 to 1. thresholdValue = normalizedThresholdValue * max(max(originalImage)); % Gray Levels. binaryImage = im2bw(originalImage, normalizedThresholdValue); % One way to threshold to binary Method #2: using a logical operation. Tested with R2008b through R2011b. Result of Example1