Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!o21g2000prn.googlegroups.com!not-for-mail
From: ImageAnalyst <imageanalyst@mailinator.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: problem with imreconstruct
Date: Sat, 26 Feb 2011 12:54:41 -0800 (PST)
Organization: http://groups.google.com
Lines: 109
Message-ID: <1355a6b2-8014-4301-af1e-1a34833c0509@o21g2000prn.googlegroups.com>
References: <ijpaam$9ep$1@fred.mathworks.com> <9dd6a81c-bf21-40dc-a7b8-9844bc58fa4c@z3g2000prz.googlegroups.com>
 <021cd4f9-eea5-4dec-ba2f-73fdb50eb490@y36g2000pra.googlegroups.com> <ikbg96$adq$1@fred.mathworks.com>
NNTP-Posting-Host: 184.57.134.178
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
X-Trace: posting.google.com 1298753681 2508 127.0.0.1 (26 Feb 2011 20:54:41 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Sat, 26 Feb 2011 20:54:41 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: o21g2000prn.googlegroups.com; posting-host=184.57.134.178; posting-account=0rLUzAkAAABojYSRC64DkTbtiSCX77HH
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13)
 Gecko/20101203 Firefox/3.6.13,gzip(gfe)
Xref: news.mathworks.com 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.

ImageAnalyst

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% 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.
if(~isdeployed)
	cd(fileparts(which(mfilename)));
end

% 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.
		return;
	end
end

% 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);
		uiwait(warndlg(errorMessage));
		return;
	end
end
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);