Rober算子
function edgeDiscover_Roderts(image_path,threshold)
data_rgb = imread(image_path);%彩色值矩阵
data_gray = rgb2gray(data_rgb);%灰度矩阵
data_double = im2double(data_gray);%uint8变为double类型
[r,c] = size(data_double);
data_edge = zeros(r,c);%0是黑色,255是白色
for i = 1:r-1
for j = 1:c-1
x = (data_double(i,j)-data_double(i+1, j+1)).^2;
y = (data_double(i+1,j)-data_double(i, j+1)).^2;
xy = sqrt(x+y);
if(xy > threshold)
data_edge(i,j) = 1;
end
end
end
data_roberts = edge(data_gray, 'roberts');
subplot(1,3,1);imshow(data_gray);title('原始灰度图像');
subplot(1,3,2);imshow(data_edge*255);title('自己的roberts算法');
subplot(1,3,3);imshow(data_roberts);title('matlab的roberts算子');
Sobel算子
function edgeDiscover_Sobel(image_path,threshold)
data_rgb = imread(image_path);%彩色值
data_gray = rgb2gray(data_rgb);%灰度
data_double = im2double(data_gray);%uint8变为double类型
[r,c] = size(data_double);
data_edge = zeros(r,c);%0是黑色,255是白色
for i = 2:r-1
for j = 2:c-1
x = (data_double(i-1,j+1)+data_double(i+1,j+1)+2*data_double(i,j+1)...
-data_double(i-1,j-1)-data_double(i+1,j-1)-2*data_double(i,j-1)).^2;
y = (data_double(i-1)+2*data_double(i-1,j)+data_double(i-1,j+1)-...
data_double(i+1, j-1)-2*data_double(i+1,j)-data_double(i+1,j+1)).^2;
xy = sqrt(x+y);
if(xy > threshold)
data_edge(i,j) = 1;
end
end
end
data_roberts = edge(data_gray, 'sobel');
subplot(1,3,1);imshow(data_gray);title('原始灰度图像');
subplot(1,3,2);imshow(data_edge*255);title('my sobel算法');
subplot(1,3,3);imshow(data_roberts);title('matlab自带sobel');
end
二阶微分检测
function edgeDiscover2(image_path, threshold)
data_rgb = imread(image_path);%彩色值
data_gray = rgb2gray(data_rgb);%灰度值
data_double= im2double(data_gray);%double型灰度值
[r,c] = size(data_double);
data_edge = zeros(r,c);%存储边缘检测的图像像素值 0是黑色,255是白色
data_temp = zeros(r,c);%存储二阶每个像素点的二阶微分值
for i = 2:r-1
for j = 2:c-1
%拉普拉斯算子
x = data_double(i+1, j) - 2*data_double(i, j)+data_double(i-1, j);
y = data_double(i, j+1) - 2*data_double(i, j)+data_double(i, j-1);
data_temp(i,j) = x+y;
end
end
for i = 2:r-1
for j = 2:c-1
if(data_temp(i,j) < 0 && data_temp(i,j+1) > 0 && ...
abs(data_temp(i,j) - data_temp(i,j+1)) > threshold)
data_edge(i,j) = 1;% [- +]
end
if(data_temp(i,j-1) > 0 && data_temp(i,j) < 0 && ...
abs(data_temp(i,j-1) - data_temp(i,j)) > threshold)
data_edge(i,j) = 1;% [+ -]
end
if(data_temp(i,j) < 0 && data_temp(i+1,j) > 0 && ...
abs(data_temp(i,j) - data_temp(i+1,j)) > threshold)
data_edge(i,j) = 1;% [- +]'
end
if(data_temp(i-1,j) > 0 && data_temp(i,j) < 0 && ...
abs(data_temp(i-1,j) - data_temp(i,j)) > threshold)
data_edge(i,j) = 1;% [+ -]'
end
end
end
[BW,thresh] = edge(data_gray,'zerocross');
subplot(1,3,1);imshow(data_gray);title('原始灰度图');
subplot(1,3,2);imshow(data_edge*255);title('自己的二阶微分算法');
subplot(1,3,3);imshow(BW);title('matlab的zerocross算法');
end