Threshold segmentation of image processing [global threshold, Otsu threshold and iterative threshold segmentation]

1. Basic definition of threshold segmentation

Threshold segmentation technology is one of the most classic and popular image segmentation methods, and it is also the simplest image segmentation method. The key to this technique is to find an appropriate gray threshold, which is usually selected according to the gray histogram of the image. It uses one or several thresholds to divide the gray level of the image into several parts, and considers that the pixels belonging to the same part are the same object . It can not only greatly compress the amount of data, but also greatly simplify the analysis and processing steps of image information. Threshold segmentation technique is especially suitable for images in which the target and background are in different gray scale ranges. The biggest feature of this method is that it is simple to calculate, and it has been widely used in applications that emphasize operational efficiency.

2. Global Threshold Segmentation

1. Basic principles

Global information can be passed, such as the grayscale histogram of the entire image. If only one threshold is used in the whole image, then this method is called global thresholding, and the whole image is divided into two regions, the target object (black) and the background object (white) . Global Threshold thresholds the grayscale of the entire image to a constant value.

For an image with a relatively obvious pair of objects and background, its grayscale histogram has a bimodal shape, and the pixel value corresponding to the valley between the two peaks can be selected as the global threshold to segment the image into the target object and the background. Its formula is as follows:
insert image description here

Where f(x,y) is the pixel value of point (x,y), g(x,y) is the segmented image, T is the global threshold, and the global threshold is usually obtained through a histogram .

2. Matlab implementation

(1) Implementation code:

% 采用全局阈值对图像进行分割
close all;
clear all;
clc;

I=imread('rice.png');
[width,height]=size(I);
for i=1:width      % 双重for循环逐个像素进行比较计算
    for j=1:height
        if(I(i,j)>130)
            K(i,j)=1;% 将大于全局阈值的像素点置为1(白色)
        else
            K(i,j)=0;% 将小于等于全局阈值的像素点置为0(黑色)
        end
    end
end

subplot(131),imshow(I);
title('原始图像');
subplot(132),imhist(I);
title('原始图像直方图');
subplot(133),imshow(K);
title('全局阈值分割后的图像');

(2) Achievement effect:
insert image description here
2. Otsu threshold segmentation

1. Basic principles

The maximum inter-class variance method, also known as the Otsu algorithm, is derived from the principle of the least square method on the basis of the gray histogram, and has the best segmentation in the statistical sense. Its basic principle is to divide the gray value of the image into two parts with the optimal threshold, so that the variance between the two parts is the largest, that is, it has the largest separation .

Let f(x,) be the gray value at the position (x, y) of the image IxN, and the gray level is L, then f(x, y) belongs to [0, L-1]. If the gray level i The number of all pixels is f, then the probability of the i-level gray level is: the
insert image description here
pixels in the image are divided into two categories according to the gray level with the threshold t, namely the background C0 and the target C1. The gray level of the background CO is 0 ~ t-1, and the gray level of the target C1 is t ~ L-1. The pixels corresponding to the background C0 and the target C1 are respectively: {f(x,y)<1} and {f(x,y)>=t}.
insert image description here
insert image description here

In MATLAB software, the function graythresh() uses the Otsu algorithm to obtain the global threshold value. After obtaining the global threshold value, the function im2bw() can be used for image segmentation .

2. Matlab implementation

(1) Implementation code:

% 采用Ostu算法进行图像分割
close all;
clear all;
clc;
I=imread('coins.png');
I=im2double(I);
% 函数graythresh()采用Ostu算法获取图像(既可以是灰度也可以RGB)的最优阈值,调用格式为level=graythresh(I),level大小介于[0,1之间
T=graythresh(I);
J=im2bw(I,T);

subplot(121),imshow(I);
title('原始图像');
subplot(122),imshow(J);
title('Otsu阈值分割后的图像');

(2) Achievement effect:
insert image description here
4. Iterative threshold segmentation

1. Basic principles

The iterative threshold method is a relatively effective method in the threshold method image segmentation, and iterative method is used to obtain the optimal threshold value of the segmentation, which has a certain degree of self-adaptability. The steps of iterative threshold segmentation are as follows:

(1) Set parameter T0 and select an initial estimated threshold T1.

(2) Segment the image with a threshold T. Divide the image into two parts: G1 is composed of pixels whose gray value is greater than T1, and G2 is composed of pixels whose gray value is less than or equal to T1.

(3) Calculate the average gray value u1 and u2 of all pixels in G1 and G2, and a new threshold T2 = (u1+u2)/2.

(4) If |T2-T1|<T0, then deduce that T2 is the optimal threshold; otherwise, assign T2 to T1, and repeat steps (2) ~ (4) until the optimal threshold is obtained.

2. Matlab implementation

(1) Implementation code:

% 采用迭代式阈值进行图像分割
close all;
clear all;
clc;
I=imread('cameraman.tif');
I=im2double(I);
% 第一步:设置参数T0,并选择一个初始的估计阈值T1(取图像I像素值的最小值和最大值的平均值)
T0=0.01;
T1=(min(I(:))+max(I(:)))/2;
% 第二步:用阈值T1分割图像.将图像分成两部分:r1由灰度值大于T1的像素组成,r2是由灰度小于或等于T1的像素组成
r1=find(I>T1);% find函数返回素有非零元素的位置
r2=find(I<=T1);
% 第三步:计算r1和r2中所有像素的平均灰度值h1和h2以及新的阈值T2=(h1+h2)/2
T2=(mean(I(r1))+mean(I(r2)))/2;
% 第四步:|T2-T1|<T0,则推出T2即为最优阈值;否则,将T2赋值给T1,并重复步骤2-4直到获取最优阈值
if abs(T2-T1)<T0
    J=imbinarize(I,T2);  % 使用imbinarize函数进行图像分割
else
    while abs(T2-T1)>=T0
        T1=T2;
        r1=find(I>T1);
        r2=find(I<=T1);
        T2=(mean(I(r1))+mean(I(r2)))/2;
    end
    J=imbinarize(I,T2);  % 使用imbinarize函数进行图像分割
end

subplot(121),imshow(I);
title('原始图像');
subplot(122),imshow(J);
title('迭代式阈值分割后的图像');

(2) Achievement effect:
insert image description here

Note: In addition to the above three threshold segmentation methods, there are other methods such as adaptive threshold segmentation (local threshold segmentation), maximum entropy threshold segmentation, and so on. But I found that adaptive threshold segmentation and maximum entropy threshold segmentation are basically implemented in C++ or Python, and I haven't found a suitable code for using matlab. If you have a chance in the future, come back and add it!

Since I just started learning image processing, I don't understand a lot of knowledge. If there are any mistakes, please correct me, there is a long way to go, and work slowly!

Guess you like

Origin blog.csdn.net/qq_44111805/article/details/126447143