資源簡介
圖像邊緣檢測CANNY算子源碼,能夠快速檢測到圖象邊緣
代碼片段和文件信息
function?CVHomework1
????????I?=?imread(‘001.bmp‘);
????????I=rgb2gray(I)
????????BW?=?canny_edge(I2.5);
????????figure(1)
????????imshow(I)
????????figure(2)
????????imshow(BW)
%?Canny邊緣檢測的函數
%?Input:
%???a:?input?image
%???sigma:?Gaussian的均方差
function?e=canny_edge(asigma)
a?=?double(a);?????%?將圖像像素數據轉換為double類型
[mn]?=?size(a);
e?=?repmat(logical(uint8(0))mn);??%?生成初始矩陣
OffGate?=?0.0001;??
Perc?=?0.7;
Th?=?0.4;
pw?=?1:30;?????????????%?hardcoding.?But?it‘s?large?enough?if?sigma?isn‘t?too?large
sig2?=?sigma?*?sigma;??%?方差
width?=?max(find(exp(-(pw.*pw)/(2*sig2))?>?OffGate));??%?尋找截斷點
t?=?(-width:width);
len?=?2*width+1;
t3=[t-0.5;t;t+0.5];
dgau?=?(-t.*exp(-(t.*t)/(2*sig2))/sig2).‘;??????????????%?一階高斯函數的導數
ra?=?size(a1);??%?圖像行數
ca?=?size(a2);??%?圖像列數
ay?=?255*a;
ax?=?255*a‘;
ax?=?conv2(axdgau‘same‘).‘;???%?高斯平滑濾波后的圖像的x方向梯度
ay?=?conv2(aydgau‘same‘);?????%?高斯平滑濾波后的圖像的y方向梯度
mag?=?sqrt((ax.*ax)+(ay.*ay));??%?每個像素點的梯度強度
magmax?=?max(mag(:));
if?magmax>0
????mag?=?mag/magmax;??%?歸一化
end
[countsx]?=?imhist(mag64);????%?直方圖統計
high?=?min(find(cumsum(counts)>Perc*m*n))/64;
low?=?Th*high;
thresh?=?[low?high];???%?根據直方圖統計確定上下限
%?Nonmax-suppression
idxStrong?=?[];
for?dir?=?1:4
????Localmax?=?Findlocalmax(diraxaymag);
????idxWeak?=?Localmax(mag(Localmax)>low);
????e(idxWeak)?=?1;
????idxStrong?=?[idxStrong;?idxWeak(mag(idxWeak)>high)];
end
rstrong?=?rem(idxStrong-1m)+1;
評論
共有 條評論