Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question about bwmorph(BW,'dilate',n) and imdilate(BW,SE)

71 views
Skip to first unread message

Nathan

unread,
Apr 26, 2010, 5:45:40 PM4/26/10
to
What is the difference between using bwmorph(BW,'dilate',1) and using
imdilate(BW,ones(3))? (Is there any advantage of using one over the
other?)

The bwmorph documentation states that it uses ones(3) as the
structuring element.
Both functions, when asking for a returned image BWret, return the
same dilated image. (bwmorph without a return image seems to display
the dilated image)

Since I am asking for an updated image to be returned, what is the
difference between these two functions (using the same structuring
element, as stated above)?
Is one faster than another?

Note that BW is a logical matrix throughout my program, and therefore
BWret is also a logical matrix.

(I did notice that if BW was not logical to start with, imdilate
returns BW's type, while bwmorph returns the type logical)

Any help with this would be much appreciated.

I don't know how much it will effect my program, but I am at least
interested in the difference between the two specific calls.

-Nathan

ImageAnalyst

unread,
Apr 26, 2010, 6:37:14 PM4/26/10
to
I doubt there's any difference. You say the output images are the
same. You could try timing with tic and toc to see if there is any
speed difference (but I doubt it). A kernel window with radius of 1
is essentially
1 1 1
1 1 1
1 1 1
which is also what ones(3) is, so I don't doubt that they're the same
for binary images(I haven't brought up the documentation and
scrutinized it though).

In general, the functions in the Image Processing Toolbox that start
with "bw" refer to operations on BINARY images, while those with "im"
prefixes refer to functions that operate on GRAYSCALE images. Thus if
you pass in a grayscale image, imdilate will give you the grayscale
morphology operation (for example local max for imdilate, local min
for imerode, etc.) You can pass in grayscale or binary images to the
im functions but the bw functions only take binary (logical or 0&1)
images.

Nathan

unread,
Apr 26, 2010, 7:00:04 PM4/26/10
to

Hm. Alright then. I'll try to do some timing-tests on my own then.

I have a second question, still regarding bwmorph and imdilate, but
off topic from the first post.

How can I go about dilating only one connected "blob" of an image,
leaving the rest of the "blobs" the same size?
I would like to do this by selecting a blob with my mouse (using
ginput) and finding that region's label within my code and then
dilating only that region.

My blobs are of various shapes and sizes.
The program I use takes sections of a larger image and zooms in to
center on a blob. Doing so often includes more than one blob within
this sub-image. I would like to dilate a chosen blob within this sub-
image, rather than dilate all the blobs (as is currently happening).

Example of a subimage:
BW = [...
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0;
0,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,0;
0,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,0;
0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0;
0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0;
0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0;
0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

Note that if I dilate this image using the aforementioned functions,
both blobs will dilate and will ultimately become one blob.
Note that if I only want to dilate the smaller blob, the two blobs
will remain separate.
(Note that I am using 8-connectivity in my program for labeling and
whatnot)

Doing BWlab = bwlabel(BW,8); we get
BWlab ==
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,1,1,1,0,0,2,2,2,0,0,0,0,0,0,0;
0,1,1,1,1,0,0,2,2,2,2,0,2,2,2,2,0;
0,1,1,1,0,0,0,2,2,2,2,2,2,2,2,2,0;
0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,0,0;
0,0,0,0,0,0,2,2,2,2,2,2,2,0,0,0,0;
0,0,0,2,2,2,2,2,2,0,0,0,0,0,0,0,0;
0,0,0,2,2,2,2,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

What I would like do to is be able to click on BWlab(3,4) to select
the smaller blob to dilate, and end up with BW as:

[...
0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0;
1,1,1,1,1,1,0,1,1,1,0,0,0,0,0,0,0;
1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0;
1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0;
1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0;
0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0;
0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0;
0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

(I would want to be able to do this with either blob)

Any information on how to do this?

-Nathan

ImageAnalyst

unread,
Apr 26, 2010, 8:32:24 PM4/26/10
to
Nathan:
Extract out one blob into it's own image. Let's say you want the blob
that is labeled "5" and want to exclude all the others. You can do:
blob5Image = (labeledImage == 5);

You will get a binary image (0 and 1 now, not 0 and 5) that has ONLY
blob #5 in it. Understand?

Nathan

unread,
Apr 26, 2010, 8:40:33 PM4/26/10
to

Doh!
I was trying to think too hard about this.
I guess all I have to do is extract that one blob (as you mention),
dilate that blob's image, and then bit-or the original image with the
dilated blob's image (right?).

Thanks for the help and clarification.

-Nathan

Image Analyst

unread,
Apr 26, 2010, 8:54:05 PM4/26/10
to
Nathan:
Did you know that if you use the "thicken" option for bwmorph(), it won't join objects, unlike dilate which WILL join objects? If you extract out one blob at a time, dilate it and OR it back it, you will have joined/connected separate blobs.
-ImageAnalyst

Nathan

unread,
Apr 26, 2010, 9:11:21 PM4/26/10
to
On Apr 26, 5:54 pm, "Image Analyst" <imageanal...@mailinator.com>
wrote:

> Nathan:
> Did you know that if you use the "thicken" option for bwmorph(), it won't join objects, unlike dilate which WILL join objects?  If you extract out one blob at a time, dilate it and OR it back it, you will have joined/connected separate blobs.
> -ImageAnalyst

Ah, yeah. I've noticed that. Sometimes my blobs, through thresholding
and whatnot, should be connected while they aren't, so dilate is what
I was looking for. Thanks, though.

-Nathan

Image Analyst

unread,
Apr 26, 2010, 9:29:04 PM4/26/10
to
Are you sure you're not looking for imclose(), which will connect and smooth boundaries while maintaining roughly the original size of the blob (because it dilates but then shrinks back down by following that with an erosion)?

Nathan

unread,
Apr 27, 2010, 4:52:48 PM4/27/10
to
On Apr 26, 6:29 pm, "Image Analyst" <imageanal...@mailinator.com>
wrote:

> Are you sure you're not looking for imclose(), which will connect and smooth boundaries while maintaining roughly the original size of the blob (because it dilates but then shrinks back down by following that with an erosion)?

Ah, no. I have multiple options in my program to edit a given sub-
image to find the area of each blob. I somewhat automate finding each
blob using a pretty decent thresholding technique (the Triangle method
that you either mentioned to me or you've heard me speak of before in
a different thread). This thresholding, due to an uneven background
level, causes some blobs to be slightly smaller than they should be,
and therefore one dilate is usually sufficient to fill in the full
area of the blob.

I also have options for eroding, a usage of the graythresh at a given
threshold, and some manual manipulations available for me (such as
splines, filling in regions, drawing single pixels, and deleting
rectangular regions).

I will also look into eroding a selected region now that I think of
it, but as far as I can tell the same scenario applies as imdilate/
imerode and bwmorph with 'dilate' or 'erode'

Thanks for your interest in helping me out.

(Note that I DO have some functional form of imclose implemented such
that the user is allowed to dilate and erode manually as they please)

-Nathan

Nathan

unread,
Apr 27, 2010, 6:03:53 PM4/27/10
to

Ah, imerode seems to not be the same thing as using bwmorph with the
'erode' option. Test example on r2010a/r2009b/r2009a/r2007b:

>> tmp = bwmorph(true(5),'erode',1)
tmp =
0 0 0 0 0
0 1 1 1 0
0 1 1 1 0
0 1 1 1 0
0 0 0 0 0
>> tmp2 = imerode(true(5),ones(3))
tmp2 =


1 1 1 1 1

1 1 1 1 1


1 1 1 1 1

1 1 1 1 1


1 1 1 1 1

This makes me question whether imerode is working correctly... Eroding
a 5x5 matrix full of ONES/TRUE should at least get rid of the outer
layer, as the bwmorph with 'erode' does.
Could someone tell me why imerode does not (and has not) do this?


So far: 1 point for using bwmorph, 0 points for using imdilate/
imerode.

-Nathan

ImageAnalyst

unread,
Apr 27, 2010, 9:34:26 PM4/27/10
to
Steve Eddins had a posting on that explaining it - either on his blog
or in this newsgroup. Yes, it can be confusing. As I remember I
think it was related to filtering being done on pixel centers rather
than pixel outside edges. Maybe Steve will chime in again.
0 new messages