##### Contents

Classify two dimensional pieces.

# Description

The input image is a binary image typically found in industrial automation applications. It has three types of objects: rings, nails and T-pins. Our procedure for identification of these classes of objects is based on thickening, thinning and reconstruction.

# Script

The binary image of the pieces is read.

```1. a = adreadgray('pieces_bw.tif').astype(bool)

## Contour noise reduction

A thickening that preserves the number of connected objects is applied to reduce contour noise.

```01. from ia870 import iaimg2se
02. from ia870 import iabinary
03. from ia870 import iase2hmt
04. from ia870 import iaintershow
05. from ia870 import iathick
06.
07.
08. seA = iaimg2se( iabinary([[0,1,0],
09.                           [1,0,1],
10.                           [0,0,0]]))
11. seB = iaimg2se( iabinary([[0,0,0],
12.                           [0,1,0],
13.                           [0,0,0]]))
14. iAB = iase2hmt(seA,seB)
15. print iaintershow(iAB)
16. b = iathick(a, iAB)
```. 0 .
0 0 0
. . .
```

## Skeleton

The homotopic skeleton by thinning is created.

```1. from ia870 import iathin
2.
3. c = iathin(b)

## Skeleton pruning

The open lines of the skeleton are pruned by the end point thinning. The remaining skeleton components will be loops, identifying the rings.

```1. from ia870 import iaendpoints, iagshow
2.
3. d = iathin(c,iaendpoints())

## Detect rings

Extraction of the rings by reconstruction of the thicked image from the filtered skeleton.

```1. from ia870 import iainfrec
2.
3. e = iainfrec(d,b)

## Rings in the input image

Restriction of the objects detected to the input-image.

```1. from ia870 import iaintersec
2.
3. f = iaintersec(a,e)

## Skeleton of the remaining objects

It eliminates the skeleton of the rings.

```1. from ia870 import iasubm
2.
3. g = iasubm(c,e)

## End points filtering

It removes sucessively 4 end-points to let T junctions just on T-pins.

```1. h = iathin(g, iaendpoints(), 4)

## T-pins markers

It detects triple points, applying the union of matchings with the template rotated 90 degrees. These points will identify (mark) the T-pins.

```1. from ia870 import iasupcanon
3. from ia870 import iasedisk
4.
5.
6. seA2 = iaimg2se( iabinary([[0,1,0],[1,1,1],[0,0,0]]))
7. seB2 = iaimg2se( iabinary([[1,0,1],[0,0,0],[0,1,0]]))
8. i = iasupcanon(h, iase2hmt(seA2,seB2))

## Detect T-pins

Detection of the T-pins by reconstruction of the ticked image from the T-pin markers.

```1. from ia870 import iasebox
2.
3.
4. j = iainfrec(i,b,iasebox())

## T-pins in the input image

Restriction of the objects detect to the input image

```1. k = iaintersec(a,j)

## Detect nails

The nails are imediatly detected by the subtration of the images of the rings and T-pints from the input image.

```1. l = iasubm( iasubm(a,f),k)

## Color composition

The result of the classification is presented in a pseudo color image.

```01. from ia870 import iagray
02. from ia870 import iaunion
03. from ia870 import iaglblshow
04.
05.
06. m = iagray(f,'uint8',1)
07. n = iagray(k,'uint8',2)
08. o = iagray(l,'uint8',3)
09. p = iaunion(m,n,o)