使用Marr算子提取图像边缘的matlab代码,要求: (1)标注出算子的大小 (2)改变 σ 的值,观察图像的变化
时间: 2024-02-01 11:14:43 浏览: 68
以下是使用Marr算子提取图像边缘的MATLAB代码,其中标注了算子的大小为5x5:
```matlab
img = imread('lena.jpg');
img = rgb2gray(img);
sigma1 = 1;
sigma2 = 2;
[m,n] = size(img);
L = zeros(m,n);
for i = 1:m
for j = 1:n
L(i,j) = img(i,j);
end
end
filter_size = 5; % Marr算子大小为5x5
Marr = zeros(filter_size,filter_size);
for i = 1:filter_size
for j = 1:filter_size
x = i - ceil(filter_size/2);
y = j - ceil(filter_size/2);
Marr(i,j) = -(x^2+y^2)/(2*pi*sigma1^4)*exp(-(x^2+y^2)/(2*sigma1^2));
end
end
for i = 1:m-filter_size+1
for j = 1:n-filter_size+1
patch = L(i:i+filter_size-1,j:j+filter_size-1);
response = sum(sum(patch.*Marr));
L(i+ceil(filter_size/2)-1,j+ceil(filter_size/2)-1) = response;
end
end
figure,imshow(L);
sigma1 = 2;
Marr = zeros(filter_size,filter_size);
for i = 1:filter_size
for j = 1:filter_size
x = i - ceil(filter_size/2);
y = j - ceil(filter_size/2);
Marr(i,j) = -(x^2+y^2)/(2*pi*sigma1^4)*exp(-(x^2+y^2)/(2*sigma1^2));
end
end
for i = 1:m-filter_size+1
for j = 1:n-filter_size+1
patch = L(i:i+filter_size-1,j:j+filter_size-1);
response = sum(sum(patch.*Marr));
L(i+ceil(filter_size/2)-1,j+ceil(filter_size/2)-1) = response;
end
end
figure,imshow(L);
```
运行该代码,将会得到两张提取边缘后的图像,其中第一张使用的是sigma1=1,第二张使用的是sigma1=2。可以发现,当sigma值增大时,边缘提取变得更加模糊。
阅读全文