一、简介
大津法(OTSU)是一种确定图像二值化分割阈值的算法,由日本学者大津于1979年提出。从大津法的原理上来讲,该方法又称作最大类间方差法,因为按照大津法求得的阈值进行图像二值化分割后,前景与背景图像的类间方差最大。
它被认为是图像分割中阈值选取的最佳算法,计算简单,不受图像亮度和对比度的影响,因此在数字图像处理上得到了广泛的应用。它是按图像的灰度特性,将图像分成背景和前景两部分。因方差是灰度分布均匀性的一种度量,背景和前景之间的类间方差越大,说明构成图像的两部分的差别越大,当部分前景错分为背景或部分背景错分为前景都会导致两部分差别变小。因此,使类间方差最大的分割意味着错分概率最小。
应用:是求图像全局阈值的最佳方法,应用不言而喻,适用于大部分需要求图像全局阈值的场合。
优点:计算简单快速,不受图像亮度和对比度的影响。
缺点:对图像噪声敏感;只能针对单一目标分割;当目标和背景大小比例悬殊、类间方差函数可能呈现双峰或者多峰,这个时候效果不好。
二、源代码
%本程序可以完成布匹疵点检测且本程序是批处理程序。
function varargout = FabricGui(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @FabricGui_OpeningFcn, ...
'gui_OutputFcn', @FabricGui_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
% --- 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 hedit_detect_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function hedit_detect_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in ptnRun.
function ptnRun_Callback(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.hedit_zhouqi,'string')); %获得织物纹理周期
T1=str2num(get(handles.hedit_yuzhifenge,'string')); %分割阈值
T2=str2num(get(handles.hedit_yuzhihou,'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 hedit_zhouqi_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function hedit_zhouqi_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function hedit_yuzhifenge_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function hedit_yuzhifenge_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function hedit_yuzhihou_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function hedit_yuzhihou_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function hedit_you_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function hedit_you_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function hedit_wu_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function hedit_wu_CreateFcn(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
三、运行结果
四、备注
完整代码或者代写添加QQ 1564658423
转载:https://blog.csdn.net/TIQCmatlab/article/details/116236873
查看评论