基于局部统计可变阈值的图像分割

基于局部统计可变阈值的图像分割

  1. 灰度图上进行
  2. 每个像素点处设一个阈值
  3. 领域
  4. 一般使用领域内标准差,标准差表示对比度
  5. 全局平均还是领域平均看情况自己选择
  6. 求标准差、均值前一定要图像转换为float类型,tofloat不是matlab内置函数,需要自己添加
  7. 分割结果图均为二值图像

使用默认8领域

%默认8领域
I=imread('F:\20191214162428.jpg');
I1=rgb2gray(I);
I1=tofloat(I1);
I2=stdfilt(I1);
figure,imshow(I2);
m=imfilter(I1,1/9*ones(3),'replicate');
m2=mean2(I1);
I3=(I1>15*I2)&(I1>0.4*m);
figure,imshow(I3);

function [out,revertclass] = tofloat(inputimage)

% 类型转换
% 单纯的取 x 用的匿名函数句柄(玩的有点花)
identify = @(x) x;
% 将输入转换为单精度的函数句柄
tosingle = @im2single;

table = {'uint8',tosingle,@im2uint8 

         'uint16',tosingle,@im2uint16 

         'logical',tosingle,@logical

         'double',identify,identify

         'single',identify,identify};
% strcmp(s1,s2),输入字符串的数组可以说任意类型组合
% find 找出其中的真值
classIndex = find(strcmp(class(inputimage),table(:,1)));

if isempty(classIndex)

    error('不支持的图像类型');

end
% 找到对应的转换类型
out = table{classIndex,2}(inputimage);
% 记录对应逆转换类型
revertclass = table{classIndex,3};
end

原图:
在这里插入图片描述
局部标准差图像:
在这里插入图片描述
使用局部平均值的分割结果图:
在这里插入图片描述
使用全局平均值的分割结果图:
在这里插入图片描述
可见使用全局平均值的效果更好

使用自定义领域

%自定义领域
fil=[1,0,1,0,1;
    0,0,1,0,1;
    1,1,0,1,0;
    0,0,0,0,1;
    1,1,1,1,0];
I=imread('F:\20191214162428.jpg');
I1=rgb2gray(I);
I1=tofloat(I1);
I2=stdfilt(I1,fil);
figure,imshow(I2);
fil=fil/sum(fil(:));
m=imfilter(I1,fil,'replicate');
m2=mean2(I1);
I3=(I1>11*I2)&(I1>0.9*m);
figure,imshow(I3);

function [out,revertclass] = tofloat(inputimage)

% 类型转换
% 单纯的取 x 用的匿名函数句柄(玩的有点花)
identify = @(x) x;
% 将输入转换为单精度的函数句柄
tosingle = @im2single;

table = {'uint8',tosingle,@im2uint8 

         'uint16',tosingle,@im2uint16 

         'logical',tosingle,@logical

         'double',identify,identify

         'single',identify,identify};
% strcmp(s1,s2),输入字符串的数组可以说任意类型组合
% find 找出其中的真值
classIndex = find(strcmp(class(inputimage),table(:,1)));

if isempty(classIndex)

    error('不支持的图像类型');

end
% 找到对应的转换类型
out = table{classIndex,2}(inputimage);
% 记录对应逆转换类型
revertclass = table{classIndex,3};
end

分割结果图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值