Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!s15g2000yqs.googlegroups.com!not-for-mail
From: ImageAnalyst <imageanalyst@mailinator.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: damage measurement in a micrograph
Date: Tue, 3 Nov 2009 19:12:16 -0800 (PST)
Organization: http://groups.google.com
Lines: 66
Message-ID: <0396a454-3b1a-46e4-bffb-e769ea7043d1@s15g2000yqs.googlegroups.com>
References: <hcadg1$3m1$1@fred.mathworks.com> <op.u2kwubcja5ziv5@uthamaa.dhcp.mathworks.com> 
	<79aec1bd-0d70-4fbb-bf15-5c8274ab70dc@j24g2000yqa.googlegroups.com> 
	<hcq35r$d3f$1@fred.mathworks.com> <op.u2t8zakya5ziv5@uthamaa.dhcp.mathworks.com>
NNTP-Posting-Host: 75.186.70.56
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
X-Trace: posting.google.com 1257304336 15318 127.0.0.1 (4 Nov 2009 03:12:16 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 4 Nov 2009 03:12:16 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: s15g2000yqs.googlegroups.com; posting-host=75.186.70.56; 
	posting-account=0rLUzAkAAABojYSRC64DkTbtiSCX77HH
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; 
	GTB6; CyberSafe-IWA-Enable; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 
	3.0.04506.648; .NET CLR 3.5.21022; AskTB5.5),gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:582226


Ashish's code will find the defects - the large dark areas.  The code
I'm giving you below will find the grains.  Run it and see.  What you
have to do to find the grains not including the defects is to use
Ashish's defect image and use it to mask (multiply) my labeled image.
That way you'll get the grains not including the defects.  The key to
finding the grain boundaries is to use the bottom hat filter.  Then
skeletonize them down to single dividing lines.  Then just label, mask
with Ashish's defect map, then call regionprops on the grains.  You
can call regionprops on the defect image if you want info on the
defects too.  I don't have anymore time to mask out the grains with
the defects (I already spent an hour on this algorithm for you) but I
think you can handle it from here.

% Demo to find grains in a metallurgical micrograph.
% by ImageAnalyst
% function test
clc;
close all;
clear all;
workspace; % Show the Workspace panel.
originalImage = imread('grains.jpg');
tic; % Start timer.
originalImage = rgb2gray(originalImage);
figure;
imshow(originalImage);
title('Original Image');

% Do a bottom hat filter.
se = strel('disk', 5);
topHatImage = imbothat(originalImage,se);
figure;
imshow(topHatImage, []);
title('Bottom Hat Image');
% imwrite(topHatImage, 'bottom hat image.bmp');

% Threshold at 20
figure;
grainsImage = topHatImage > 20;
imshow(grainsImage, []);
% Remove small debris.
grainsImage = bwareaopen(grainsImage, 50);
imshow(grainsImage, []);
% Close gaps
grainsImage = imclose(grainsImage, ones(3));
imshow(grainsImage, []);
% Skeletonize to find boundary lines.
grainsImage = bwmorph(grainsImage, 'skel', inf);
grainsImage = bwmorph(grainsImage, 'spur', inf);
imshow(grainsImage, []);
% Remove small lines.
grainsImage = ~bwareaopen(grainsImage, 650);
imshow(grainsImage, []);
% Remove partial grains, where we don't have the complete grain.
grainsImage = imclearborder(grainsImage, 4);
% Show the final image.
imshow(grainsImage, []);
title('Thresholded Image');

% Label it.
labeledImage = bwlabel(grainsImage, 4);
coloredLabeledImage = label2rgb(labeledImage, 'lines');
figure;
imshow(coloredLabeledImage, []);
title('Labeled Image');
grainMeasurements = regionprops(labeledImage, 'all');
toc; % Stop timer.