Matlab中如何实现灰度膨胀和灰度腐蚀?

Matlab中如何实现灰度膨胀和灰度腐蚀?,第1张

1、选取函数为f(x)=-|x-6|+5在[1,11]上的图像,画出函数图像。

2、使用strel构建平坦的结构元素,如图。

3、使用平坦的结构元素进睁销行灰度膨胀,设置y轴范围为[0, 8]如指答图。

4、构建高度为[1 1 1]的非平坦结构元素。显示的结果和平坦元素相同,但实际结构不同。悉逗游

5、使用非平坦的结构元素进行灰度膨胀,并画图即可。

就算给了代码, 看起来也要费一番精神的, 特别是算法不熟悉的

clear,clc

h=imread('ceshi2.bmp')

i=im2bw(h)

i1i=187

i1j=192

for ai=181:193

    for aj=186:198

        if(sqrt(double(ai-i1i)^2+double(aj-i1j)^2)<=5)

            i(ai,aj)=1%定义圆形结构元素

        end

    end

end

figure,imshow(i)

i1=i

for i1i=6:205%用B腐蚀A

    for i1j=6:205

        flag=0

        if(i1i>=181&&i1i<=193&&i1j>=186&&i1j<=198)

            continue

        else

            if(i(i1i,i1j)==1)

                for ai=i1i-5:i1i+5

                    for aj=i1j-5:i1j+5

                        if(i1(ai,aj)==0&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))<=5)

                            i(i1i,i1j)=0

                            flag=1

                         break

                        end

                    end

                    if(flag==1)

                        break

                    end

                end

            end

        end

    end

end

figure,imshow(i)

%在上面C的图像上用B进行膨胀

i2=i

for i1i=6:205%用B膨胀C

    for i1j=6:205

        flag=0

        if(i1i>=175&&i1i<=199&&i1j>=180&&i1j<=204)

            continue

        else

            for ai=i1i-5:i1i+5

                for aj=i1j-5:i1j+5   

                辩胡    if(i2(ai,aj)==1&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))<=5)

                        i(i1i,i1j)=1

                        flag=1

                        break

                    end

                 end

                if(flag==1)

                    break

                end

            end

        end

    end

end

figure,imshow(i)

%在上面D的图像上用B进行膨胀

i2=i

for i1i=6:205%用B膨胀D

    for i1j=6:205

        flag=0

        if(i1i>=175&&i1i<=199&&i1j>=180&&i1j<=204)

            continue

        else

            for ai=i1i-5:i1i+5

                for aj=i1j-5:i1j+5                   

                   if(i2(ai,aj)==1&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))<=5)

                        i(i1i,i1j)=1

                        flag=1

                        break

                    end

                end

                if(flag==1)

                    break

                扮灶春end

            end

        end

    end

end

figure,imshow(i)

%在上面E的图像上用B进行腐蚀

i1=i

for i1i=6:205%用B腐蚀E

    for i1j=6:205

        flag=0

        if(i1i>=181&&i1i<=193&&i1j>=186&&i1j<=198)

            continue

        else

            if(i(i1i,i1j)==1)

                for ai=i1i-5:i1i+5

                    for aj=i1j-5:i1j+5                

                       if(i1(ai,aj)==0&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))<=5)

                            i(i1i,i1j)=0

                            flag=1

                            break

         厅耐               end

                    end

                    if(flag==1)

                        break

                    end

                end

            end

        end

    end

end

figure,imshow(i)

在matlab定义里面,这两个函数本来就是橘樱能够作用于灰度图像的,看帮助誉伍桐文件你也能看到

作用与灰度图的例子

当然,膨胀和腐蚀多用于处理二值图像

先把输入的图像二值化成为二值图像,在用这两个庆坦函数就可以了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12212623.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存