数字图像处理——图像分割

基本全局阈值算法:

1.设置初始灰度值T,可设为图像的平均灰度值

2.用T把图像分成两部分G1和G2,G1的灰度值大于T,G2的灰度值小于等于T

3.计算G1,G2的平均灰度值m1,m2

4.更新T=(m1+m2)/2

5.重复2~4直到T的变化量小于某个很小的阈值

Matlab代码:

clc,clear
f1 = imread('blobz1.png');
f2 = imread('blobz2.png');
figure;
subplot(1,2,1),imhist(f1),title('原图像(光照均匀)');
subplot(1,2,2),imhist(f2),title('原图像(光照不均匀)');
figure;
subplot(1,2,1),imshow(f1),title('光照均匀');
subplot(1,2,2),imshow(f2),title('光照不均匀');

[h1,w1] = size(f1);
[h2,w2] = size(f2);
delta_T = 1;
T_last = sum(f1(:))/(h1*w1);  % 用图像的平均灰度作为初始值
T_new = 125;
while abs(T_new - T_last) > delta_T
     g1 = f1(logical(f1>T_last));   % 找出阈值以上的像素
     c1 = sum(sum(logical(f1>T_last)));
     m1 = sum(sum(g1))/c1;  % 计算g1的平均灰度值
     g2 = f1(logical(f1<=T_last));  % 找出阈值以下的像素
     c2 = sum(sum(logical(f1<=T_last)));
     m2 = sum(sum(g2))/c2;
     T_last = T_new;
     T_new = (m1+m2)/2;     % 更新阈值
end
T_last = floor(T_last)
f1(logical(f1>T_last)) = 255;   % 把背景置为最亮
f1 = uint8(f1);

T_last = sum(sum(f2))/(h2*w2);  % 用图像的平均灰度作为初始值
T_new = 150;
while abs(T_new - T_last) > delta_T
     g1 = f2(logical(f2>T_last));   % 找出阈值以上的像素
     c1 = sum(sum(logical(f2>T_last)));
     m1 = sum(sum(g1))/c1;
     g2 = f2(logical(f2<=T_last));  % 找出阈值以下的像素
     c2 = sum(sum(logical(f2<=T_last)));
     m2 = sum(sum(g2))/c2;     % 计算g2的平均灰度值
     T_last = T_new;
     T_new = (m1+m2)/2;     % 更新阈值
end
T_last = floor(T_last)
f2(logical(f2>T_last)) = 255;   % 把背景置为最亮
f2 = uint8(f2);

figure;
subplot(1,2,1),imshow(f1),title('光照均匀');
subplot(1,2,2),imshow(f2),title('光照不均匀');

测试图像:

运行结果:

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值