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

该博客介绍了如何使用MATLAB实现灰度图像的腐蚀和膨胀操作。通过扩展边界和迭代计算,定义了两个函数gray_erode和gray_dilate,分别用于图像的腐蚀和膨胀。这两个基本操作在图像处理中常用于形态学变换,能够改变图像的边界特征。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值