利用MATLAB实现Sobel边缘检测

Sobel边缘检测

先把代码放上来,后续有时间再慢慢补全其他内容吧。

1. 处理流程

2. 算法原理

两个卷积因子怎么来的。

3. 代码示例

%% 本程序实现对于单张图片的Sobel边缘检测
clear
clc
GATE = 128;
A = imread("C:\Users\14751\Desktop\workplace.bmp");

GX = [-1 0 1; -2 0 2; -1 0 1];
GY = [1 2 1; 0 0 0; -1 -2 -1];

A_red   = A(:,:,1);
A_green = A(:,:,2);
A_blue  = A(:,:,3);
[rows,colums] = size(A_red);

A_sobel = zeros(rows,colums,3);
for i = 1:3 % 对每一幅图片进行处理
    A_process = double(A(:,:,i));
    A_temp = zeros(rows,colums);
    fprintf(['正在处理第',num2str(i),'页...\n']);
    for r = 2:(rows-1) % 对于图片的行数
        for c = 2:(colums-1) % 对于图片的列数
            A_tempx = sum(sum(A_process((r-1):(r+1),(c-1):(c+1)).*GX));
            A_tempy = sum(sum(A_process((r-1):(r+1),(c-1):(c+1)).*GY));
            A_temp_cal = sqrt(A_tempx^2 + A_tempy^2);
%             A_temp_cal = abs(A_tempx) + abs(A_tempy); % 一种损失精度的近似
            if(A_temp_cal>GATE)
                A_temp(r,c) = 255;
            else
                A_temp(r,c) = 0;
            end
        end
%         fprintf(['正在处理第',num2str(r),'行...\n']);
    end
    A_sobel(:,:,i) = A_temp;
end
A_sobel = uint8(A_sobel);
figure;
imshow(A_sobel);
lena图像处理前后对比
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虎慕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值