由于Log和DoG边缘检测时只是卷积的模板不同,因此将二者的程序写在一起,便于比较。且Matlab的edge函数中并没有直接用于DoG检测的调用方式。
自行建立边缘检测模板程序如下:
mask1 = fspecial('gaussian',fsize,2);
mask2 =fspecial('gaussian',fsize,1.5);
op = mask1 - mask2;
LoG、DoG的Matlab实现原程序大体如下:
I = imread('cameraman.tif');
sigma = 2;�fult
% Transform to a double precision intensity image ifnecessary
if ~isa(I,'double')&& ~isa(I,'single')
a = im2single(I);%转化为单精度类型,计算时出现负数的话uint8类型会将其置零
end
[m,n] = size(a);
e = false(m,n);%输出矩阵
rr = 2:m-1; cc=2:n-1; %确定起始开始找零的位置,忽略图像的最外圈。
fsize = ceil(sigma*3) * 2 +1; % choose an odd fsize >6*sigma;确定滤波模板大小
op =fspecial('log',fsize,sigma);%创建一个大小为fsize,高斯分布方差为sigma,再经过拉普拉斯运算后的模板
%若将OP换为下面注释中的的计算方式,便为DoG边缘检测。canny边缘检测 matlab
% mask1 = fspecial('gaussian',fsize,2);
% mask2 = fspecial('gaussian',fsize,1.5);
% op = mask1 - mask2;
%
op = op - sum(op(:))/numel(op);% 将op的sum值归零化
b = imfilter(a,op,'replicate');
thresh =.75*mean2(abs(b)); %近似信噪比值作为log之后找交叉零点的阈值
% 寻找交叉零点: +-, -+ 及他们的转置(即竖直方向的-+、+-)
% We arbitrarily choose the edge to be the negative point
%我们在此规定灰度值为负的像素点作为边缘
[rx,cx] = find( b(rr,cc) < 0& b(rr,cc+1) > 0 ...
& abs( b(rr,cc)-b(rr,cc+1) ) >thresh ); % [- +]
e((rx+1) + cx*m) = 1;
[rx,cx] = find( b(rr,cc-1) >0 & b(rr,cc) < 0 ...
& abs( b(rr,cc-1)-b(rr,cc) ) >thresh ); % [+ -]
e((rx+1) + cx*m) = 1;
[rx,cx] = find( b(rr,cc) < 0& b(rr+1,cc) > 0 ...
& abs( b(rr,cc)-b(rr+1,cc) ) >thresh); % [- +]'
e((rx+1) + cx*m) = 1;
[rx,cx] = find( b(rr-1,cc) >0 & b(rr,cc) < 0 ...
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-33967-3.html
如今美国不可能再有机会给你
海上自卫队实力世界第二
就是阻滞中国的发展
什么屁都敢放