欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在CIELab颜色空间下使用八方向Sobel算子实现边缘检测

程序员文章站 2022-03-29 22:48:17
...

参考河北师范大学硕士学位论文——基于八方向Sobel算子的边缘检测算法研究。
由于自己实现滤波器运算,计算速度很慢,以后有能力再进行改进。


算子定义如下:
在CIELab颜色空间下使用八方向Sobel算子实现边缘检测


算法思路:
1.将RGB图像转化为CIELab颜色空间的图像。
2.计算不同方向上的梯度,乘以权值后取最大值当作输出值。
在CIELab颜色空间下使用八方向Sobel算子实现边缘检测


效果如下:
在CIELab颜色空间下使用八方向Sobel算子实现边缘检测
运行时间:33.543444 秒。

在CIELab颜色空间下使用八方向Sobel算子实现边缘检测
运行时间:79.695009 秒。

跑的好慢QAQ


代码如下:
CIELabEdge.m(算法的总入口)

%在CIELab颜色空间下对图片进行边缘检测(参数为待处理的RGB图像)
%使用论文中的八方向Sobel算子进行滤波
%论文引自--郑英娟. 基于八方向Sobel算子的边缘检测算法研究. 河北师范大学硕士学位论文. 2013.3.26

%From:Yinggang Wang
%Create Date:2018.3.30

function [] = CIELabEdge(img)

%颜色空间转换
Lab = RGB2Lab(img);

%滤波处理
Edge = MyFilter(Lab);

%输出结果
figure('NumberTitle','off','Name','边缘');
imshow(Edge,[]);

figure('NumberTitle','off','Name','边缘(负片)');
imshow(imcomplement(Edge),[]);

MyFilter.m(自己实现的滤波器)

%使用滤波器滤波(参数:CIELab颜色空间的三维矩阵)(图像输出大小比实际小一点)
%
%From:Yinggang Wang
%Create Date:2018.3.30

function im = MyFilter(ori)

s = size(ori);
im = zeros(s(1),s(1,2));

for i = 3:(s(1)-2)
    for j = 3:(s(2)-2)
        im(i-2,j-2) = getOutput(ori,i,j);
    end
end

getOutput.m(得到每一点的输出值)

%得到x,y坐标像素点的输出值
%
%From:Yinggang Wang
%Create Date:2018.3.30

function out = getOutput(ori,x,y)

%以下是八个方向的算子
D0 = [1,2,4,2,1];
D22_5 = [1,2,4,2,4];
D45 = [1,4,2,4,1];
D67_5 = [1,2,4,2,4];
D90 = [1,2,4,2,1];
D112_5 = [1,2,4,2,1];
D135 = [1,4,2,4,1];
D157_5 = [1,2,4,2,4];

a = zeros(1,8);
a(1) = sum(D0 .* [CD(ori(x-2,y-1),ori(x-2,y+1)),CD(ori(x-1,y-1),ori(x-1,y+1)),CD(ori(x,y-1),ori(x,y+1)),CD(ori(x+1,y-1),ori(x+1,y+1)),CD(ori(x+2,y-1),ori(x+2,y+1))]);
a(2) = sum(D22_5 .* [CD(ori(x-2,y),ori(x+2,y)),CD(ori(x-1,y+1),ori(x+1,y-1)),CD(ori(x,y-1),ori(x,y+1)),CD(ori(x-1,y-1),ori(x+1,y+1)),CD(ori(x-1,y),ori(x+1,y))]);
a(3) = sum(D45 .* [CD(ori(x+1,y-2),ori(x+2,y-1)),CD(ori(x,y-1),ori(x+1,y)),CD(ori(x-1,y-1),ori(x+1,y+1)),CD(ori(x-1,y),ori(x,y+1)),CD(ori(x-2,y+1),ori(x-1,y+2))]);
a(4) = sum(D67_5 .* [CD(ori(x,y-2),ori(x,y+2)),CD(ori(x-1,y-1),ori(x+1,y+1)),CD(ori(x-1,y),ori(x+1,y)),CD(ori(x-1,y+1),ori(x+1,y-1)),CD(ori(x,y+1),ori(x,y-1))]);
a(5) = sum(D90 .* [CD(ori(x-1,y-2),ori(x+1,y-2)),CD(ori(x-1,y-1),ori(x+1,y-1)),CD(ori(x-1,y),ori(x+1,y)),CD(ori(x-1,y-2),ori(x+1,y-2)),CD(ori(x-1,y+2),ori(x+1,y+2))]);
a(6) = sum(D112_5 .* [CD(ori(x,y-2),ori(x,y+2)),CD(ori(x-1,y-1),ori(x+1,y+1)),CD(ori(x-1,y),ori(x+1,y)),CD(ori(x-1,y+1),ori(x+1,y-1)),CD(ori(x,y+1),ori(x,y-1))]);
a(7) = sum(D135 .* [CD(ori(x-1,y-2),ori(x-2,y-1)),CD(ori(x,y-1),ori(x-1,y)),CD(ori(x+1,y-1),ori(x-1,y+1)),CD(ori(x+1,y),ori(x,y+1)),CD(ori(x+2,y+1),ori(x+1,y+2))]);
a(8) = sum(D157_5 .* [CD(ori(x-2,y),ori(x+2,y)),CD(ori(x-1,y+1),ori(x+1,y-1)),CD(ori(x,y-1),ori(x,y+1)),CD(ori(x-1,y-1),ori(x+1,y+1)),CD(ori(x-1,y),ori(x+1,y))]);

a(1) = a(1)/10.0;
a(2) = a(2)/13.0;
a(3) = a(3)/12.0;
a(4) = a(4)/13.0;
a(5) = a(5)/10.0;
a(6) = a(6)/13.0;
a(7) = a(7)/12.0;
a(8) = a(8)/13.0;

out = ((max(a)+0.5));

CD.m(求两点的欧式距离)

%求两点的欧式距离
%
%From:Yinggang Wang
%Create Date:2018.3.30

function dis = CD(a,b)
dis = sqrt((a-b)^2 + (a-b)^2 + (a-b)^2);

代码已经上传:点击进入下载页面

相关标签: 边缘检测