数字图像处理第八章例题处理
例8.1使用函数bwboundaries和bound2im.
代码如下:
f = [
0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 1 1 1 1 1 1 1 1 1 1 1 1 0;
0 1 1 1 1 1 1 1 1 1 1 1 1 0;
0 1 1 0 0 0 0 0 0 0 0 1 1 0;
0 1 1 0 1 1 1 1 1 1 0 1 1 0;
0 1 1 0 1 1 1 1 1 1 0 1 1 0;
0 1 1 0 1 1 0 0 1 1 0 1 1 0;
0 1 1 0 1 1 0 0 1 1 0 1 1 0;
0 1 1 0 1 1 1 1 1 1 0 1 1 0;
0 1 1 0 1 1 1 1 1 1 0 1 1 0;
0 1 1 0 0 0 0 0 0 0 0 1 1 0;
0 1 1 1 1 1 1 1 1 1 1 1 1 0;
0 1 1 1 1 1 1 1 1 1 1 1 1 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0
];
B = bwboundaries(f, ‘noholes’);
numel(B)
b = cat(1, B{:});
[M, N] = size(f);
imgae = bound2im(b, M, N)
[B, L, NR, A] = bwboundaries(f);
numel(B)
numel(B) - NR
bR = cat(1, B{1:2}, B{4});
imageBoundaries = bound2im(bR, M, N);
imageNumveredBoundaries = imageBoundaries.*L
bR = cat(1, B{:});
imageBoundaries = bound2im(bR, M, N);
imageNumberedBoundaries = imageBoundaries.*L
find(A(:,1))
find(A(1,:))
A
full(A)
结果图:
例8.2弗雷曼链码及其某些变化
代码:
f = imread(‘C:\NO.8\DIP3E_CH11_Original_Images\Fig1105(a)(noisy_stroke).tif’);
figure;subplot(2,2,1),imshow(f),title(‘(a)带噪原图’);
h = fspecial(‘average’, 9);
g = imfilter(f, h ,‘replicate’); % 因为感兴趣的是粗边界,所以进行平滑处理可有效抑制噪声
subplot(2,2,2),imshow(g),title(‘(b)均值模板平滑处理结果’);
T = graythresh(g);
gB = imbinarize(g,T); % 使用Ostu方法对图像进行阈值处理
subplot(2,2,3),imshow(gB),title(‘©Ostu方法对平滑图像阈值的结果’);
B =bwboundaries(gB,‘noholes’); % 提取图像边界
d = cellfun(‘length’,B);
[max_d, k] = max(d); % k是最长边界对应的区域数
b = B{k(1)}; % 得到最长边界的坐标
[M,N] = size(g);
g = bound2im(b,M,N); % 构建最长边界对应的二值图像
subplot(2,2,4),imshow(g),title(‘(d)图c的最长外部边界’);
[s,su] = bsubsamp(b,50); % 对最长边界二次取样,节点间距为50像素(约为图像宽度的10%,图像宽度为570)
g2 = bound2im(s,M,N); % 构建取样后的边界对应的二值图像
figure;imshow(g2),title(‘(e)对最长边界取样后的边界图像’);
cn = connectpoly(s(:, 1),s(:, 2)); % 对取样后边界进行连接
g3 = bound2im(cn,M,N); % 将感兴趣的边界显示为二值图像
figure; imshow(g3),title(‘(f)对取样后的点进行连接’);
c = fchcode(su); %产生链码
c.x0y0
c.fcc
c.mm
c.diff
c.diffmm
结果图:
例8.3得到包围一个区域的边界的细胞组合体
代码:
f=imread(‘C:\NO.8\matlab教材示例图片\1.tif’);
g=bwperim(f,8);
Q=qtdecomp(g,0,2);
R=imfill(gF,‘holes’)&g;
B=bwboundaries(R,4,‘holes’);
b=B{1};%There is only one boundary in this case.
IN=inpolygon(X,Y,xv,yv)
结果图:
例8.4使用函数 im2minperpoly
代码:
clc,clear,close all;
f = imread(‘C:\NO.8\DIP3E_CH11_Original_Images\Fig1108(a)(mapleleaf).tif’);
figure
subplot(321),imshow(f)
B = bwboundaries(f, 4, ‘noholes’);
b = B{1};
[M, N] = size(f);
bOriginal = bound2im(b, M, N);
subplot(322),imshow(bOriginal)
[X, Y] = im2minperpoly(f, 2);
b2 = connectpoly(X, Y);
bCellsize2 = bound2im(b2, M, N);
subplot(323),imshow(bCellsize2)
[X, Y] = im2minperpoly(f, 3);
b2 = connectpoly(X, Y);
bCellsize2 = bound2im(b2, M, N);
subplot(324),imshow(bCellsize2)
[X, Y] = im2minperpoly(f, 4);
b2 = connectpoly(X, Y);
bCellsize2 = bound2im(b2, M, N);
subplot(325),imshow(bCellsize2)
[X, Y] = im2minperpoly(f, 8);
b2 = connectpoly(X, Y);
bCellsize2 = bound2im(b2, M, N);
subplot(326),