Advanced Image Analysis and Pattern Recognition

profilesyeduru06
Lesson7MorphologyHW_7B.pdf

7 Binary Image Processing

7.1 Neighborhood Relations

In binary images, an object is defined as a connected set of pixels. But which pixels are connected? In 2D images we can distinguish 4-connected and 8-connected objects. If an object is 4-connected, pixels touching each other diagonally are not considered to be connected; that is, each pixel has only four neighbors. In 8-connected objects, all 8 neighbors are considered connected.

This leads to the notion of distance. In a 4-connected world, a diagonal step has a distance of two (since we need to move horizontally first and then vertically). This is called city-block-distance (imagine driving through New-York, where you can only drive in orthogonal directions). The 8-connected distance is called chessboard distance (compare to the steps the King can make in chess). A circle in these two metrics are a diamond and a square, respectively. These are bad approximations to the Euclidean distance. By alternating steps with these two metrics, a new metric (4-8 or 8-4 distance, depending on the first step taken) is obtained, in which a circle is octagonal. This is the best approximation possible if only nearest neighbors are to be taken into account.

In DIPimage, these connectivities are specified as 1, 2 for the 4 and 8-connected steps (1 is only the direct neighbors, 2 are the next neighbors; this notion extends readily to 3D, where a connectivity of 3 can be added). -1 means alternating, starting with 1, and -2 means alternating, starting with 2.

In 3D, there are a 6-connected, a 18-connected and a 26-connected neighborhoods. These are represented in DIPimage with connectivities of 1, 2 and 3 respectively.

7.2 Binary Morphology

Most binary image processing operations fall under the denominator morphology. In Section 8 we will extend these operations to gray-value images.

There are dedicated operations for binary images. The point operations ‘not’ (̃ ), ‘or’☞ (|), ‘and’ (&), ‘xor’ (xor) can only be issued directly onto the command line (see A.4). The binary morphological filter can be found under the ‘Binary Filters’ menu.

The dilation is an operation that ‘grows’ the binary objects. To see it in action, loadbook: 9.6.2 the image ‘cermet’ and threshold it. Apply the function bdilation with different

values for ’iterations’ and ‘connectivity’. Note how the connectivity affects the shape of the resulting objects. A connectivity of -1 or -2 produces the most circular borders.

Exercise 13: Neighborhood shapes Compare the shapes imposed by the selection of a connectivity. To do so, make a binary image with one pixel set:

>> a = deltaim(256,256,’bin’);

Apply several (64) steps of a dilation to it using the different connectivities. What is represented by these shapes?

24

Now try berosion. The erosion ‘shrinks’ objects, and produces the same result as applying a dilation to the background (berosion(a) == ˜bdilation(˜a)).

Note how an erosion completely removes the smaller objects, whereas the larger ones are reduced to small spots. If we were to dilate this image again, we somehow would reconstruct the original large objects, but the small ones, which had disappeared, can- not return.

binary ‘cermet’ + erosion + dilation

This sequence of erosion and dilation is called an opening (bopening). The inversebook: sequence is a closing (bclosing). If the first one removes small objects, the second

one will remove small holes in the objects.

7.3 Selecting Objects

If, instead of applying a dilation after the erosion, we apply a ‘constrained’ dilation, the opening is converted into an opening by reconstruction. There is no such function in DIPimage, but the constrained dilation does exist. It is called binary propagation (bpropagation), and requires two input images: a seed image (the result of thebook:

9.6.8 erosion), and a mask image (the original binary image). What the function does is dilate the seed image, constraining it to the mask image. That is, the resulting objects will never be larger than the objects in the mask image. Try it out on the image we were working on. Make sure to set the edge condition to 0. This is the value of the pixels just outside the boundary of the image. If you set it to 1, all objects touching the border will also be reconstructed. This edge condition can be used to remove edge objects (as done in the function brmedgeobjs).

Exercise 14: Quality control of incandescent lamps Load the image ‘lamps’. It contains an image of six bulbs, two of which are to be discarded. The bulbs must have a contact at the bottom, and it must not touch the outer edge, which is the other contact.

Threshold at a low value, such that the bulb is merged with the background (we are only interested in the fitting which is characterized by the two black rings). Now remove the background using brmedgeobjs (which is imple- mented using bpropagation). Now devise some operations using not (or ˜), bdilation, berosion, bpropagation and/or brmedgeobjs to detect either the good or bad bulbs (either make a program that rejects bad bulbs or accepts good bulbs).

25

‘lamps’ exercise goal alternate goal

The colored images were generated with the command overlay. It overlays a grey-☞ value or color image with a binary image. The third (optional) parameter determines the color for the binary objects. It is possible to apply this function several times, each with a different binary image, which can thus be used to mark the image using several colors.

The last operation we will discuss here is the binary skeleton (bskeleton). It isbook: 9.6.7 a conditional erosion: the objects are eroded until a single line remains. This line

lies close to the geometrical center of the object, and has the same topological prop- erties as the object (i.e. some shape characteristics are preserved). It can be used, as demonstrated in the next two exercises, to generate a seed image for the binary propagation, so as to select objects with specific shape properties (note the functions getsinglepixel, getbranchpixel, etc. in the “Binary Filters” menu).

Exercise 15: Distinguishing nuts from bolts Now load the image ‘nuts_bolts1’. Threshold it. Note that the threshold opera- tion chooses the background as the object (because it is lighter). You will need to inverse the image before or after the thresolding.

Use the bskeleton function (under the “Binary Filters” menu) to create a skeleton of the objects. What is the influence of the ‘Edge Condition’? What does ‘End-Pixel Condition’ control?

With ’looseendsaway’ we can transform the nuts seen from the top (with the hole in them) into a form that is distinguishable from the other objects in the image. Now use the function getsinglepixel to extract the ob- jects without a hole in them. This new image can be used as a seed image in bpropagation. The mask image is the original binary image. The objects with holes are retrieved with b & ˜c (literally b and not c) if the output image for bpropagation was c.

Try extracting the last nut using the bskeleton and the getbranchpixel functions.

As a final test, load the image ‘nuts_bolts2’ and apply the same sequence of commands to it. You should be able to correctly identify the objects in this image.

26

‘nuts_bolts1’ exercise goal

Exercise 16: Recognize components Read in the image ‘components’ and threshold it (make sure that the objects are connected in the thresholded image). Now try to differentiate the transis- tors (three-legged), capacitors (big) and resistors (small) using the techniques learned in this chapter. Optionally, you can differentiate the current stabilizers from the transistors (they have a hole in them), and the ceramic capacitors from the electrolytic capacitors (the round ones are ceramic).

‘components’ exercise goal

This colored image was generated with the command☞ >> joinchannels(’RGB’,(cerco+elco+stab)*255,...

(cerco+res)*255,(trans+stab)*255)

27