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

本文介绍了灰度图像处理中的两种基本操作——腐蚀与膨胀,并提供了详细的MATLAB实现代码。通过这两种操作,可以有效地改变图像中目标物的大小与形状,对于图像预处理、特征提取等有着重要的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值