MATLAB边缘检测
sobel算子
二阶Laplacian算子
Kirsch算子
LoG(高斯-拉普拉斯)算子
边缘检测函数edge
题目1:编程实现,使用一阶微分sobel算子进行边缘检测(不能使用edge函数)。
-1 | -2 | 1 | -1 | 0 | 1 | |
---|---|---|---|---|---|---|
0 | 0 | 0 | -2 | 0 | 2 | |
1 | 2 | 1 | -1 | 0 | 1 |
-
算法思想或步骤
使用给出的算子模板卷积即可。
-
程序代码
clc;
close all;
clear all;
[filename filepath] = uigetfile('* *', '图片选择');
im = imread([filepath filename]);
if numel(size(im) ) > 2
im = rgb2gray(im);
end
[row col] = size(im);
im = im2double(im);
g1 = im; g2 = im;
%卷积模板1
M1 = [-1,-2,1; 0,0,0; 1,2,1];
M2 = [-1,0,1; -2,0,2; -1,0,1];
for i = 2 : row - 1
for j = 2 : col - 1
g1(i , j) = sum(sum(im(i-1 : i+1 , j-1 : j+1) .* M1));
g2(i , j) = sum(sum(im(i-1 : i+1 , j-1 : j+1) .* M2));
end
end
figure;
subplot(221); imshow(im); title('原始图像');
subplot(222); imshow(g1); title('sobel1卷积后图像');
subplot(223); imshow(g2); title('sobel2卷积后图像');
- 结果及分析
题目2:编程实现,使用二阶Laplacian算子对灰度图像进行边缘检测(不能使用edge函数),并实现灰度图像的增强处理
0 | 1 | 0 | 1 | 1 | 1 | |
---|---|---|---|---|---|---|
1 | -4 | 1 | 1 | -8 | 1 | |
0 | 1 | 0 | 1 | 1 | 1 |
-
算法思想或步骤
使用给出的算子模板卷积即可。
- 程序代码
clc;
close all;
clear all;
[filename filepath] = uigetfile('* *', '图片选择');
im = imread([filepath filename]);
if numel(size(im) ) > 2
im = rgb2gray(im);
end
[row col] = size(im);
im = im2double(im);
g1 = im; g2 = im;
%卷积模板1
M1 = [0,1,0; 1,4,1; 0,1,0];
M2 = [1,1,1; 1,-8,1; 1,1,1];
for i = 2 : row - 1
for j = 2 : col - 1
g1(i , j) = sum(sum(im(i-1 : i+1 , j-1 : j+1) .* M1));
g2(i , j) = sum(sum(im(i-1 : i+1 , j-1 : j+1) .* M2));
end
end
figure;
subplot(221); imshow(im); title('原始图像');
subplot(222); imshow(g1); title('Laplacian1卷积后图像');
subplot(223); imshow(g2); title('Laplacian2卷积后图像');
- 结果及分析
题目3:Kirsch算子是一种8方向均不相同(8个方向的检测模板如下所示)的边缘检测算子,请用该算子8方向检测模板对图像进行边缘检测。
-
算法思想或步骤
使用给出的算子模板将8个结果分别存放在8层的三维矩阵中。
- 程序代码
clc;
close all;
clear all;
[filename filepath] = uigetfile('* *', '图片选择');
im = imread([filepath filename]);
if numel(size(im) ) > 2
im = rgb2gray(im);
end
[row col] = size(im);
im = im2double(im);
g = zeros(row, col, 8);
M = zeros(3,3,8);
%卷积模板1
M(:,:,1) = [-3,-3,5; -3,0,5; -3,-3,5];
M(:,:,2) = [-3,5,5; -3,0,5; -3,-3,-3];
M(:,:,3) = [5,5,5; -3,0,3; -3,-3,-3];
M(:,:,4) = [5,5,-3; 5,0,-3; -3,-3,-3];
M(:,:,5) = [5,-3,-3; 5,0,-3; 5,-3,-3];
M(:,:,6) = [-3,-3,-3; 5,0,-3; 5,5,-3];
M(:,:,7) = [-3,-3,-3; -3,0,-3; 5,5,5];
M(:,:,8) = [-3,-3,-3; -3,0,5; -3,5,5];
for i = 2 : row - 1
for j = 2 : col - 1
for k = 1 : 8
g(i,j,k) = sum(sum(im(i-1 : i+1, j-1 : j+1) .* M(:,:,k)));
end
end
end
figure;
imshow(im); title('原始图像');
figure;
subplot(221); imshow(g(:,:,1)); title('Kirsch 0°卷积后图像');
subplot(222); imshow(g(:,:,2)); title('Kirsch 45°卷积后图像');
subplot(223); imshow(g(:,:,3)); title('Kirsch 90°卷积后图像');
subplot(224); imshow(g(:,:,4)); title('Kirsch 135°卷积后图像');
figure;
subplot(221); imshow(g(:,:,5)); title('Kirsch 180°卷积后图像');
subplot(222); imshow(g(:,:,6)); title('Kirsch 225°卷积后图像');
subplot(223); imshow(g(:,:,7)); title('Kirsch 270°卷积后图像');
subplot(224); imshow(g(:,:,8)); title('Kirsch 315°卷积后图像');
- 结果与分析
题目4:LoG(高斯-拉普拉斯)算子的边缘检测模板如下,请用该模板对图像的边缘进行检测
-
算法思想或步骤
使用给给出的算子模板卷积即可。
-
程序代码
clc;
close all;
clear all;
[filename filepath] = uigetfile('* *', '图片选择');
im = imread([filepath filename]);
if numel(size(im) ) > 2
im = rgb2gray(im);
end
[row col] = size(im);
im = im2double(im);
g = im;
%卷积模板1
M = [-2,-4,-4,-4,-2; -4,0,8,0,-4; -4,8,24,8,-4; -4,0,8,0,-4; -2,-4,-4,-4,-2];
for i = 3 : row - 2
for j = 3 : col - 2
g(i,j) = sum(sum(im(i-2 : i+2, j-2 : j+2) .* M));
end
end
figure;
subplot(121); imshow(im); title('原始图像');
subplot(122); imshow(g); title('LoG卷积后图像');
- 结果与分析
题目5:使用Matlab提供的边缘检测函数edge对图像的边缘进行检测,把该函数提供的所有检测方法全部试一下
-
算法思想或步骤
-
程序代码
clc;
close all;
clear all;
[filename filepath] = uigetfile('* *', '图片选择');
im = imread([filepath filename]);
if numel(size(im) ) > 2
im = rgb2gray(im);
end
[row col] = size(im);
im = im2double(im);
BW1 = edge(im);
BW2 = edge(im, 'sobel');
BW3 = edge(im, 'prewitt');
BW4 = edge(im, 'roberts');
BW5 = edge(im, 'log');
figure;
subplot(131); imshow(im); title('原始图像');
subplot(132); imshow(BW1); title('默认edge图像');
subplot(133); imshow(BW2); title('sobel图像');
figure;
subplot(131); imshow(BW3); title('prewitt图像');
subplot(132); imshow(BW4); title('roberts图像');
subplot(133); imshow(BW5); title('log图像');
- 结果与分析
实训的收获与体会
-
遇到问题1:问题描述,解决思路,如何解决,解决的效果如何?
-
问题1
在进行卷积时,循环体内要写很多加法,遇到5*5的卷积模板就要写25个,代码不精简。
对于二维矩形求和可以使用两次sum,首先可以在前面将算子矩阵写出来,然后再在运算时可以用点乘运算来代替加法。
-
-
尚未解决的问题
无。