用matlab编写,计算以(i,j)为中心点的四个对角是否相异,使用Marr算子提取图像边缘
时间: 2023-07-16 16:15:32 浏览: 121
以下是使用MATLAB编写的代码,实现了计算以(i,j)为中心点的四个对角是否相异,并使用Marr算子提取图像边缘:
```matlab
% 读取图像
img = imread('test.jpg');
% 转为灰度图像
gray_img = rgb2gray(img);
% 定义Marr算子
marr_op = [-1,-1,-1,-1,2,-1,-1,-1,-1];
% 定义输出图像
out_img = zeros(size(gray_img));
% 遍历图像像素
for i = 2:size(gray_img,1)-1
for j = 2:size(gray_img,2)-1
% 计算四个对角的灰度值差异
d1 = abs(gray_img(i-1,j-1)-gray_img(i+1,j+1));
d2 = abs(gray_img(i-1,j+1)-gray_img(i+1,j-1));
d3 = abs(gray_img(i,j-1)-gray_img(i,j+1));
d4 = abs(gray_img(i-1,j)-gray_img(i+1,j));
% 计算Marr算子响应值
response = sum([d1,d2,d3,d4].*marr_op);
% 判断响应值是否大于阈值
if response > 10
out_img(i,j) = 255;
end
end
end
% 显示原图和边缘提取结果
figure;
subplot(1,2,1);
imshow(gray_img);
title('原图');
subplot(1,2,2);
imshow(uint8(out_img));
title('边缘提取结果');
```
需要注意的是,本代码中使用的阈值为10,可以根据实际情况进行调整。另外,Marr算子也可以根据需要进行修改。
阅读全文
相关推荐
















