matlab sobel canny prewitt 边缘检测算子



clc

clear all


I=imread('1.jpg');
I0=rgb2gray(I);
subplot(221);
imshow(I);
title('原图');


threshold = 0.3;


% BW1=edge(I0,'Roberts',threshold);
% subplot(232);
% imshow(BW1);
% title('Robert算子边缘检测')
 
BW2=edge(I0,'Sobel',threshold);
subplot(222);
imshow(BW2);
title('Sobel算子边缘检测')
 
BW3=edge(I0,'Prewitt',threshold);
subplot(223);
imshow(BW3);
title('Prewitt算子边缘检测');
 
% BW4=edge(I0,'LOG',threshold);
% subplot(235);
% imshow(BW4);
% title('LOG算子边缘检测')
 
BW5=edge(I0,'Canny',threshold);
subplot(224);
imshow(BW5);
title('Canny算子边缘检测')
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是分别使用Sobel、PrewittRoberts、LoG、Canny算子进行边缘检测MATLAB代码示例: 1. Sobel算子 ```matlab % 读入图像 img = imread('image.jpg'); % 转为灰度图像 gray_img = rgb2gray(img); % Sobel算子 h = fspecial('sobel'); % 水平方向差分 hx = imfilter(double(gray_img), h'); % 垂直方向差分 hy = imfilter(double(gray_img), h); % 计算梯度幅值 grad = sqrt(hx.^2 + hy.^2); % 二值化处理 threshold = 50; % 设置阈值 binary_img = grad > threshold; % 显示结果 imshow(binary_img); ``` 2. Prewitt算子 ```matlab % 读入图像 img = imread('image.jpg'); % 转为灰度图像 gray_img = rgb2gray(img); % Prewitt算子 h = fspecial('prewitt'); % 水平方向差分 hx = imfilter(double(gray_img), h'); % 垂直方向差分 hy = imfilter(double(gray_img), h); % 计算梯度幅值 grad = sqrt(hx.^2 + hy.^2); % 二值化处理 threshold = 50; % 设置阈值 binary_img = grad > threshold; % 显示结果 imshow(binary_img); ``` 3. Roberts算子 ```matlab % 读入图像 img = imread('image.jpg'); % 转为灰度图像 gray_img = rgb2gray(img); % Roberts算子 h1 = [1 0; 0 -1]; h2 = [0 1; -1 0]; % 水平方向差分 hx = imfilter(double(gray_img), h1); % 垂直方向差分 hy = imfilter(double(gray_img), h2); % 计算梯度幅值 grad = sqrt(hx.^2 + hy.^2); % 二值化处理 threshold = 50; % 设置阈值 binary_img = grad > threshold; % 显示结果 imshow(binary_img); ``` 4. LoG算子 ```matlab % 读入图像 img = imread('image.jpg'); % 转为灰度图像 gray_img = rgb2gray(img); % LoG算子 h = fspecial('log'); % 边缘检测 edge_img = edge(gray_img, 'zerocross', [], h); % 显示结果 imshow(edge_img); ``` 5. Canny算子 ```matlab % 读入图像 img = imread('image.jpg'); % 转为灰度图像 gray_img = rgb2gray(img); % Canny算子 sigma = 2; % 高斯核标准差 threshold_low = 0.1; % 低阈值 threshold_high = 0.3; % 高阈值 % 边缘检测 edge_img = edge(gray_img, 'canny', [threshold_low threshold_high], sigma); % 显示结果 imshow(edge_img); ``` 以上代码仅供参考,实际使用时需要根据具体情况进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值