Shapes and Boundaries 357
MATLAB/Octave
>> uint8(shape(test,8))
ans =
6 6 5 6 6 0 0 0 1 2 2 2 3 4 4
and the answer is indeed the code obtained earlier by following the arrows around the object.
Programs are given at the end of the chapter.
Normalization of Chain Codes
There are two problems with the definition of the chain code as given in previous sections:
1. The chain code is dependent on the starting pixel
2. The chain code is dependent on the orientation of the object
First look at Problem (1). The idea is to “normalize” the chain code as follows: imaging
the code to be written around the edge of a circle. We choose as our starting place the
position for which the code, when read off, will be the lowest possible integer. The result is
the normalized chain code for our object.
For example, supp ose we have an object consisting of a 3 × 3 square:
MATLAB/Octave
>> a=zeros(5,5);a(2:4,2:4)=1
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
>> shape(a,4)
ans =
3 3 0 0 1 1 2 2
Now let’s put these codes around a circle as shown in Figure 12.5.
The arrow indicates where we should start reading off the code to obtain the lowest
integer; in this case it is
0 0 1 1 2 2 3 3
But we can do this easily in MATLAB or Octave by using the circshift function to create
every possible shift. For our chain code above, we can do this: