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.

Binary image hit-miss operator

A few months ago, MATLAB user Julia asked how to "fill 4-connected pixels exclusively" on MATLAB Answers. Here's a more precise statement of the problem: if a zero-valued pixel in a binary image has one-valued north, east, south, and west neighbors, then change that zero-valued pixel to a one-valued pixel.

I suggested bwhitmiss in my answer. The function bwhitmiss is a very useful tool for searching for specific pixel neighborhood configurations in a binary image. Here's how it works.

First, create one structuring element (as a matrix of zeros and ones) representing neighborhood pixels you want to "hit." For example, if you want the north, east, south, and west neighbors in a 3-by-3 neighborhood to be one-valued, use this structuring element:

se1 = [0 1 0
       1 0 1
       0 1 0]
se1 =

     0     1     0
     1     0     1
     0     1     0

Now create a second structuring element representing neighborhood pixels you want to "miss." For example, if you want the center pixel of a 3-by-3 neighborhood to be zero-valued, use this structuring element:

se2 = [0 0 0
       0 1 0
       0 0 0]
se2 =

     0     0     0
     0     1     0
     0     0     0

Then you'd pass your binary image and both of these structuring elements to bwhitmiss. Let's construct a small test image to experiment with.

bw = [1 1 0 0 0 0 0
      1 0 1 0 0 1 0
      0 1 0 0 0 0 0
      0 0 0 0 0 0 0
      1 1 1 0 0 1 1
      1 0 1 0 1 0 1
      1 1 1 0 1 0 1]
bw =

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

pixel_matches = bwhitmiss(bw,se1,se2)
pixel_matches =

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

You can see that bwhitmiss found two places where a zero-valued pixel had one-valued north, east, south, and west neighbors. To modify the original image by turning these zero-valued pixels into one-valued pixels, use an elementwise OR operator:

bw2 = bw | pixel_matches
bw2 =

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

Sometimes you'll see references to "don't care" neighbors in discussions about the hit-miss operator. These are neighborhood pixels that can be either zero or one without affecting the match. In terms of the inputs to bwhitmiss, the "don't care" neighbors are the ones where neither se1 nor se1 is equal to one.

dont_care_neighbors = ~(se1 | se2)
dont_care_neighbors =

     1     0     1
     0     0     0
     1     0     1

Here's one more example to finish with. Let's use bwhitmiss to identify isolated pixels. These are one-valued pixels that are completely surrounded by zero-pixels.

se1 = [0 0 0
       0 1 0
       0 0 0];

se2 = [1 1 1
       1 0 1
       1 1 1];

bw3 = bwhitmiss(bw,se1,se2)
bw3 =

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

There was only one isolated pixel in our little sample image.




Published with MATLAB® 7.12

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.