![](http://images-20200215.ebookreading.net/23/1/1/9781482247336/9781482247336__a-computational-introduction__9781482247336__bg181.png)
Shapes and Boundaries 367
Python
In : from numpy.fft import fft, ifft
In : execfile(’chaincode.py’)
In : z = np.zeros((256,256))
In : z[31:224,31:96] = 1;z[31:96,95:223] = 1; z[192:224,95:160]=1
In : cc,bdy = shape(z)
In : c = bdy[:,0]+bdy[:,1]
*
1j
In : k = 12; N = len(c)
In : cf = fft(c); cf2 = np.copy(cf); cf2[k+1:N-k]=0; bdy2 = ifft(cf2);
In : cf3 = np.hstack((cf[:k+1],cf[-k:])); bdy3 = ifft(cf3)
In : cf4 = np.hstack((cf[:k+1],np.zeros((100)),cf[-k:])); bdy4 = ifft(cf4)
At this stage cf2 consists of 896 values, most of them zero, and so bdy2 has 896 values.
But
cf3
consists only of the first 13 and last 12 values of
cf
—in effect
cf3
is
cf2
with all
the zero elements removed—and so both it and
bdy3
have only 25 values. Finally,
cf4
is
created from
cf3 by putting in 100 zeros between the first k + 1 and final k values. There
is nothing special about 100—any value will do—but the larger the value, the more points
will fill out the boundary.
So the plots can be created as
MATLAB/Octave
>> plot(bdy2,’.’),axis([0,256,0,256]),axis equal,axis nolabel
>> plot([bdy3;bdy3(1)],’.-’,"markersize",5),axis([0,10000,0,10000]),...
axis equal,axis nolabel
>> plot(bdy4,’.’),axis([0 2000 0 2000]),axis equal,axis nolabel
or as
Python
In : plt.plot(bdy2.real,bdy2.imag,’.’); plt.axis(’equal’)
In : bdy3 = np.hstack(bdy3,bdy3[0])
In : plt.plot(bdy3.real,bdy3.imag,’.-’,markersize=5); plt.axis
([0,9000,0,9000])
In : plt.plot(bdy4.real,bdy4.imag,’.’); plt.axis([0.0,2000.0,0.0,2000.0])
and the results are shown in Figure 12.11.
bdy2: 896 points bdy3: 25 points joined with lines bdy4: 25 + 100 points
FIGURE 12.11: Fourier descriptors drawn differently
To give a slightly more substantial example, consider the three shapes in Figure 12.12.
Each one can be plotted from a list of points; for example, the plane can be created in
MATLAB or Octave with: