一、簡介
1 最小誤差法原理
最小誤差門檻值分割法是根據圖像中背景和目标像素的機率分布密度來實作的,其思想是找到一個門檻值,并根據該門檻值進行劃分,計算出目标點誤分為背景的機率和背景點誤分為目标點的機率,得出總的誤差劃分機率。當總的誤差劃分機率最小時,便得到所需要的最佳門檻值。
2 最小誤差法實作步驟
根據圖可得最小誤差門檻值法的算法步驟:
步驟1:假設目标和背景灰階值得密度為,。計算混合機率密度。
步驟2:標明門檻值T,計算總的誤差機率,對進行求導。
步驟3:根據準則函數,計算使其最小時的T,作為最佳門檻值。
二、源代碼
function varargout = MainForm(varargin) % MAINFORM MATLAB code for MainForm.fig % MAINFORM, by itself, creates a new MAINFORM or raises the existing % singleton*. % % H = MAINFORM returns the handle to a new MAINFORM or the handle to % the existing singleton*. % % MAINFORM('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in MAINFORM.M with the given input arguments. % % MAINFORM('Property','Value',...) creates a new MAINFORM or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before MainForm_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to MainForm_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 MainForm % Last Modified by GUIDE v2.5 02-May-2021 08:10:18 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @MainForm_OpeningFcn, ... 'gui_OutputFcn', @MainForm_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 function InitAxes(handles) clc; axes(handles.axes1); cla reset; set(handles.axes1, 'XTick', [], 'YTick', [], ... 'XTickLabel', '', 'YTickLabel', '', 'Color', [0.7020 0.7804 1.0000], 'Box', 'On'); axes(handles.axes2); cla reset; set(handles.axes2, 'XTick', [], 'YTick', [], ... 'XTickLabel', '', 'YTickLabel', '', 'Color', [0.7020 0.7804 1.0000], 'Box', 'On'); function filePath = OpenFile(imgfilePath) % 打開檔案 % 輸出參數: % filePath——檔案路徑 if nargin < 1 imgfilePath = fullfile(pwd, 'images/test.jpg'); end [filename, pathname, ~] = uigetfile( ... { '*.jpg','All jpg Files';... '*.png','All png Files';... '*.*', '所有檔案 (*.*)'}, ... '選擇檔案', ... 'MultiSelect', 'off', ... imgfilePath); filePath = 0; if isequal(filename, 0) || isequal(pathname, 0) return; end filePath = fullfile(pathname, filename); % --- Executes just before MainForm is made visible. function MainForm_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 MainForm (see VARARGIN) % Choose default command line output for MainForm handles.output = hObject; InitAxes(handles); handles.I = 0; handles.J = 0; handles.bw_direct = 0; handles.bw_poly = 0; handles.bw__kittler = 0; handles.bw_temp = 0; % Update handles structure guidata(hObject, handles); % UIWAIT makes MainForm wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = MainForm_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; % --- 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) filePath = OpenFile(); if isequal(filePath, 0) return; end Img = imread(filePath); % 灰階化 if ndims(Img) == 3 I = rgb2gray(Img); else I = Img; end axes(handles.axes1); imshow(I, []); title('原圖像'); handles.I = I; 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 isequal(handles.I, 0) return; end % 直接二值化 bw_direct = im2bw(handles.I, graythresh(handles.I)); axes(handles.axes2); imshow(bw_direct, []); title('直接二值化分割'); handles.bw_direct = bw_direct; 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 isequal(handles.bw_direct, 0) return; end % 圈選胃區域空氣 bw_poly = roipoly(handles.bw_direct, c, r); axes(handles.axes2); imshow(handles.I, []); hold on; plot(c, r, 'r-', 'LineWidth', 2); hold off; title('胃區域空氣選擇'); handles.bw_poly = bw_poly; guidata(hObject, handles); % --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) if isequal(handles.bw_poly, 0) return; end % 圖像歸一化 IE = mat2gray(handles.I); % 對比度增強 IE = imadjust(IE, [0.532 0.72], [0 1]); IE = im2uint8(mat2gray(IE)); I = im2uint8(mat2gray(handles.I)); % 顯示 axes(handles.axes2); imshow(IE, []); title('圖像增強'); figure; subplot(2, 2, 1); imshow(I); title('原圖像'); subplot(2, 2, 2); imshow(IE); title('增強圖像'); subplot(2, 2, 3); imhist(I); title('原圖像直方圖'); subplot(2, 2, 4); imhist(IE); title('增強圖像直方圖'); JE = IE; JE(handles.bw_poly) = 255; handles.JE = JE; guidata(hObject, handles); % --- Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, handles) % hObject handle to pushbutton5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) if isequal(handles.JE, 0) return; end J = handles.JE; % 直方圖統計 [counts, gray_style] = imhist(J); % 亮度級别 gray_level = length(gray_style); % 計算各灰階機率 gray_probability = counts ./ sum(counts); % 統計像素均值 gray_mean = gray_style' * gray_probability; % 初始化 gray_vector(1) = realmax; ks = gray_level-1; for k = 1 : ks % 疊代計算 w = w + gray_probability(k+1); mean_k = mean_k + k * gray_probability(k+1); % 判斷是否收斂 if (w < eps) || (w > 1-eps) gray_vector(k+1) = realmax; else % 計算均值 mean_k1 = mean_k / w; mean_k2 = (gray_mean-mean_k) / (1-w); % 計算方差 var_k1 = (((0 : k)'-mean_k1).^2)' * gray_probability(1 : k+1); var_k1 = var_k1 / w; var_k2 = (((k+1 : ks)'-mean_k2).^2)' * gray_probability(k+2 : ks+1); var_k2 = var_k2 / (1-w); % 計算目标函數 if var_k1 > eps && var_k2 > eps else gray_vector(k+1) = realmax; end end end
三、運作結果
四、備注
版本:2014a