类间最大距离法原理

设计思想:在某个适当的阈值下,图像分割后的前景目标与背景两个类的差异最大为最佳分割。两个类别的差异,用两个类别中心与阈值之间的距离差来度量。

分割步骤:

  1. 给定一个初始阈值Th=Th0(例如:可以默认为1),则将原图分为C1和C2两类;

  2. 分别计算两类的灰度均值:

图片1.png

图片2.png

  1. 计算相对距离度量值S:

图片3.png

  1. 选择最佳阈值Th=Th*,使得下式成立:

图片4.png

  1. 利用阈值S将图像分成黑白两个部分。

实现代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111

close all;

clear all;

a=imread('1.jpg');

[m n]=size(a);

total1=zeros(1,254);

total2=zeros(1,254);

num1=zeros(1,254);

num2=zeros(1,254);

s=zeros(1,254);



for th=1:254

     for i=1:m

          for j=1:n

              if(a(i,j)<th)

                  total1(1,th)=double(total1(1,th))+double(a(i,j));

                  num1(1,th)=num1(1,th)+1;

              else 

                  total2(1,th)=double(total2(1,th))+double(a(i,j));

                  num2(1,th)=num2(1,th)+1;

              end

          end

     end

end



for i=1:254

    if (num1(1,i)~=0&num2(1,i)~=0)

        mean1(1,i)=total1(1,i)/num1(1,i);

        mean2(1,i)=total2(1,i)/num2(1,i);

    end

end



for i=1:254

      if (num1(1,i)~=0&num2(1,i)~=0)

        s(1,i)=(mean2(1,i)-double(i))*(double(i)-mean1(1,i))/(mean2(1,i)-mean1(1,i))/(mean2(1,i)-mean1(1,i));

    end

end



for i=1:254

    if(s(1,i)==max(s))

        thresult=i;

    end

end



for i=1:m

    for j=1:n

        if(a(i,j)<thresult)

            b(i,j)=0;

        else

            b(i,j)=255;

        end

    end

end



figure,subplot(1,2,1),imshow(a),subplot(1,2,2),imshow(b,[]);



最终效果

图片5.png