From: ImageAnalyst <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: problem with imreconstruct
Date: Sat, 26 Feb 2011 12:54:41 -0800 (PST)
Lines: 109
Message-ID: <>
References: <ijpaam$9ep$> <>
 <> <ikbg96$adq$>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
X-Trace: 1298753681 2508 (26 Feb 2011 20:54:41 GMT)
NNTP-Posting-Date: Sat, 26 Feb 2011 20:54:41 +0000 (UTC)
Injection-Info:; posting-host=; posting-account=0rLUzAkAAABojYSRC64DkTbtiSCX77HH
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:
 Gecko/20101203 Firefox/3.6.13,gzip(gfe)
Xref: comp.soft-sys.matlab:712593

Ferrari P.:
You really need to think about what you're doing first before coding
up a useless algorithm like this.  First of all, your binary image m3
is all zeros because there is no pixel in the cameraman image that has
a gray level greater than 16653.  Then you make up an image with a
white block in it and you erode it and then skeletonize the erosion.
But then you make a marker image which is the AND of the white block
and the skeleton.  Don't you realize that this is the same as the
skeleton alone?  Think about it.

So then you use this all skeleton image as the marker image, trying to
find white blobs in logicalm3.  But remember, logicalm3 had nothing at
all in it?  So you get absolutely nothing except all black images.  By
the way, the code ran with no errors, as it should.  But you need to
think about how to design an algorithm.  For a good example on how to
use imreconstruct, you should look to the code I already gave you.  Or
else look at this additional demo where I use imreconstruct to segment
out only the nickels from the coins.png demo image.


% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

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 = 14;

% Change the current folder to the folder of this m-file.

% 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.

% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'coins.png';
% 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);
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, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

% Get a binary image.
binaryImage = grayImage > 94;
% Display the image.
subplot(2, 3, 2);
imshow(binaryImage, []);
title('The Starting Binary Image', 'FontSize', fontSize);

% Erode it to form a marker image.
% Erode it wnough to have only 2 blobs instead of 3
markerImage = imerode(binaryImage, ones(40));
% Display the image.
subplot(2, 3, 3);
imshow(markerImage, []);
title('Marker Image = Eroded Binary Image', 'FontSize', fontSize);

reconst = imreconstruct(markerImage, binaryImage);
% Display the reconstructed image.
subplot(2, 3, 4);
imshow(reconst, []);
title('Final Reconstructed Image', 'FontSize', fontSize);

% Mask the image
nickelOnlyImage = zeros(size(grayImage));
nickelOnlyImage(reconst) = grayImage(reconst);
% Display the masked image.
subplot(2, 3, 5);
imshow(nickelOnlyImage, []);
title('Masked, Nickels-Only Image', 'FontSize', fontSize);