Thread Subject: Separating connected image regions?

Subject: Separating connected image regions?

From: Sven

Date: 14 Aug, 2008 21:43:02

Message: 1 of 15

Hi there,

I am attempting to separate two blobs on a series of images,
and I am stuck on a certain aspect of the problem.

Imagine two cells in a series of images. Simple thresholding
returns two masks when the cells aren't touching, but only
one mask when they *are* touching.

Unfortunately the blobs aren't brightest at the centre,
dissipating out towards the edges. Instead, they are
brightest along the cell walls.

Therefore I have tried the following approach.

1. Find an image in the series with two separate blobs. Save
this label matrix as a 'template'.

2. Move to the next image in the series and threshold for blobs.

3. If only one (larger) blob is found, compute the distance
function (as per the 'distance transform' doc page).

At this point I am a little stuck. This image usually gives
me two peaks with a valley between. I want to draw a line
down the lowest point in this valley in order to
artificially break my single mask into two separate masks.

Is this a smart way to tackle the problem? How can my final
step be done?

Note that the template computed in (1) will be used to
verify my final two masks, as I expect the artificially
separated masks to overlap quite well with my template.

Any help would be great.

Cheers,
Sven.

Subject: Separating connected image regions?

From: Steve Carano

Date: 15 Aug, 2008 14:52:17

Message: 2 of 15

There are some excellent examples in the image processing toolbox that
demonstrate the distance function as well as watershed (probably more
appropriate for your application) segmentation.

Steve.

Subject: Separating connected image regions?

From: Sven

Date: 15 Aug, 2008 19:02:02

Message: 3 of 15

Steve Carano <scarano1@earthlink.net> wrote in message
<b902fd14-8fdc-4d2d-b603-c95b4b140531@k30g2000hse.googlegroups.com>...
> There are some excellent examples in the image processing
toolbox that
> demonstrate the distance function as well as watershed
(probably more
> appropriate for your application) segmentation.
>
> Steve.

Hi Steve, thanks for the pointer. I have had a look at
watershed, but my results always seemed to be over-split
(ie, I found too many pools of water).

I thought I should just upload a sample image for clarity.
You can find it here:
http://tinypic.com/view.php?pic=292y49t&s=4
(It has been blurred very slightly in this view, but you can
easily see the two red blobs and where I would like to
separate them).

I am thinking of doing the following:

1: Threshold and get a binary of the two blobs combined into
one blob.
2: Caculate the distance function for that blob.
3: Select two points, one near the centroid of each of the
two blobs (as I wrote in my previous post, I think I can
obtain these)
4: Try to watershed the distance function using these two
points as sources.

I was hoping that this would result in two regions that
match (reasonably well) the two blobs that I'm looking for.
I am having trouble though, forcing the watershed function
to return just two regions. It instead seems to be quite
arbitrary, splitting each blob into a few small regions on
some images, and sometimes returning only one of the blobs
on other images.

I am quite sure that I'm not preparing my image well enough
to force the watershed algorithm to work how I want it (ie,
return two regions).

Can anyone help me out with this?

Cheers,
Sven.

Subject: Separating connected image regions?

From: ImageAnalyst

Date: 17 Aug, 2008 13:56:10

Message: 4 of 15

On Aug 15, 3:02=A0pm, "Sven " <sven.holco...@gmail.deleteme.com> wrote:
> Steve Carano <scara...@earthlink.net> wrote in message
>
> <b902fd14-8fdc-4d2d-b603-c95b4b140...@k30g2000hse.googlegroups.com>...
>
> > There are some excellent examples in the image processing
> toolbox that
> > demonstrate the distance function as well as watershed
> (probably more
> > appropriate for your application) segmentation.
>
> > Steve.
>
> Hi Steve, thanks for the pointer. =A0I have had a look at
> watershed, but my results always seemed to be over-split
> (ie, I found too many pools of water).
>
> I thought I should just upload a sample image for clarity.
> You can find it here:http://tinypic.com/view.php?pic=3D292y49t&s=3D4
> (It has been blurred very slightly in this view, but you can
> easily see the two red blobs and where I would like to
> separate them).
>
> I am thinking of doing the following:
>
> 1: Threshold and get a binary of the two blobs combined into
> one blob.
> 2: Caculate the distance function for that blob.
> 3: Select two points, one near the centroid of each of the
> two blobs (as I wrote in my previous post, I think I can
> obtain these)
> 4: Try to watershed the distance function using these two
> points as sources.
>
> I was hoping that this would result in two regions that
> match (reasonably well) the two blobs that I'm looking for.
> I am having trouble though, forcing the watershed function
> to return just two regions. It instead seems to be quite
> arbitrary, splitting each blob into a few small regions on
> some images, and sometimes returning only one of the blobs
> on other images.
>
> I am quite sure that I'm not preparing my image well enough
> to force the watershed algorithm to work how I want it (ie,
> return two regions).
>
> Can anyone help me out with this?
>
> Cheers,
> Sven.

--------------------------------------------------------------
Sven:
I deal with these kinds of blobs frequently. Here's a method that
often works nicely for me (requires image processing toolbox):
Threshold the blobs by some method.
Erode the thresholded image
Use this eroded image as a "marker" image on the original binary image
during a morphological reconstruction (I forget the name of that
function - maybe it's imreconstruct or something similar).
Parameters include the threshold value and the number of erosions to
do. If you're lucky, these will work for all blob pairs in the entire
image. If not, they may need to be adjusted for each connected pair.
Good luck,
ImageAnalyst

Subject: Separating connected image regions?

From: Lauren Inan

Date: 6 Jun, 2011 06:04:05

Message: 5 of 15

Hi Steve,
I want to segment touching cells which you can see on the image:
http://imageshack.us/photo/my-images/36/cell1.png/

I am just a beginner of Matlab image processing. I tried to use watershed commends which are given at the web page:
http://www.mathworks.com/help/toolbox/images/ref/watershed.html

but it did not work because it divided some single cells although they are not supposed to be divided.
Please help me out about this issue.
It is really urgent.
Thank you,
Lauren

Subject: Separating connected image regions?

From: Sven

Date: 10 Jun, 2011 02:55:04

Message: 6 of 15

"Lauren Inan" <fatih@u.northwestern.edu> wrote in message <ishqkk$omm$1@newscl01ah.mathworks.com>...
> Hi Steve,
> I want to segment touching cells which you can see on the image:
> http://imageshack.us/photo/my-images/36/cell1.png/
>
> I am just a beginner of Matlab image processing. I tried to use watershed commends which are given at the web page:
> http://www.mathworks.com/help/toolbox/images/ref/watershed.html
>
> but it did not work because it divided some single cells although they are not supposed to be divided.
> Please help me out about this issue.
> It is really urgent.
> Thank you,
> Lauren

If I understand things correctly, watershed works well for you on *connected* double-blobs, but doesn't work well for unconnected single-blobs.

So, you could first try to identify those double-blobs (that need watershedding), and only process those with the extra watershed step.

Think of some "features" of a single-blob vs connected-blob. Try taking a look at "regionprops" and the "solidity" property. I suspect that almost all single-blobs will have a higher solidity than double-blobs.
What about the ratio between perimeter and area (which is very similar to solidity). I bet this would also hint at which blobs need breaking up and which blobs you can take at face-value.

Something like this:

1. Threshold the image, get firstBlobs
2. Use regionprops to classify your firstBlobs into 2 sets: singleBlobs (solidity>0.95) and doubleBlobs (solidity<0.95)
3. Run watershedding *only* on doubleBlobs to generate a new set of splitBlobs
4. Your resulting blobs should be the combination of splitBlobs and singleBlobs

Thanks,
Sven.

Subject: Separating connected image regions?

From: ImageAnalyst

Date: 10 Jun, 2011 11:28:20

Message: 7 of 15

On Jun 6, 2:04 am, "Lauren Inan" <fa...@u.northwestern.edu> wrote:
> Hi Steve,
> I want to segment touching cells which you can see on the image:http://imageshack.us/photo/my-images/36/cell1.png/
>
> I am just a beginner of Matlab image processing. I tried to use watershed commends which are given at the web page:http://www.mathworks.com/help/toolbox/images/ref/watershed.html
>
> but it did not work because it divided some single cells although they are not supposed to be divided.
> Please help me out about this issue.
> It is really urgent.
> Thank you,
> Lauren

----------------------------------------
Lauren, did you try "marker-controlled" watershed segmentation?
http://www.mathworks.com/products/image/demos.html?file=/products/demos/shipping/images/ipexwatershed.html

Subject: Separating connected image regions?

From: sarah

Date: 14 Jun, 2011 11:27:04

Message: 8 of 15

"Sven" <sven.holcombe@gmail.deleteme.com> wrote in message <iss128$9vu$1@newscl01ah.mathworks.com>...
> "Lauren Inan" <fatih@u.northwestern.edu> wrote in message <ishqkk$omm$1@newscl01ah.mathworks.com>...
> > Hi Steve,
> > I want to segment touching cells which you can see on the image:
> > http://imageshack.us/photo/my-images/36/cell1.png/
> >
> > I am just a beginner of Matlab image processing. I tried to use watershed commends which are given at the web page:
> > http://www.mathworks.com/help/toolbox/images/ref/watershed.html
> >
> > but it did not work because it divided some single cells although they are not supposed to be divided.
> > Please help me out about this issue.
> > It is really urgent.
> > Thank you,
> > Lauren
>
> If I understand things correctly, watershed works well for you on *connected* double-blobs, but doesn't work well for unconnected single-blobs.
>
> So, you could first try to identify those double-blobs (that need watershedding), and only process those with the extra watershed step.
>
> Think of some "features" of a single-blob vs connected-blob. Try taking a look at "regionprops" and the "solidity" property. I suspect that almost all single-blobs will have a higher solidity than double-blobs.
> What about the ratio between perimeter and area (which is very similar to solidity). I bet this would also hint at which blobs need breaking up and which blobs you can take at face-value.
>
> Something like this:
>
> 1. Threshold the image, get firstBlobs
> 2. Use regionprops to classify your firstBlobs into 2 sets: singleBlobs (solidity>0.95) and doubleBlobs (solidity<0.95)
> 3. Run watershedding *only* on doubleBlobs to generate a new set of splitBlobs
> 4. Your resulting blobs should be the combination of splitBlobs and singleBlobs
>
> Thanks,
> Sven.



I have used watershed algorithm only on those cells which are overlapping. I have used regionprops to identify objects which are overlapping but again it is not giving correct results. It is dividing the cells many times. Cells have many lines in them. On counting this do not give correct results. Now what can I do?

Subject: Separating connected image regions?

From: ImageAnalyst

Date: 14 Jun, 2011 12:32:19

Message: 9 of 15

On Jun 14, 7:27 am, "sarah " <sarahrq...@gmail.com> wrote:
> I have used watershed algorithm only on those cells which are overlapping. I have used regionprops to identify objects which are overlapping but again it is not giving correct results. It is dividing the cells many times. Cells have many lines in them. On counting this do not give correct results. Now what can I do?

-----------------------------------------------------------------------------
If, surprisingly, my suggestion of "marker controlled" watershed
segmentation didn't work, then perhaps you need to just manually
separate them using imfreehand. Actually I'd just exclude overlapped
ones since your measurements on those are not reliable since you don't
have the complete cell. After all, you need to measure them right?
Isn't that the real task here? Or do you not care about the
measurement and your real task is just to separate the cells. If so,
why? Because like I said any measurements aren't going to be reliable
(unless you make some assumptions so that you can "complete" the
missing parts of the cell).

Subject: Separating connected image regions?

From: Sven

Date: 14 Jun, 2011 12:57:04

Message: 10 of 15

Yep, ImageAnalyst is right on all 3 parts:

1. "marker controlled" watershed has a tutorial in the help and it's the way to go if you haven't done so already. It involves a few preparatory steps that will stop you getting the many small regions which would normally result from a simple watershed of the basic image. In fact, I believe that in my first post at the top of this thread I was having the same problem.

2. If your aim is to *measure* all cells, what will you do with the overlapped ones *anyway*? Even if you successfully segment them, the overlapping region will be divided between two cells so there will *still* be some small error in area/length measurement.

3. If your aim is to *count* the cells, is there something you can assume about overlapped blobs? For example, can you always assume they represent 2 blobs and not bother segmenting them? What about taking the average area of all single blob areas, then just guessing at the number of blobs inside a connected blob area? I think you could get pretty close.

If your aim is to count them but you haven't tried marker controlled watershed, then I'd go with that first.


ImageAnalyst <imageanalyst@mailinator.com> wrote in message <33f882e5-6799-41d6-8366-b8f555c8f2da@j23g2000yqc.googlegroups.com>...
> On Jun 14, 7:27 am, "sarah " <sarahrq...@gmail.com> wrote:
> > I have used watershed algorithm only on those cells which are overlapping. I have used regionprops to identify objects which are overlapping but again it is not giving correct results. It is dividing the cells many times. Cells have many lines in them. On counting this do not give correct results. Now what can I do?
>
> -----------------------------------------------------------------------------
> If, surprisingly, my suggestion of "marker controlled" watershed
> segmentation didn't work, then perhaps you need to just manually
> separate them using imfreehand. Actually I'd just exclude overlapped
> ones since your measurements on those are not reliable since you don't
> have the complete cell. After all, you need to measure them right?
> Isn't that the real task here? Or do you not care about the
> measurement and your real task is just to separate the cells. If so,
> why? Because like I said any measurements aren't going to be reliable
> (unless you make some assumptions so that you can "complete" the
> missing parts of the cell).

Subject: Separating connected image regions?

From: sarah

Date: 14 Jun, 2011 15:03:03

Message: 11 of 15

"Sven" <sven.holcombe@gmail.deleteme.com> wrote in message <it7lr0$aj4$1@newscl01ah.mathworks.com>...
> Yep, ImageAnalyst is right on all 3 parts:
>
> 1. "marker controlled" watershed has a tutorial in the help and it's the way to go if you haven't done so already. It involves a few preparatory steps that will stop you getting the many small regions which would normally result from a simple watershed of the basic image. In fact, I believe that in my first post at the top of this thread I was having the same problem.
>
> 2. If your aim is to *measure* all cells, what will you do with the overlapped ones *anyway*? Even if you successfully segment them, the overlapping region will be divided between two cells so there will *still* be some small error in area/length measurement.
>
> 3. If your aim is to *count* the cells, is there something you can assume about overlapped blobs? For example, can you always assume they represent 2 blobs and not bother segmenting them? What about taking the average area of all single blob areas, then just guessing at the number of blobs inside a connected blob area? I think you could get pretty close.
> ------------------------------------------------------------------
I am little bit confused how can you say that I do not have complete call. I do not want to make measurements on them I just simply need to count them.
Thanks for giving tip I will try "marker-controlled" watershed algorithm.
> If your aim is to count them but you haven't tried marker controlled watershed, then I'd go with that first.
>
>
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <33f882e5-6799-41d6-8366-b8f555c8f2da@j23g2000yqc.googlegroups.com>...
> > On Jun 14, 7:27 am, "sarah " <sarahrq...@gmail.com> wrote:
> > > I have used watershed algorithm only on those cells which are overlapping. I have used regionprops to identify objects which are overlapping but again it is not giving correct results. It is dividing the cells many times. Cells have many lines in them. On counting this do not give correct results. Now what can I do?
> >
> > -----------------------------------------------------------------------------
> > If, surprisingly, my suggestion of "marker controlled" watershed
> > segmentation didn't work, then perhaps you need to just manually
> > separate them using imfreehand. Actually I'd just exclude overlapped
> > ones since your measurements on those are not reliable since you don't
> > have the complete cell. After all, you need to measure them right?
> > Isn't that the real task here? Or do you not care about the
> > measurement and your real task is just to separate the cells. If so,
> > why? Because like I said any measurements aren't going to be reliable
> > (unless you make some assumptions so that you can "complete" the
> > missing parts of the cell).

Subject: Separating connected image regions?

From: Sven

Date: 14 Jun, 2011 16:07:04

Message: 12 of 15

If you simply need to count them, how about somethings like this?

% Read the image and clean it up
I = imread('C:\Documents and Settings\sven\Desktop\Temp Attachments\cell1.png');
I = I(:,:,1); % Take only the red channel
level = graythresh(I);
BW = im2bw(I,level);
BW_clean = imfill(bwareaopen(BW,50),'holes');
% Split blobs into single/multi blobs
CC = bwconncomp(BW_clean);
stats = regionprops(CC,'Area','Solidity','Centroid');
figure, plot(sort([stats.Solidity]),'-b.');
solidityThresh = 0.92;
singleBlobMask = [stats.Solidity]>solidityThresh;
multiBlobMask = ~singleBlobMask;
% Make an image showing single/multi blobs
[BW_empty,BW_multi] = deal(false(size(BW)));
BW_multi(cat(1,CC.PixelIdxList{multiBlobMask})) = true;
figure, imshow(double(cat(3,BW_clean, BW_multi, BW_empty))), hold on
% Take a guess at how many blobs are actually in a multi-blob
expectedBlobArea = mean([stats(singleBlobMask).Area]);
multiBlobCountGuess = round([stats(multiBlobMask).Area] / expectedBlobArea);
multiCentroids = cat(1,stats(multiBlobMask).Centroid);
text(multiCentroids(:,1),multiCentroids(:,2), num2str(multiBlobCountGuess'), 'Color','b','FontWeight','bold')

Note that you already *know* how many single blobs there are. The only thing that stops you counting how many total blobs is the fact that you're not sure how many actual blobs are inside a multi-blob. So, the above takes a guess based on the total size of that multi-blob compared to the expected size from all the single-blobs. Note that you could improve the guess by making some assumptions (like each mult-blob must have at least 2 blobs in it).

Thanks,
Sven.

"sarah" wrote in message <it7t77$5uo$1@newscl01ah.mathworks.com>...
> "Sven" <sven.holcombe@gmail.deleteme.com> wrote in message <it7lr0$aj4$1@newscl01ah.mathworks.com>...
> > Yep, ImageAnalyst is right on all 3 parts:
> >
> > 1. "marker controlled" watershed has a tutorial in the help and it's the way to go if you haven't done so already. It involves a few preparatory steps that will stop you getting the many small regions which would normally result from a simple watershed of the basic image. In fact, I believe that in my first post at the top of this thread I was having the same problem.
> >
> > 2. If your aim is to *measure* all cells, what will you do with the overlapped ones *anyway*? Even if you successfully segment them, the overlapping region will be divided between two cells so there will *still* be some small error in area/length measurement.
> >
> > 3. If your aim is to *count* the cells, is there something you can assume about overlapped blobs? For example, can you always assume they represent 2 blobs and not bother segmenting them? What about taking the average area of all single blob areas, then just guessing at the number of blobs inside a connected blob area? I think you could get pretty close.
> > ------------------------------------------------------------------
> I am little bit confused how can you say that I do not have complete call. I do not want to make measurements on them I just simply need to count them.
> Thanks for giving tip I will try "marker-controlled" watershed algorithm.
> > If your aim is to count them but you haven't tried marker controlled watershed, then I'd go with that first.
> >
> >
> > ImageAnalyst <imageanalyst@mailinator.com> wrote in message <33f882e5-6799-41d6-8366-b8f555c8f2da@j23g2000yqc.googlegroups.com>...
> > > On Jun 14, 7:27 am, "sarah " <sarahrq...@gmail.com> wrote:
> > > > I have used watershed algorithm only on those cells which are overlapping. I have used regionprops to identify objects which are overlapping but again it is not giving correct results. It is dividing the cells many times. Cells have many lines in them. On counting this do not give correct results. Now what can I do?
> > >
> > > -----------------------------------------------------------------------------
> > > If, surprisingly, my suggestion of "marker controlled" watershed
> > > segmentation didn't work, then perhaps you need to just manually
> > > separate them using imfreehand. Actually I'd just exclude overlapped
> > > ones since your measurements on those are not reliable since you don't
> > > have the complete cell. After all, you need to measure them right?
> > > Isn't that the real task here? Or do you not care about the
> > > measurement and your real task is just to separate the cells. If so,
> > > why? Because like I said any measurements aren't going to be reliable
> > > (unless you make some assumptions so that you can "complete" the
> > > missing parts of the cell).

Subject: Separating connected image regions?

From: sarah

Date: 15 Jun, 2011 14:55:05

Message: 13 of 15

Thanks alot for your help Sven. It really means alot to me. Well I have tried marker controlled watershed. But It is not giving me accurate results. May be I have done something wrong with it. I have tried your code also. Regionprops was gving same measurements of solidity for both single and multiblobs. For I have used area instead of solidity in your code. But the last part of your code is not working.
%Make an image showing single/multi blobs
[BW_empty,BW_multi] = deal(false(size(BW)));
BW_multi(cat(1,CC.PixelIdxList{multiBlobMask})) = true;
figure, imshow(double(cat(3,BW_clean, BW_multi, BW_empty))), hold on
% Take a guess at how many blobs are actually in a multi-blob
expectedBlobArea = mean([stats(singleBlobMask).Area]);
multiBlobCountGuess = round([stats(multiBlobMask).Area] / expectedBlobArea);
multiCentroids = cat(1,stats(multiBlobMask).Centroid);
text(multiCentroids(:,1),multiCentroids(:,2), num2str(multiBlobCountGuess'), 'Color','b','FontWeight','bold')

Subject: Separating connected image regions?

From: Sven

Date: 15 Jun, 2011 16:03:04

Message: 14 of 15

"sarah" wrote in message <itah49$of4$1@newscl01ah.mathworks.com>...
> Thanks alot for your help Sven. It really means alot to me. Well I have tried marker controlled watershed. But It is not giving me accurate results. May be I have done something wrong with it. I have tried your code also. Regionprops was gving same measurements of solidity for both single and multiblobs. For I have used area instead of solidity in your code. But the last part of your code is not working.
> %Make an image showing single/multi blobs
> [BW_empty,BW_multi] = deal(false(size(BW)));
> BW_multi(cat(1,CC.PixelIdxList{multiBlobMask})) = true;
> figure, imshow(double(cat(3,BW_clean, BW_multi, BW_empty))), hold on
> % Take a guess at how many blobs are actually in a multi-blob
> expectedBlobArea = mean([stats(singleBlobMask).Area]);
> multiBlobCountGuess = round([stats(multiBlobMask).Area] / expectedBlobArea);
> multiCentroids = cat(1,stats(multiBlobMask).Centroid);
> text(multiCentroids(:,1),multiCentroids(:,2), num2str(multiBlobCountGuess'), 'Color','b','FontWeight','bold')

Do you mean this image?
http://imageshack.us/photo/my-images/36/cell1.png/

That's the image my code was working on. In *that* image, solidity was a good predictor. If it's a different image, so be it.

As for when you say "the last part of your code is not working" -- this is not enough information. The code before that and the code after that all ran on my version of MATLAB (2011a). I have no way to help you unless you describe *what* is not working.

Subject: Separating connected image regions?

From: sarah

Date: 16 Jun, 2011 14:33:04

Message: 15 of 15

Well thanks again. I have tried your code and it work previously I was using imresize function that is why it was not giving correct measurements. I have used solidity measurement. It has separated overlapping cells. But few overlapping cells have solidity greater than 0.92. So they are not separated. But I have problem in understanding these lines. Can you explain these lines to me?
 %Make an image showing single/multi blobs
 [BW_empty,BW_multi] = deal(false(size(BW)));
 BW_multi(cat(1,CC.PixelIdxList{multiBlobMask})) = true;
 figure, imshow(double(cat(3,BW_clean, BW_multi, BW_empty))), hold on

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
watershed Sven 15 Aug, 2008 15:05:06
image Sven 14 Aug, 2008 17:45:07
distance Sven 14 Aug, 2008 17:45:07
rssFeed for this Thread

Contact us at files@mathworks.com