Sobel边缘检测(2)-matlab
clc
clear
clear all
close all
%%%对图像做均值滤波处理
img = imread('1.png');
figure(1)
subplot(1,2,1),imshow(img),title('原始图像')
%%%将彩色图像转灰度图像
img_gray = rgb2gray(img);
subplot(1,2,2),imshow(img_gray),title('RGB-GRAY灰度图像')
BW1=edge(img_gray,'sobel'); %用Sobel算子进行边缘检测
BW2=edge(img_gray,'roberts');%用Roberts算子进行边缘检测
BW3=edge(img_gray,'prewitt'); %用Prewitt算子进行边缘检测
BW4=edge(img_gray,'log'); %用Log算子进行边缘检测
BW5=edge(img_gray,'canny'); %用Canny算子进行边缘检测
figure(2)
subplot(2,3,1),imshow(img_gray),title('原始图像')
subplot(2,3,2),imshow(BW1),title('Sobel算子图像')
subplot(2,3,3),imshow(BW2),title('Roberts算子图像')
subplot(2,3,4),imshow(BW3),title('Prewitt算子图像')
subplot(2,3,5),imshow(BW4),title('Log算子图像')
subplot(2,3,6),imshow(BW5),title('Canny算子图像')
figure(3);
Sobel_Img = img_gray;
Sobel_Threshold = 30;
[ROW,COL] = size(Sobel_Img);
for r = 2:ROW-1
for c = 2:COL-1
Sobel_x = img_gray(r-1,c+1) + 2*img_gray(r,c+1) + img_gray(r+1,c+1) - img_gray(r-1,c-1) - 2*img_gray(r,c-1) - img_gray(r+1,c-1);
Sobel_y = img_gray(r-1,c-1) + 2*img_gray(r-1,c) + img_gray(r-1,c+1) - img_gray(r+1,c-1) - 2*img_gray(r+1,c) - img_gray(r+1,c+1);
Sobel_Num = abs(Sobel_x) + abs(Sobel_y);
%Sobel_Num = sqrt(Sobel_x^2 + Sobel_y^2);
if(Sobel_Num > Sobel_Threshold)
Sobel_Img(r,c)=0;
else
Sobel_Img(r,c)=255;
end
end
end
imshow(Sobel_Img);
wq = 1;