MATLAB学习笔记 图像分割(二)

edge(f, ‘method’, par) 边缘检测函数

这里写图片描述

sobel边缘检测器(默认为sobel)

edge(f, ‘sobel’, T, dir);
dir 分为 vertical horizontal both(默认) 三种

f = imread('img16.tif');
g = edge(f, 'sobel', 'vertical');
subplot(1, 2, 1), imshow(f), title('原图');
subplot(1, 2, 2), imshow(g), title('sobel');
  • 输入:
    这里写图片描述
    *输出:
    这里写图片描述
Prewitt边缘检测器

edge(f, ‘prewitt’, T, dir);

f = imread('img16.tif');
g = edge(f, 'prewitt', 'vertical');
subplot(1, 2, 1), imshow(f), title('原图');
subplot(1, 2, 2), imshow(g), title('prewitt');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
Roberts边缘检测器

edge(f, ‘roberts’, T, dir);

f = imread('img16.tif');
g = edge(f, 'roberts', 'vertical');
subplot(1, 2, 1), imshow(f), title('原图');
subplot(1, 2, 2), imshow(g), title('roberts');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
LoG边缘检测器

edge(f, ‘log’, T, sigma);

f = imread('img16.tif');
g = edge(f, 'log', 'vertical');
subplot(1, 2, 1), imshow(f), title('原图');
subplot(1, 2, 2), imshow(g), title('log');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
零交叉检测器

edge(f, ‘zerocross’, T, H);

f = imread('img16.tif');
g = edge(f, 'zerocross', 'vertical');
subplot(1, 2, 1), imshow(f), title('原图');
subplot(1, 2, 2), imshow(g), title('zerocross');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
Canny边缘检测器

edge(f, ‘canny’, T, sigma);

f = imread('img16.tif');
g = edge(f, 'canny', 'vertical');
subplot(1, 2, 1), imshow(f), title('原图');
subplot(1, 2, 2), imshow(g), title('canny');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
使用Sobel
f = imread('img16.tif');
subplot(3, 2, 1), imshow(f), title('原图');
[gv, v] = edge(f, 'sobel', 'vertical');
subplot(3, 2, 2), imshow(gv), title('自动阈值 Sobel 垂直');
[gv, v] = edge(f, 'sobel', 0.15, 'vertical');
subplot(3, 2, 3), imshow(gv), title('0.15阈值 Sobel 垂直');
[gv, v] = edge(f, 'sobel', 0.15);
subplot(3, 2, 4), imshow(gv), title('0.15阈值 Sobel 垂直水平');
w45 = [-2 -1 0; -1 0 1; 0 1 2];
g = imfilter(double(f), w45, 'replicate');
T = 0.3 * max(abs(g(:)));
g = g > T;
subplot(3, 2, 5), imshow(g), title('45%');
w45 = [0 1 2; -1 0 1; -2 -1 0];
g = imfilter(double(f), w45, 'replicate');
T = 0.3 * max(abs(g(:)));
g = g > T;
subplot(3, 2, 6), imshow(g), title('-45%');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
所有检测器比较

sobel prewitt roberts log zerocross canny

f = imread('img16.tif');
[g, v] = edge(f, 'sobel', 'vertical');
subplot(3, 4, 1), imshow(g), title('sobel'), xlabel(v);
[g, v] = edge(f, 'prewitt', 'vertical');
subplot(3, 4, 2), imshow(g), title('prewitt'), xlabel(v);
[g, v] = edge(f, 'roberts', 'vertical');
subplot(3, 4, 3), imshow(g), title('roberts'), xlabel(v);
[g, v] = edge(f, 'log', 'vertical');
subplot(3, 4, 4), imshow(g), title('log'), xlabel(v);
[g, v] = edge(f, 'zerocross', 'vertical');
subplot(3, 4, 5), imshow(g), title('zerocross'), xlabel(v);
[g, v] = edge(f, 'canny', 'vertical');
subplot(3, 4, 6), imshow(g), title('canny'), xlabel(v);

[g, v] = edge(f, 'sobel', 0.05, 'vertical');
subplot(3, 4, 7), imshow(g), title('sobel'), xlabel(v);
[g, v] = edge(f, 'prewitt', 0.05, 'vertical');
subplot(3, 4, 8), imshow(g), title('prewitt'), xlabel(v);
[g, v] = edge(f, 'roberts', 0.06, 'vertical');
subplot(3, 4, 9), imshow(g), title('roberts'), xlabel(v);
[g, v] = edge(f, 'log', 0.003, 2.25, 'vertical');
subplot(3, 4, 10), imshow(g), title('log'), xlabel(v);
[g, v] = edge(f, 'zerocross', 0.003, 'vertical');
subplot(3, 4, 11), imshow(g), title('zerocross'), xlabel(v);
[g, v] = edge(f, 'canny', [0.04, 0.10], 1.5 , 'vertical');
subplot(3, 4, 12), imshow(g), title('canny'), xlabel(v);
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

Hough变换的线检测

sparse(r, c, s, m, n)将矩阵变为稀疏矩阵

这里写图片描述
这里写图片描述

full(S) 可以将稀疏矩阵还原
hough(f, dtheta, drho) 计算Hough变换
f = zeros(101, 101);
f(1, 1) = 1;
f(101, 1) = 1;
f(1, 101) = 1;
f(101, 101) = 1;
f(51, 51) = 1;
H = hough(f);
subplot(1, 2, 1), imshow(f), title('原图');
subplot(1, 2, 2), imshow(H), title('计算');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
利用Hough变换做峰值处理 houghpeaks
  • 找到包含有最大值的hough变换单元并记下它的位置
  • 把第一步中找到的最大值点的邻域中的hough变换单元设为零
  • 重复该步骤,直到找到需要的峰值数时为止,或者达到一个指定的阈值时为止
利用Hough变换做线检测和链接
houghpixels(f, theta, rho, rbin, cbin)

找到图像中影响到峰值的每一个非零值点的位置。

houghlines(f, theta, rho, rr, cc, fillgap, minlength)

该函数采用以下策略
这里写图片描述

f = imread('img16.tif');
f = edge(f, 'canny', [0.04, 0.10], 1.5 , 'vertical');
[H, theta, rho] = hough(f, 0.5);
subplot(1, 2, 1), imshow(imadjust(mat2gray(H)), 'XData', theta, 'YData', rho, 'InitialMagnification', 'fit'), axis on, axis normal;
[r, c] = houghpeaks(H, 5);
hold on;
plot(theta(c), rho(r), 'LineStyle', 'none', 'marker', 's', 'color', 'r');
lines = houghlines(f, theta, rho, r, c);
subplot(1, 2, 2), imshow(f), hold on;
for k = 1:length(lines)
xy = [lines(k).point1 ; lines(k).point2];
plot(xy(:, 2),xy(:,1),'LineWidth', 4, 'Color', 'r');
end
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

阈值处理

* graythresh(f) 获得图像的阈值[0, 1]*

f = imread('coins.png');
T = graythresh(f);
g = im2bw(f, T);
subplot(1, 2, 1), imshow(f), title('原图');
subplot(1, 2, 2), imshow(g), title(T);
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

计算全局阈值

迭代策略
这里写图片描述

f = imread('img17.tif');
T = 0.5 * (double(min(f(:))) + double(max(f(:))));
done = false;
while ~done
    g = f >= T;
    Tnext = 0.5 * (mean(f(g)) + mean(f(~g)));
    done = abs(T - Tnext) < 0.5;
    T = Tnext;
end
subplot(1, 3, 1), imshow(f), title('原图');
subplot(1, 3, 2), imshow(g), title('利用迭代策略');
subplot(1, 3, 3), imshow(im2bw(f, graythresh(tofloat(f)))), title('利用graythresh');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

局部阈值处理

f = imread('img17.tif');
g = imsubtract(imadd(f, imtophat(f, strel('disk', 3))), imtophat(f, strel('disk', 3)));
subplot(1, 3, 1), imshow(f), title('原图');
subplot(1, 3, 2), imshow(g), title('顶帽后');
subplot(1, 3, 3), imshow(im2bw(g, graythresh(tofloat(g)))), title('顶帽后进行阈值处理');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

猜你喜欢

转载自blog.csdn.net/linglian0522/article/details/74002375
今日推荐