Figure 13.2

>>
>>
>>
castle = iread('castle_sign.jpg', 'double');
iprint(castle, 'sign1');
castle = iread('castle_sign.jpg', 'grey', 'double');


Figure 13.2

>>
>>
idisp(castle > 0.6)
iprint(castle>0.6, 'sign1_bin')

>>
ithresh(castle)

Figure 13.2

>>
>>
>>
clf
ihist(castle, 'b');
iprint('sign1_hist');

>>
t = otsu(castle)

Figure 13.3

>>
>>
>>
>>
castle = iread('castle_sign2.png', 'double', 'grey');
iprint(castle, 'thresh_orig');
ihist(castle, 'b');
iprint('thresh_hist');


Figure 13.3

>>
>>
>>
>>
t = otsu(castle)
iprint(castle>t, 'thresh_otsu');
iprint(castle>0.50, 'thresh_050');
iprint(castle>0.60, 'thresh_060');

>>
>>
t=niblack(castle, -0.2, 35);
idisp(t)

Figure 13.4

>>
>>
>>
idisp(castle>t)
iprint(t, 'thresh_niblack_t')
iprint(castle>t, 'thresh_niblack_result')

>>
[mser,nsets] = imser(castle, 'light');
>>
nsets

Figure 13.5

>>
>>
>>
idisp(mser, 'colormap', 'jet')
idisp(mser, 'colormap', 'jet', 'nogui')
iprint('sign_mser');


Figure 13.6

>>
>>
>>
im_targets = iread('yellowtargets.png', 'gamma', 'sRGB', 'double');
z = iread('yellowtargets.png',  'double');
iprint(z, 'targets');


Figure 13.7

>>
>>
>>
im_garden = iread('tomato_124.jpg', 'gamma', 'sRGB', 'double');
z = iread('tomato_124.jpg', 'double');
iprint(z, 'tomatoes');

>>
>>
randinit
[cls, cxy,resid] = colorkmeans(im_targets, 4);
>>
cxy

Figure 13.6

>>
>>
>>
clf
xycolorspace(cxy);
iprint('targets_xy');

>>
colorname(cxy(:,3), 'xy')
>>
resid

Figure 13.6

>>
>>
>>
idisp(cls, 'colormap', 'flag')
idisp(cls, 'nogui', 'colormap', 'flag')
iprint('targets_seg')

>>
cls3 = (cls == 3);

Figure 13.6

>>
>>
idisp(cls3)
iprint(cls3, 'targets_binary');


Figure 13.6

>>
>>
targets_binary = iopen(cls3, kcircle(2));
iprint(targets_binary, 'targets_clean');


Figure 13.7

>>
>>
>>
>>
>>
>>
>>
randinit
[cls, cxy] = colorkmeans(im_garden, 4);
cxy
idisp(cls, 'nogui', 'colormap', 'flag')
iprint('tomatoes_seg')
xycolorspace(cxy);
iprint('tomatoes_xy');


Figure 13.7

>>
>>
cls2 = (cls == 2);
iprint(cls2, 'tomatoes_binary');


Figure 13.7

>>
>>
tomatoes_binary = iclose(cls2, kcircle(15));
iprint(tomatoes_binary, 'tomatoes_clean');

>>
im = ilabeltest;
>>
idisp(im)

Figure 13.8

>>
>>
[label, m] = ilabel(im);
iprint(im, 'imraw');

>>
m
>>
label

Figure 13.8

>>
>>
idisp(label, 'colormap', 'jet')
iprint(label, 'imlab', 'colormap', 'jet');


Figure 13.8

>>
>>
>>
reg3 = (label==3);
idisp(reg3)
iprint( reg3, 'imlab2');

>>
[label, m, parents, cls] = ilabel(im);
>>
parents'
>>
parents(4)
>>
cls'

Figure 13.11

>>
>>
>>
>>
targets_label = ilabel(targets_binary);
idisp(targets_label, 'colormap', 'jet');
idisp(targets_label, 'nogui', 'colormap', 'jet');
iprint('targets_label');


Figure 13.9

>>
>>
>>
>>
tomatoes_label = ilabel(tomatoes_binary);
idisp(tomatoes_label, 'colormap', 'jet');
idisp(tomatoes_label, 'nogui', 'colormap', 'jet');
iprint('tomatoes_label');


Figure 13.10

>>
>>
im = iread('58060.jpg');
iprint(im, '58060');


Figure 13.10

>>
>>
>>
>>
[label, m] = igraphseg(im, 1500, 100, 0.5);
m
idisp(label, 'colormap', 'jet')
iprint(label, '58060_seg', 'colormap', 'jet');

>>
>>
>>
reg5 = (targets_label == 5);
idisp(reg5);
idisp(reg5, 'nogui', 'invert')
>>
sum(reg5(:))
>>
[v,u] = find(reg5);
>>
about(u)
>>
>>
>>
>>
umin = min(u)
umax = max(u)
vmin = min(v)
vmax = max(v)
>>
plot_box(umin, vmin, umax, vmax, 'b')
>>
m00 = mpq(reg5, 0, 0)
>>
>>
uc = mpq(reg5, 1, 0) / m00
vc = mpq(reg5, 0, 1) / m00

Figure 13.11

>>
>>
>>
hold on
plot(uc, vc, 'gx'); plot(uc, vc, 'go');
iprint('target_R1');

>>
>>
u20 = upq(reg5, 2, 0); u02 = upq(reg5, 0, 2); u11 = upq(reg5, 1, 1);
J = [ u20 u11; u11 u02]
>>
lambda = eig(J)
>>
ab = 2 * sqrt(lambda / m00)
>>
ab(1)/ab(2)
>>
>>
[x,lambda] = eig(J);
x
>>
v = x(:,end);
>>
atan2( v(2), v(1) ) * 180/pi

Figure 13.12

>>
>>
>>
>>
>>
>>
>>
plot_ellipse(4*J/m00, [uc, vc], 'r');
clf
idisp(reg5, 'nogui', 'invert');
hold on
plot(uc, vc, 'gx'); plot(uc, vc, 'go');
plot_ellipse(4*J/m00, [uc, vc], 'r');
iprint('equiv_ellipse');

>>
blob = imoments(reg5)
>>
>>
>>
blob.uc
blob.theta
blob.shape
>>
>>
blob.moments.m00
blob.moments.u11
>>
f = iblobs(targets_binary)
>>
>>
>>
>>
>>
f(1).class
f(1).parent
f(1).touch
f(1).umin
f(1).shape
>>
f(1).plot_box('g')
>>
f.plot_box('r:')
>>
f(4).children
>>
f = iblobs(tomatoes_binary, 'area', [1000, 5000])
>>
f = iblobs(tomatoes_binary, 'touch', 0)
>>
im = iread('P2.png');
>>
>>
[f,L] = iblobs(im, 'class', 1);
f
>>
>>
humoments(L==2)
humoments(L==4)
>>
f = iblobs(tomatoes_binary, 'boundary', 'class', 1)
>>
size(f(1).edge)
>>
f(1).edge(:,1:5)
>>
f(1).plot_boundary('g.')

Figure 13.14

>>
>>
>>
>>
>>
idisp(tomatoes_binary)
idisp(tomatoes_binary, 'nogui');
f.plot_boundary('r.')
f.plot_box('g')
iprint('tomatoes_perim');

>>
im = iread('P2.png');

Figure 13.15

>>
>>
>>
>>
f = iblobs( im, 'boundary', 'class', 1)
idisp(im, 'nogui')
f.plot()
iprint(im, 'letterP');

>>
f(2).area / f(1).area
>>
sqrt(ans)
>>
(f(2).theta - f(1).theta) * 180/pi
>>
f(1).boundary();

Figure 13.15

>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
[r1,theta1] = f(1).boundary();
clf
f(1).boundary('b-')
hold on
f(2).boundary('r-')
grid
set(gca,'ydir', 'reverse')
axis square
xlabel('u (pixels)')
ylabel('v (pixels)')
iprint('Pboundary');
clf

>>
plot(theta1, r1)

Figure 13.15

>>
>>
>>
>>
>>
>>
>>
[r2, theta2] = f(2).boundary();
hold on
plot(theta1, r1, theta2, r2);
xaxis(-pi, pi);
xlabel('\theta'); ylabel('r'); legend('region 1', 'region 2')
iprint('Ppolar')
clf

>>
[sim,scale] = boundmatch(r1, r2);

Figure 13.15

>>
>>
>>
>>
>>
plot(theta1, sim)
xaxis(-pi, pi);
xlabel('theta (rad)');
ylabel('similarity')
iprint('Pxcorr')

>>
>>
x = peakfit2(theta1, sim);
x * 180/pi
>>
1/scale

Figure 13.16

>>
>>
>>
>>
>>
im = iread('5points.png', 'double');
iprint(im, '5points');
h = Hough(im);
h.show();
iprint('5points_hough');

>>
>>
im = testpattern('squares', 256, 256, 128);
im = irotate(im, -0.3);

Figure 13.18

>>
>>
edges = icanny(im);
iprint(edges, 'hough1');

>>
h = Hough(edges)

Figure 13.18

>>
>>
>>
>>
h.show();
iprint('hough2');
axis([-1.40 -1.1 -190 -110]);
iprint('hough2a');

>>
lines = h.lines()
>>
h = Hough(edges, 'suppress', 5)
>>
lines = h.lines()

Figure 13.18

>>
>>
>>
>>
>>
idisp(im);
h.plot('g')
idisp(im, 'nogui');
h.plot('g')
iprint('hough3');

>>
>>
>>
>>
im = iread('church.png', 'grey', 'double');
edges = icanny(im);
h = Hough(edges, 'suppress', 5);
lines = h.lines();
>>
>>
idisp(im);
lines(1:10).plot();
>>
lines = lines.seglength(edges);
>>
lines(1)
>>
k = find( lines.length_v > 100);

Figure 13.19

>>
>>
>>
>>
>>
lines(k).plot('g')
idisp(im, 'nogui');
lines(1:10).plot();
lines(k).plot('g');
iprint('church_hough');

>>
>>
b1 = iread('building2-1.png', 'grey', 'double');
idisp(b1)
>>
C = icorner(b1, 'nfeat', 200);

Figure 13.20

>>
>>
>>
>>
>>
>>
>>
idisp(b1, 'dark');
C.plot('ws');
idisp(b1, 'nogui',  'dark');
C.plot('ws');
iprint('building_1');
xaxis(400, 900); yaxis(50, 350);
iprint('building_1_zoom');

>>
C1 = icorner(b1, 'nfeat', 200, 'suppress', 10);
>>
length(C)
>>
C(1:4)
>>
>>
C(1:5).strength
C(1).u
>>
C(1:5:100).plot()
>>
[C,strength] = icorner(b1, 'nfeat', 200);

Figure 13.21

>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
idisp(strength, 'invsigned')
idisp(strength,  'invsigned', 'nogui')
axis([300 500 300 500]);
iprint('harris_corner_image');
s = strength;
[n,x] = hist(s, 100);
cc = cumsum(n);
plot(x,cc/max(cc))
grid
xaxis(max(x)); yaxis(0.8, 1);
xlabel('corner strength');
ylabel('cumulative number of features');
iprint('harris_cdf')
idisp(strength, 'nogui')
C.plot('g+')
axis([460 475 150 165])
iprint('harris_corner_zoom');

>>
ihist(strength, 'normcdf')
>>
b2 = iread('building2-2.png', 'grey', 'double');

Figure 13.20

>>
>>
>>
>>
>>
>>
>>
>>
C2 = icorner(b2,  'nfeat', 200);
idisp(b2,'dark')
C2.plot('ws');
idisp(b2, 'nogui',  'dark')
C2.plot('ws');
iprint('building_2');
xaxis(200, 700); yaxis(50, 350);
iprint('building_2_zoom');


Figure 13.23

>>
>>
>>
im = iread('scale-space.png', 'double');
idisp(im, 'nogui', 'square');
iprint('ss-image');


Figure 13.23

>>
>>
>>
>>
>>
>>
>>
>>
>>
[G,L,s] = iscalespace(im, 60, 2);
k=5
idisp(L(:,:,k), 'invsigned', 'nogui', 'square');  text(10, 180, sprintf('sigma = %f', s(k))); hold on; plot(64, 64, 'k+'); plot(128, 64, 'k+'); plot(64, 128, 'k+'); plot(128, 128, 'k+'); hold off; iprint('ss-5');
k=20
idisp(L(:,:,k), 'invsigned', 'nogui', 'square');  text(10, 180, sprintf('sigma = %f', s(k))); hold on; plot(64, 64, 'k+'); plot(128, 64, 'k+'); plot(64, 128, 'k+'); plot(128, 128, 'k+'); hold off; iprint('ss-20');
k=35
idisp(L(:,:,k), 'invsigned', 'nogui', 'square');  text(10, 180, sprintf('sigma = %f', s(k))); hold on; plot(64, 64, 'k+'); plot(128, 64, 'k+'); plot(64, 128, 'k+'); plot(128, 128, 'k+'); hold off; iprint('ss-35');
k=55
idisp(L(:,:,k), 'invsigned', 'nogui', 'square');  text(10, 180, sprintf('sigma = %f', s(k))); hold on; plot(64, 64, 'k+'); plot(128, 64, 'k+'); plot(64, 128, 'k+'); plot(128, 128, 'k+'); hold off; iprint('ss-55');


Figure 13.23

>>
>>
>>
>>
>>
idisp(L(:,:,5), 'invsigned')
clf; plot(s(1:end-1), -[squeeze(L(64,64,:)) squeeze(L(64,128,:)) squeeze(L(128,64,:)) squeeze(L(128,128,:))])
xaxis(s(end))
grid; xlabel('scale'); ylabel('|LoG|'); legend('5x5', '9x9', '17x17', '33x33')
iprint('ss-plot');

>>
s(5)
>>
f = iscalemax(L, s)
>>
>>
>>
idisp(im)
idisp(im, 'nogui');
f(1:4).plot('g*')

Figure 13.24

>>
>>
f(1:4).plot_scale('r')
iprint('ss_feature', 'square');

>>
im = iread('lena.pgm', 'double');
>>
[G,L] = iscalespace(im, 8, 8);

Figure 13.25

>>
>>
>>
>>
>>
>>
>>
idisp(G,  'flatten', 'wide', 'square');
idisp(G, 'flatten', 'wide', 'square', 'nogui');
iprint('scalespace_g', 'nogrid')
idisp(L,  'flatten', 'wide', 'square', 'invsigned');
idisp(L, 'flatten', 'wide', 'square', 'invsigned', 'nogui');
iprint('scalespace_l', 'nogrid')
delete(gcf)

>>
surf1 = isurf(b1)
>>
surf1(1)

Figure 13.27

>>
>>
>>
>>
>>
idisp(b1, 'dark');
surf1(1:50:end).plot_scale('g', 'clock')
idisp(b1, 'nogui', 'dark');
surf1(1:50:end).plot_scale('g', 'clock')
iprint('sift_features', 'nogrid');


Figure 13.28

>>
>>
>>
>>
>>
>>
>>
>>
>>
hist(surf1.scale, 100);
[n,x] = hist(surf1.scale, 100)
bar(x,n, 'b')
hold on
set(gca, 'yscale', 'log')
k=n==1;
plot(x(k),n(k), '.b')
xlabel('Scale'); ylabel('N'); grid; set(gca, 'Yminorgrid', 'off')
iprint('sift_hist');