Bootstrap

matlab一维形态学,MATLAB实现灰度图像形态学(膨胀、腐蚀)

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

;