当前位置:网站首页>MATLAB image processing - Otsu threshold segmentation (with code)
MATLAB image processing - Otsu threshold segmentation (with code)
2022-06-12 08:06:00 【HNU_ Liu Yuan】
Catalog
Otsu threshold
Otsu threshold is also called maximum variance threshold , yes 1979 Proposed by Japan's Otsu exhibition in , It is derived from the principle of discrimination and least square method , The basic idea is to maximize the variance between classes , The optimal threshold is obtained .
Algorithm flow
Make
Indicates a picture with the size of
Pixels in a digital image
Different gray levels ,
Indicates that the gray level is
The number of pixels , The total number of pixels in the image is
. The normalized histogram has components
, There is

Now? , Suppose you choose a threshold
, And use it to thresholding the input image into two types
and
, among
The gray value in the image is in the interval
All pixels in the ,
By the gray value in the interval
All pixels in the . With this threshold , Pixels are classified into classes
The probability of
Given by the following cumulative sum :

Change the angle , It's kind of
Probability of occurrence . Allied , class
The probability of that happening is zero :

Assign to class
The average gray value of is :

Allied , Assign to class
The average gray value of is :

To the first k The cumulative mean value of level ( Average grayscale ) Given by the following formula :

The average gray level of the whole image is given by the following formula :

To evaluate the segmentation quality of the selected threshold , Define the variance between classes σ2
by :
![]()
The above formula can be reduced to :

As can be seen from the above formula , Calculate different thresholds k Between class variance
, You just have to calculate m and
Two parameters . To get the optimal threshold
, from
Choose different k Calculate the variance between classes
, When
Obtained at maximum k Is the maximum variance threshold .
The flow chart shows

result

As you can see from the diagram , After Otsu threshold segmentation, we can basically get two parts , You can extract parts of the moon from the picture . Can quickly and effectively find the segmentation threshold between classes , But its shortcomings are also obvious , That is, it can only be divided for a single target , Or the objects of interest belong to the same gray range .
Code
clear; clc;
I=rgb2gray(imread('moon.jpg'));
subplot(1, 2, 1)
imshow(I);
xlabel('(a) original image ');
% level = graythresh(I); % Use MATLAB Function to calculate the threshold
% BW = im2bw(I, level);
% subplot(1, 3, 2)
% imshow(BW);
% xlabel('(b) graythresh');
% disp(['graythresh Calculate the gray threshold :', num2str(level*255)]);
T = Otsu(double(I)); % Use Otsu method to calculate the threshold
disp([' Otsu method to calculate the gray threshold :', num2str(T)])
BW = im2bw(I, T/255);
% Threshold segmentation
subplot(1, 2, 2)
imshow(BW);
xlabel('(c) Dajin law ');
function ThreshValue = Otsu(Imag)
% Otsu method to calculate the threshold
% Input :
% Imag: Two dimensional array , Numerical values represent grayscale ;
% Output :
% ThreshValue: threshold
iMax = max(Imag(:)); % Maximum
iMin = min(Imag(:)); % minimum value
T = iMin:iMax; % Gray value range
Tval = zeros(size(T)); % variance
[iRow, iCol] = size(Imag); % Data dimension size
imagSize = iRow*iCol; % Number of pixels
% Traversal gray value , Calculate variance
for i = 1 : length(T)
TK = T(i);
iFg = 0; % prospects
iBg = 0; % background
FgSum = 0; % Total prospects
BgSum = 0; % Total number of backgrounds
for j = 1 : iRow
for k = 1 : iCol
temp = Imag(j, k);
if temp > TK
iFg = iFg + 1; % Foreground pixel statistics
FgSum = FgSum + temp;
else
iBg = iBg + 1; % Background pixel statistics
BgSum = BgSum + temp;
end
end
end
w0 = iFg/imagSize; % Foreground ratio
w1 = iBg/imagSize; % Background proportion
u0 = FgSum/iFg; % Average value of foreground gray level
u1 = BgSum/iBg; % Average value of background gray level
Tval(i) = w0*w1*(u0 - u1)*(u0 - u1); % Calculate variance
end
[~, flag] = max(Tval); % The maximum subscript
ThreshValue = T(flag);
end边栏推荐
- Fundamentals of Mathematics - Taylor Theorem
- Topic 1 Single_ Cell_ analysis(2)
- Compiling principle on computer -- functional drawing language (I)
- 20220524 backbone deep learning network framework
- Leetcode notes: Weekly contest 276
- Servlet
- Rnorm function of R language generates positive distribution data, calculates descriptive statistical summary information of vector data using sum function of epidisplay package, and visualizes ordere
- Improvement of hash function based on life game (continued 1)
- 二、八、十、十六进制相互转换
- Servlet advanced
猜你喜欢

Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water

PPP agreement

HDLC protocol

『Three.js』辅助坐标轴

Cookies and sessions

Clarify the division of IPv4 addresses

Dynamic simulation method of security class using Matlab based Matpower toolbox

Rich dad, poor dad Abstract

The computer is connected to WiFi but can't connect to the Internet

The project file contains toolsversion= "14.0". This toolset may be unknown or missing workarounds
随机推荐
FPGA based communication system receiver [packet detection] development document
Visual studio code batch comment and uncomment
2.1 链表——移除链表元素(Leetcode 203)
Mathematical Essays: Notes on the angle between vectors in high dimensional space
Improvement of hash function based on life game
Conda創建虛擬環境報錯,問題解决
TMUX common commands
Introduction to coco dataset
2021.10.26 scientific research log
Some summaries of mathematical modeling competition in 2022
Servlet
Upgrade eigen to version 3.3.5 under Ubuntu 16.04
Vision Transformer | Arxiv 2205 - LiTv2: Fast Vision Transformers with HiLo Attention
2021.11.3-7 scientific research log
Getting started with Jetson nano Series IV: common skills of NVIDIA Jetson nano
OpenMP task 原理與實例
Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water
Ecmascript6 interview questions
Architecture and performance analysis of convolutional neural network
C # hide the keyboard input on the console (the input content is not displayed on the window)