1. 首先是灰度图腐蚀
function eroder = gray_erode(img, stel)
img = double(img);
[rows, cols] = size(img);
[irow, icol] = size(stel);
sortrow = ceil(irow/2);
sortcol = ceil(icol/2);
%扩展边界
tempimg1 = [fliplr(img(:, 2:sortcol)), img, fliplr(img(:, end-sortcol+1:end-1))];
tempimg = [flipud(tempimg1(2:sortrow, :)); tempimg1; flipud(tempimg1(end-sortrow+1:end-1, :))];
clear tempimg1;
%开始腐蚀
eroder = img;
for i = sortrow:rows+sortrow-1
for j = sortcol:cols+sortcol-1
win = tempimg(i-sortrow+1:i+sortrow-1, j-sortcol+1:j+sortcol-1);
eroder(i-sortrow+1, j-sortcol+1) = min( min(win+stel) );
end
end
eroder( eroder<0 ) = 0;
end
2. 灰度图膨胀
function dilater = gray_dilate(img, stel)
img = double(img);
[rows, cols] = size(img);
[irow, icol] = size(stel);
sortrow = ceil(irow/2);
sortcol = ceil(icol/2);
%扩展边界
tempimg1 = [fliplr(img(:, 2:sortcol)), img, fliplr(img(:, end-sortcol+1:end-1))];
tempimg = [flipud(tempimg1(2:sortrow, :)); tempimg1; flipud(tempimg1(end-sortrow+1:end-1, :))];
clear tempimg1;
%开始膨胀
dilater = img;
for i = sortrow:rows+sortrow-1
for j = sortcol:cols+sortcol-1
win = tempimg(i-sortrow+1:i+sortrow-1, j-sortcol+1:j+sortcol-1);
dilater(i-sortrow+1, j-sortcol+1) = max( max(win+stel) );
end
end
end
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- hzar.cn 版权所有 赣ICP备2024042791号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务