大津法(OTSU)是一种确定图像二值化分割阈值的算法,由日本学者大津于1979年提出。从大津法的原理上来讲,该方法又称作最大类间方差法,因为按照大津法求得的阈值进行图像二值化分割后,前景与背景图像的类间方差最大。\ 它被认为是图像分割中阈值选取的最佳算法,计算简单,不受图像亮度和对比度的影响,因此在数字图像处理上得到了广泛的应用。它是按图像的灰度特性,将图像分成背景和前景两部分。因方差是灰度分布均匀性的一种度量,背景和前景之间的类间方差越大,说明构成图像的两部分的差别越大,当部分前景错分为背景或部分背景错分为前景都会导致两部分差别变小。因此,使类间方差最大的分割意味着错分概率最小。\ 应用:是求图像全局阈值的最佳方法,应用不言而喻,适用于大部分需要求图像全局阈值的场合。\ 优点:计算简单快速,不受图像亮度和对比度的影响。\ 缺点:对图像噪声敏感;只能针对单一目标分割;当目标和背景大小比例悬殊、类间方差函数可能呈现双峰或者多峰,这个时候效果不好。
``` %本程序可以完成布匹疵点检测且本程序是批处理程序。 function varargout = FabricGui(varargin) guiSingleton = 1; guiState = struct('guiName', mfilename, ... 'guiSingleton', guiSingleton, ... 'guiOpeningFcn', @FabricGuiOpeningFcn, ... 'guiOutputFcn', @FabricGuiOutputFcn, ... 'guiLayoutFcn', [] , ... 'guiCallback', []); if nargin && ischar(varargin{1}) guiState.gui_Callback = str2func(varargin{1}); end
if nargout [varargout{1:nargout}] = guimainfcn(guiState, varargin{:}); else guimainfcn(guiState, varargin{:}); end
% --- Executes just before FabricGui is made visible. function FabricGui_OpeningFcn(hObject, eventdata, handles, varargin) handles.output = hObject; % Update handles structure guidata(hObject, handles);
% --- Outputs from this function are returned to the command line. function varargout = FabricGui_OutputFcn(hObject, eventdata, handles) varargout{1} = handles.output;
function heditdetectCallback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties. function heditdetectCreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
% --- Executes on button press in ptnRun. function ptnRunCallback(hObject, eventdata, handles) %批处理 srcDir=uigetdir('Choose source directory.'); %获得选择的文件夹 cd(srcDir); allnames=struct2cell(dir('*.bmp')); %只处理8位的bmp文件 [k,len]=size(allnames); %获得bmp文件的个数 %得到设置的参数 P=str2num(get(handles.heditzhouqi,'string')); %获得织物纹理周期 T1=str2num(get(handles.hedityuzhifenge,'string')); %分割阈值 T2=str2num(get(handles.hedityuzhihou,'string')); %后处理阈值 numwu=0;numyou=0; for ii=1:len %逐次取出文件 cd(srcDir); name=allnames{1,ii}; I=imread(name); %读取文件 axes(handles.hyuanshiaxes); %显示图像 imshow(I); cd('..'); I0=I; %预处理 I=double(I0); %数据类型的转换 [M,N]=size(I);%得到待检测图像的大小 J=junzhicaiyang(I,M,N,P); %调用均值下采样函数 J=uint8(J); %双线性插值,恢复原来的图像大小 I1=imresize(J,P,'bilinear'); %双线性插值,恢复原来图像的大小。 %进行方差下采样,用于增强图像疵点信息 I1=double(I1); J1=fangchacaiyang(I1,M,N,P); %调用方差下采样函数 J1=uint8(J1); %双线性插值,恢复原来图像的大小。 I=imresize(J1,P,'bilinear'); %进行二值化及其后处理 T=Otsu(I); % --- Executes on button press in ptnExit. function ptnExit_Callback(hObject, eventdata, handles) % hObject handle to ptnExit (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) delete(handles.MainFig);
function heditzhouqiCallback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties. function heditzhouqiCreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
function hedityuzhifengeCallback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties. function hedityuzhifengeCreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
function hedityuzhihouCallback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties. function hedityuzhihouCreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
function hedityouCallback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties. function hedityouCreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
function heditwuCallback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties. function heditwuCreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function J=junzhicaiyang(I,M,N,a) k=1;h=1;temp=0; %均值下采样,用于削弱图像周期纹理 for i=1:a:M-a+1 for j=1:a:N-a+1 %每一个小块的均值作为新的图像像素值 temp=0; for m=i:1:i+a-1 for n=j:1:j+a-1 temp=temp+I(m,n); end end J(h,k)=temp/(a*a); %计算均值,作为新的图像像素值 k=k+1; end ```
版本:2014a