Path: news.mathworks.com!not-for-mail
From: "Ken Campbell" <campbeks@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Need algorithm help: molecule length from an image
Date: Thu, 15 May 2008 23:27:01 +0000 (UTC)
Organization: University of Kentucky
Lines: 94
Message-ID: <g0igs5$l18$1@fred.mathworks.com>
References: <g023qb$j0m$1@fred.mathworks.com> <g0couj$np$1@fred.mathworks.com> <g0crs0$6al$1@canopus.cc.umanitoba.ca> <g0d7jc$mtc$1@fred.mathworks.com> <g0ho8m$1f7$1@fred.mathworks.com>
Reply-To: "Ken Campbell" <campbeks@gmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1210894021 21544 172.30.248.37 (15 May 2008 23:27:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 15 May 2008 23:27:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 534287
Xref: news.mathworks.com comp.soft-sys.matlab:468737


<SNIP>

> > Adrian,
> > 
> > This is very roughly coded but it gives 'an' answer. 
You 
> > might find it  helpful.
> > 
> > Ken
> > 
> > % Read in image and compress to 2D
> > ifs='c:\temp\mol.png';
> > im=imread(ifs);
> > im=sum(im,3);
> > 
> > % Show original image
> > figure(1);
> > imagesc(im);
> > 
> > % Blur and display
> > filt=fspecial('disk',2);
> > blurred_im=imfilter(im,filt,'replicate');
> > 
> > figure(2);
> > imagesc(blurred_im);
> > colorbar;
> > 
> > % Threshold the blurred version
> > threshold_im=zeros(size(im));
> > threshold_im(blurred_im>230)=1;
> > 
> > figure(3);
> > imagesc(threshold_im);
> > 
> > % And keep only the largest 'island' which is the 
molecule
> > molecule_im=bwareaopen(threshold_im,600);
> > 
> > figure(4);
> > clf;
> > imagesc(molecule_im);
> > hold on;
> > 
> > % Now get the perimeter
> > mol_properties=regionprops(bwlabel(molecule_im), ...
> >     'perimeter');
> > molecule_length=(mol_properties.Perimeter)/2
> > 
> > % And draw it to make sure we have the right thing
> > [p,l]=bwboundaries(molecule_im,4,'noholes');
> > boundary=p{1};
> > plot(boundary(:,2),boundary(:,1),'y');
> > 
> 
> Ken,
> 
> Is there anyway to modify your code to outline more than 
one
> molecule? It works great for one molecule, but when I 
start
> looking at more than one, the program breaks. 
> 
> I'm planning on making a GUI for the program so the user 
can
> change the thresholds as needed for an image. After trying
> out your algorithm with various DNA images I have, the
> shading is different between each one (because of the
> inconsistent heavy-metal shadowing, I assume). 
> 
> Thanks,
> Adrian 

You might be able to get round the inconsistent shadowing 
by doing some background correction. Look at imtophat 
and/or imbothat.

Getting the code to identify lots of molecules at the same 
time is too complicated for the quick code I posted (your 
original image only had one molecule after all), but the 
general idea is the same. (1) Make the molecules stand out 
by some blurring/background correction, (2) set the minimum 
size of the molecule you want to analyze with the second 
parameter to bwareaopen and (3) analyze the perimeters. You 
might want to correct the 4/8 connected ness problem my 
original code had.

I'm a bit too busy to do the whole thing for you :-) but it 
shouldn't be too hard. Writing the GUI to do it 
interactively will probably take you longer (it would 
certainly take me longer) than getting the image-processing 
part of the code to work once you understand the algorithm 
and the underlying Matlab commands.

Ken