数字图像处理(实验5)-图像分割

一、实验目的

1、掌握p参数分割的工作原理和算法实现

2、掌握均匀性度量法分割的工作原理和算法实现

二、实验内容

1实现P-参数法的图像分割的代码。

测试代码:

Im=imread('yw2_g.jpg');

[Im2]=pParam0(Im,0.7974);

imshow(Im2);

function Im2=pParam0(Im,perct):

function Im2=pParam0(Im,perct)
bestDelta=Im;
BestThrd=0;
[m,n]=size(Im);
for Thrd=0:255
    ind1=find(Im<=Thrd);
    ind2=find(Im>Thrd);
    if(~isempty(ind1)&&~isempty(ind2))
        p1=length(ind1)/(m*n);
        p2=length(ind2)/(m*n);
        Delta=abs(p2-perct);
        if(Delta<bestDelta)
            BestThrd=Thrd;
            bestDelta=Delta;
        end
    end
end
Im2=zeros(m,n);
Im2(find(Im>BestThrd))=1;
Im2=logical(Im2);
end

原图: 

链接: https://pan.baidu.com/s/1lAq9OEq7atcjTfANwnWgdQ 提取码: xxk7

 运行结果:

2、实现均匀性度量法的图像分割的代码。

测试代码:

Im=imread('cameraman.tif');

[Im2,BestClThrd]=jyxdl(Im);

imshow(Im2);

function [Im2,BestClThrd]=jyxdl(Im):

function [Im2,BestClThrd]=jyxdl(Im)
BestCost=Inf;
BestC1Thrd=0;
[m,n]=size(Im);
for ClThrd=0:255
    ind1=find(Im<=ClThrd);
    ind2=find(Im>ClThrd);
    if(~isempty(ind1)&&~isempty(ind2))
        mu1=mean(Im(ind1));
        mu2=mean(Im(ind2));
        sigma1_sq=sum((Im(ind1)-mu1).^2);
        sigma2_sq=sum((Im(ind2)-mu2).^2);
        p1=length(ind1)/(m*n);
        p2=length(ind2)/(m*n);
        Cost=p1*sigma1_sq+p2*sigma2_sq;
        if(Cost<BestCost)
            BestClThrd=ClThrd;
            BestCost=Cost;
        end
    end
end
Im2=zeros(m,n);
Im2(find(Im>BestClThrd))=1;
Im2=logical(Im2);
end

原图: 

链接: https://pan.baidu.com/s/1tLw4s3ovM73OCoGw7Glq5A 提取码: k6jb

运行结果:

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值