The principle of image maximum entropy threshold segmentation: make the selected threshold to segment the target area and background area of image gray statistics of the two parts of the maximum information.



Specific description:



1. According to the definition of information entropy, the information entropy of the original image H0 is calculated, and the mean of the maximum and minimum gray scales is selected as the initial threshold T0;



2. According to T0, the image is divided into G1 and G2 regions, with mean values M1 and M2 respectively, and the update threshold T2=0.5*(M1+M2);



3. Calculate the information entropy Hd and Hb of G1 and G2, and compare the size of Hd+Hb and H0. If they are equal or within the specified range, or reach the maximum number of iterations, T2 will be output as the final threshold; otherwise, T0=T2, H0=Hd+Hb, repeat step 2 until the conditions are met.



function ThreshValue = My_MaxEntropy(Imag)

% Maximum entropy calculation threshold

% input:

% Imag: two-dimensional array, numerical value represents gray;

% output:

% ThreshValue: indicates the threshold

[X, Y] = size(Imag);

V_max = max(max(Imag));

V_min = min(min(Imag));

T0 = (V_max + V_min) / 2; % Initial segmentation threshold

h = My_imhist(Imag); % Calculates the histogram of the image

grayp = h/(X*Y); % to find the probability of image pixels

% Calculate the initial entropy

H0 = 0;

for i = 1 : 256

if grayp(i) > 0

H0 = H0 – grayp(i)*log(grayp(i));

end

end

% starts iterating

cout = 100; % Sets the maximum number of iterations

while cout > 0

Tmax = 0; % initialization

T1 = T0;

A1 = 0; % The number of points in the partition region G1

A2 = 0; % Number of points in region G2

B1 = 0; % The sum of gray levels of G1 in the segmentation region

B2 = 0; % Sum of gray levels of G2 segmentation area

For I = 1: X % Calculate gray mean value

for j = 1 : Y

if(Imag(i, j) <= T1)

A1 = A1 + 1;

B1 = B1 + Imag(i, j);

else

A2 = A2 + 1;

B2 = B2 + Imag(i, j);

end

end

end

M1 = B1 / A1; % The average gray level of the segmentation region G1

M2 = B2 / A2; % Average gray level of partition region G2

T2 = (M1 + M2) / 2; % Update threshold

TT = floor(T2);

grayPd = sum(grayp(1 : TT)); % Calculates the sum of probability of partition region G1

if grayPd == 0

grayPd = eps;

end

grayPb = 1 – grayPd;

if grayPb == 0

grayPb = eps;

end

% Calculate the information entropy of G1 and G2 after segmentation

Hd = 0;

Hb = 0;

for i = 1 : 256

if i <= TT

if grayp(i) > 0

Hd = Hd – grayp(i)/grayPd*log(grayp(i)/grayPd);

end

else

if grayp(i) > 0

Hb = Hb – grayp(i)/grayPb*log(grayp(i)/grayPb);

end

end

end

H1 = Hd + Hb; % of the total entropy

% Exit condition

If abs(h0-h1) < 0.0001

Tmax = T2;

break;

else

T0 = T2;

H0 = H1;

end

cout = cout – 1;

end

% returns the threshold

ThreshValue = floor(Tmax);

end



% gray histogram

function h = My_imhist(Imag)

h = zeros(256, 1);

for k = 1 : 256

h(k) = 0;

for i = 1 : size(Imag, 2)

for j = 1 : size(Imag, 2)

if Imag(i, j) == k – 1

h(k) = h(k) + 1;

end

end

end

end

end



———————

Akiko: There’s a guy who loves code

Source: CSDN

The original:Blog.csdn.net/u012366767…

Copyright notice: This article is the blogger’s original article, reprint please attach the blog link!


For more learning materials: Annalin1203