A list,

1 Threshold segmentation principle:

An image includes target, background and noise. A certain threshold T is set to divide the image into two parts: pixel group larger than T and pixel group smaller than T.



In practice, 255 is used to represent the background and 0 is used to represent the object for display purposes.

Since the actual image target and background may not be simply distributed in two gray ranges, two or more thresholds are needed to extract the target.



Image thresholding segmentation is one of the most commonly used traditional image segmentation methods. Because of its simple implementation, small computation and stable performance, it has become the most basic and most widely used segmentation technology in image segmentation. It is especially suitable for images where the target and background occupy different grayscale ranges. The difficulty is how to choose an appropriate threshold to achieve better segmentation.

2 Maximum variance threshold

The basic idea of maximum variance threshold is to divide the histogram into two groups at a certain threshold, and determine the threshold when the variance between the two groups is maximum.

A histogram is a statistical representation of an image, represented by a series of longitudinal stripes of varying heights. Gray histogram is a function of gray level. It represents the number of pixels with each gray level in the image and reflects the frequency of each gray level in the image (i.e. the statistics of the number of pixels from 0 to 255 in the image).

Gray histogram describes the proportion of various gray levels in the image (for the image with 8-bit pixel depth, there are 256 values ranging from 0 to 255) in the whole image.

As shown in the figure below, the abscissa of the gray histogram is the gray level, and the ordinate is the frequency of the gray level, which is the most basic statistical feature of the image.



3. Select threshold by bimodal method

The principle of bimodal method is that the image is composed of foreground and background or two colors, and in the gray histogram, two colors

The distribution of gray value of color pixel forms peak state. The lowest point between the peaks is the threshold of image segmentation

In accordance with this principle, the threshold can be simply calculated to carry out image segmentation. Show the histogram to determine the threshold

Value. However, this method tends to lose some image details

That is, in the figure below, the target and background can be separated by binarization segmentation with Zt as the threshold.



4 Select threshold by iterative method

4.1 Calculate the maximum and minimum gray values of the image, denoted as Pmax and Pmin respectively, and make the initial threshold T0=(Pmax+Pmin)/2; 4.2 according to the threshold T(k)(k=0,1,2… K) Divide the image into foreground and background, and calculate their average gray values H1 and H2 respectively; 4.3 Finding the new threshold T(k+1)=(H1+H2)/2; 4.4 If T(k)=T(k+1), the threshold value is obtained; Otherwise, go to 2 and iterate

5 Select threshold by Otsu method

5.1 Calculate the number of pixels corresponding to each gray order from 0 to 255 and save them in an array, whose subscript is the gray value. The saved content is the current gray value corresponding to the pixel number 5.2 Calculate the average gray level of background image and the proportion of pixel number of background image 5.3 Calculate the average gray level of foreground image and the proportion of pixel number of foreground image 5.4 Go through each gray level from 0 to 255 to calculate and find the maximum value of inter-class variance

Otsu method is a simple and efficient method for self-adaptive calculation of single threshold, or (Otsu). Otsu method was proposed by Otsu in 1979. For Image Image, t is denoted as the threshold of foreground and background segmentation, the proportion of foreground points to Image is W0, and the average gray level is U0. The proportion of background points to the image is W1, and the average gray level is U1. The total average gray scale of the image is: U = W0U0 + W1U1. T is traversed from the minimum gray value to the maximum gray value. When t maximizes the value g=w0*(U0-U)2+ W1 *(U1-u)2, T is the optimal threshold of segmentation. The otsu method can be understood as follows: This formula is actually the variance value between classes. The foreground and background divided by threshold T constitute the whole image, and the foreground value is u0 with probability w0, the background value is U1 with probability w1, and the total mean value is U. This formula can be obtained according to the definition of variance. As variance is a measure of the uniformity of gray distribution, the larger the variance value is, the greater the difference between the two parts of the image is. When part of the target is misclassified into background or part of the background is misclassified into target, the difference between the two parts becomes smaller. Therefore, the segmentation with the largest inter-class variance means the lowest misclassification probability. The direct application of Otsu method requires a large amount of calculation, so the equivalent formula G = W0W1 (U0-U1)2 is generally used.

6. Otsu method, which selects threshold value by grayscale stretching, is a more general method, but it will lose some overall information for two groups of objects when the grayscale is not obvious. Therefore, in order to solve this phenomenon, gray stretching enhanced Otsu method is adopted. In the idea of Otsu method, the gray level is increased to enhance the gray difference between the first two groups of objects. For the original gray level multiplied by the same coefficient, thereby expanding the image gray level series. The experimental results show that the segmentation effect varies greatly with different tensile coefficients.

Ii. Source code

function varargout = Gui_Main(varargin)
% GUI_MAIN MATLAB code for Gui_Main.fig
%      GUI_MAIN, by itself, creates a new GUI_MAIN or raises the existing
%      singleton*.
%
%      H = GUI_MAIN returns the handle to a new GUI_MAIN or the handle to
%      the existing singleton*.
%
%      GUI_MAIN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_MAIN.M with the given input arguments.
%
%      GUI_MAIN('Property'.'Value',...). creates anew GUI_MAIN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Gui_Main_OpeningFcn gets called.  An
%      unrecognized property name orinvalid value makes property application % stop. All inputs are passed to Gui_Main_OpeningFcn via varargin. % % *See GUI  Options on GUIDE's Tools menu.  Choose "GUI allows only one % instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Gui_Main

% Last Modified by GUIDE v2. 5 20-Jun- 2020. 15:07:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Gui_Main_OpeningFcn, ...
                   'gui_OutputFcn',  @Gui_Main_OutputFcn, ...
                   'gui_LayoutFcn', [],...'gui_Callback'[]);if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Gui_Main is made visible.
function Gui_Main_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Gui_Main (see VARARGIN)

% Choose default command line output for Gui_Main
clc;
axes(handles.axes1); cla reset; box on; set(gca, 'XTickLabel'[],'YTickLabel'[]); axes(handles.axes2); cla reset; box on;set(gca, 'XTickLabel'[],'YTickLabel'[]); axes(handles.axes3); cla reset; box on;set(gca, 'XTickLabel'[],'YTickLabel'[]); axes(handles.axes4); cla reset; box on;set(gca, 'XTickLabel'[],'YTickLabel'[]);set(handles.text1, 'string'."); handles.output = hObject; handles.file = []; handles.Plate = []; handles.bw = []; handles.words = []; % Update handles structure guidata(hObject, handles); % UIWAIT makes Gui_Main wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = Gui_Main_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % -------------------------------------------------------------------- function uipushtool1_ClickedCallback(hObject, eventdata, handles) % hObject handle to uipushtool1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB %  handles structure with handles and user data (see GUIDATA) [filename, pathname] = uiputfile({'*.jpg; *.tif; *.png; *.gif'.'All Image Files'; .'*. *'.'All Files' }, 'Save results'.'Result\result.jpg');
if isempty(filename)
    return;
end
file = fullfile(pathname, filename);
f = getframe(gcf);
f = frame2im(f);
imwrite(f, file);
msgbox('Saving the resulting image succeeded! '.'Prompt message'.'modal'); 


% --------------------------------------------------------------------
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)% Load license plate imageset(handles.text_result, 'string'."); axes(handles.axes1); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel'[]); axes(handles.axes2); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel'[]); axes(handles.axes3); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel'[]); axes(handles.axes4); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel'[]); set(handles.text1, 'string', ''); handles.file = []; handles.Plate = []; handles.bw = []; handles.words = []; [filename, pathname, filterindex] = uigetfile({'*.jpg; *.tif; *.png; *.gif', 'All Image Files'; . '*. *', 'All Files'},Select the image to be processed',... 'images\car.jpg'); if filename == 0 return; end file = fullfile(pathname, filename); Img = imread(file); [y, ~, ~] = size(Img); if y > 800 rate = 800/y; Img1 = imresize(Img, rate); else Img1 = Img; end axes(handles.axes1); imshow(Img1); title('The original image', 'FontWeight', 'Bold'); handles.Img = Img; handles.file = file; guidata(hObject, handles); % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB %  handles structure with handles and user data (see GUIDATA) if isempty(handles.file) msgbox('Please load the image to be detected', 'Prompt information', 'modal'); return; end [Plate, ~, Loc] = Pre_Process(handles.Img, [], 0); axes(handles.axes1); hold on; row = Loc.row; col = Loc.col; plot([col(1) col(2)], [row(1) row(1)], 'g-', 'LineWidth', 3);
plot([col(1) col(2)], [row(2) row(2)], 'g-', 'LineWidth', 3);
plot([col(1) col(1)], [row(1) row(2)], 'g-', 'LineWidth', 3);
plot([col(2) col(2)], [row(1) row(2)], 'g-', 'LineWidth', 3);
hold off;
axes(handles.axes2); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel'[]); imshow(Plate, []); title('License plate area image', 'FontWeight', 'Bold'); handles.Plate = Plate; guidata(hObject, handles); % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB %  handles structure with handles and user data (see GUIDATA) if isempty(handles.Plate) msgbox('Please first license plate area calibration operation!', 'Prompt information', 'modal'); return; end bw = Plate_Process(handles.Plate, 0); axes(handles.axes3); cla reset; box on; set(gca, 'XTickLabel', [], 'YTickLabel'[]); imshow(bw, []); title('Binary image of license plate area', 'FontWeight', 'Bold'); handles.bw = bw; guidata(hObject, handles); % --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) % hObject handle to pushbutton3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB %  handles structure with handles and user data (see GUIDATA) if isempty(handles.bw) msgbox('Please first license plate area binarization operation!', 'Prompt information', 'modal'); return; endCopy the code

3. Operation results

Fourth, note

Version: 2014 a