Steve on Image Processing with MATLAB

Image processing concepts, algorithms, and MATLAB

Note

Steve on Image Processing with MATLAB has been archived and will not be updated.

A structuring element decomposition “gotcha”

In my earlier posts about dilation and erosion algorithms, I wrote about exploiting structuring element decomposition for improving speed. Today I want to discuss a potential decomposition gotcha: If you don't implement decomposed dilation and erosion carefully, you can easily end up with wrong answers near the image boundaries.

Here's an example to illustrate what can go wrong, starting with a single binary image containing only one foreground pixel;

A = false(5,5);
A(5,3) = true
A =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     1     0     0

Here's a structuring element:

b = [1 0 0; 1 1 0; 1 1 1; 0 1 1; 0 0 1]
b =

     1     0     0
     1     1     0
     1     1     1
     0     1     1
     0     0     1

Compute the dilation of A by b using imdilate:

A_b = imdilate(A, b)
A_b =

     0     0     0     0     0
     0     0     0     0     0
     0     1     0     0     0
     0     1     1     0     0
     0     1     1     1     0

The structuring element b can be decomposed into two smaller structuring elements, b1 and b2.

b1 = [1; 1; 1]
b1 =

     1
     1
     1

b2 = [1 0 0; 0 1 0; 0 0 1]
b2 =

     1     0     0
     0     1     0
     0     0     1

Morphology theory (actually, just the distributive property you learned back in high school) suggests that we should be able to dilate A by b1, and then dilate the result by b2, to get the same result as dilating A by b. Let's try that.

A_b1_b2 = imdilate(imdilate(A, b1), b2)
A_b1_b2 =

     0     0     0     0     0
     0     0     0     0     0
     0     1     0     0     0
     0     1     1     0     0
     0     0     1     1     0

Uh oh. Compare A_b and A_b1_b2 carefully to see they are not the same. So where did we go wrong? Roughly speaking, we failed to take into consideration the effect of dilation outside the image boundaries. If you take the entire image plane into account, dilation by b1 affects pixels outside the original image boundaries. The out-of-bounds pixels can then affect in-bounds results when we dilate by b2. But in the call imdilate(imdilate(A, b1), b2) above, each call to imdilate returns a result that's only the size of the input image; affected out-of-bounds pixels get cropped out.

So let's try that again, but this time we'll pre-pad the input image with 0s.

A_p = padarray(A, [1 1], 0, 'both')
A_p =

     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     0
     0     0     0     0     0     0     0
     0     0     0     1     0     0     0
     0     0     0     0     0     0     0

Now dilate with b1 and b2 in succession:

A_p_b1_b2 = imdilate(imdilate(A_p, b1), b2)
A_p_b1_b2 =

     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     1     0     0     0     0
     0     0     1     1     0     0     0
     0     0     1     1     1     0     0
     0     0     0     1     1     0     0

And trim away the padded rows and columns:

A_p_b1_b2_cropped = A_p_b1_b2(2:end-1, 2:end-1)
A_p_b1_b2_cropped =

     0     0     0     0     0
     0     0     0     0     0
     0     1     0     0     0
     0     1     1     0     0
     0     1     1     1     0

Success! That result equals A_b.

That means we have a trade-off to make. We can get significant speed improvements using structural element decomposition, but we can guarantee correct results at the image boundaries only if we use extra memory to make a padded copy of the input image. That's what imdilate and imerode do.

I have two more brief topics in mind for this series on dilation and erosion algorithms. I'll try to wrap things up in December.




Published with MATLAB® 7.7

|
  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。