📄 thresgui.m
字号:
set(handles.ActualAutomatico,'visible','on')
set(handles.text2,'visible','on')
set(handles.Intensidad,'string',int2str(Min))
set(handles.Archivo,'visible','on')
set(handles.Archivo,'string',NameArchivo)
GraficarUmbral(Min,handles)
%==========================================================================
function GraficarUmbral(Umbral,handles)
%==========================================================================
global ImagenGris ImagenUmbral
imgUmbral=im2bw(ImagenGris,Umbral/255);
subplot(224);
imshow(imgUmbral)
title(['Image segmented in threshold = ',int2str(Umbral)])
ImagenUmbral=imgUmbral;
%==========================================================================
function GraficarHistograma(Umbral,handles)
%==========================================================================
global ImagenGris
persistent H
set(handles.Intensidad,'String',Umbral)
subplot(223);imhist(ImagenGris);title('Histogram')
Escala=axis;
if ishandle(H)==1,delete(H),end%if
H=line([Umbral Umbral],[Escala(3:4)],'color','r');
%==========================================================================
function GraficarHistoUmbral(Umbral,handles,Tipo)
%==========================================================================
GraficarHistograma(Umbral,handles)
GraficarUmbral(Umbral,handles)
set(handles.slider1,'value',Umbral)
switch Tipo
case 'all' %GRAYTHRESH ITERATIVO KAPUR
set(handles.ActualAutomatico,'value',0)
set(handles.Umbralizar,'enable','on')
set(handles.Umbralizar,'string','Thresholding')
set(handles.Umbralizar,'enable','off')
case 'trian' %TRIANGULAR
set(handles.ActualAutomatico,'value',0)
set(handles.Umbralizar,'enable','on')
set(handles.Umbralizar,'string','next Threshold >>')
end %switch
%==========================================================================
function NoChecked(handles)
%==========================================================================
set(handles.graythresh,'checked','off')
set(handles.kapur,'checked','off')
set(handles.triangular,'checked','off')
set(handles.iterativo,'checked','off')
set(handles.DefinidoUsuario,'checked','off')
%==========================================================================
function umbral = Kapur1(Imagen)
%==========================================================================
%AUTOR OF THIS SUBROUTINE: BIOMEDICAL ENGINEERING RESEARCH GROUP - UIS
if ~Esgray(Imagen) %Validar si una Imagen se encuentra en Escala de Grises o en color
uiwait(msgbox('La Imagen no se encuentra en escala de grises','Error','modal'))
umbral = 0;
else
[fil col] = size(Imagen);
Pixeles = fil * col;
h1 = imhist(Imagen);
Pi = h1/Pixeles; %Calcular las probabilidades de cada escala de gris
Pt = zeros(256,1);
Pt(1) = Pi(1);
for i = 2:256 %Calcular la Funci髇 de Probabilidad Acumulada
Pt(i)=Pt(i-1)+Pi(i);
end
Hb = zeros(1,256); %Calcular Hb(u) y Hw(u) donde la suma de los dos es m醲ima suponiendo un umbral
Hw = zeros(1,256);
for i = 1:256
if Pt(i) > 0
for j = 1 : i
if Pi(j) > 0
Hb(i) = Hb(i) + ((Pi(j) / Pt(i)) * log(Pi(j) / Pt(i)));
end
end
end
end
for i = 1:256
if (1-Pt(i)) > 0
for j = i + 1 : 256
if Pi(j) > 0
Hw(i) = Hw(i) + ((Pi(j) / (1-Pt(i))) * log(Pi(j) / (1-Pt(i))));
end
end
end
end
Hb = -Hb;
Hw = -Hw;
H = Hb + Hw;
[a, b] = max(H(:));
umbral = b-1;
umbral = umbral/255;
end
%==========================================================================
function Umbral = Triangular1(Imagen);
%==========================================================================
%AUTOR OF THIS SUBROUTINE: BIOMEDICAL ENGINEERING RESEARCH GROUP - UIS
if ~Esgray(Imagen) %Validar si una Imagen se encuentra en Escala de Grises o en color
uitwait(msgbox('La imagen no se encuentra en escala de grises','Error','modal'))
Umbral = 0;
else
z = 0;
H = imhist(Imagen);
H = Promedio(H);
dh = Derivada(H);
[maxim minim]= MaxiMini(dh);
for i = 1 : length(maxim)-1;
clear D, k = 0;
x1 = maxim(i); y1 = H(maxim(i));
x2 = maxim(i + 1); y2 = H(maxim(i + 1));
M = (y2 - y1) / (x2 - x1);
if (x2 - 1) - (x1 + 1) > 0
for j = x1 : x2;
Px = j;
Py = H(j);
k = k + 1;
D(k,1) = sqrt(((Px - x1) * M - Py + y1)^2 / (M^2 + 1));
D(k,2) = j;
end
[Pu p]= max(D(:,1));
if Pu > 0
z = z + 1;
Umbral(z) = D(p,2);
end
end
end
Umbral = Repetidos(Umbral,10)/255;
end
function x = Promedio(h);
n = 5;
g = zeros(256 + 2 * round(n / 2 - 1), 1);
g(1 + round(n / 2 - 1) : length(g) - round(n / 2 - 1)) = h;
for i = 1 + round(n / 2 - 1) : length(g)-round(n / 2 - 1);
sum = 0;
for j = i - round(n / 2 - 1) : i + round(n / 2 - 1)
sum = sum + g(j);
end
x(i - round(n / 2 - 1), 1) = round(sum / n);
end
function dx = Derivada(h);
dx=zeros(256,1);
for i=3:254;
dx(i)= (h(i-2) - 8*(h(i-1)) + 8*(h(i+1)) - h(i+2))/12;
end
function [Maxi, Mini] = MaxiMini(d);
j = 1; y = 1;
for i = 2 : 256;
if (d(i - 1) < 0 && d(i) >= 0)
Mini(j) = i;
j = j + 1;
elseif (d(i - 1) >= 0 && d(i) < 0)
Maxi(y) = i;
y = y + 1;
end
end
function Vector = Repetidos(Arreglo, D);
N_Ind = length(Arreglo);
for i = 1 : N_Ind - 1;
if Arreglo(i) > 0
for j = i + 1 : N_Ind;
if Arreglo(i) == Arreglo(j) || (abs(Arreglo(i)-Arreglo(j))<D)
Arreglo_Nuevo(j) = 0;
else
Arreglo_Nuevo(j) = Arreglo(j);
end
end
end
end
[i j Vector] = find(Arreglo_Nuevo);
%==========================================================================
function Umbral = Iterativo1(Imagen)
%==========================================================================
%AUTOR OF THIS SUBROUTINE: BIOMEDICAL ENGINEERING RESEARCH GROUP - UIS
if ~Esgray(Imagen) %Validar si una Imagen se encuentra en Escala de Grises o en color
uitwait(msgbox('La imagen no se encuentra en escala de grises','Error','modal'))
Umbral = 0;
else
Histograma = imhist(Imagen);
Grises = find(Histograma); %Localizar los niveles diferentes a cero
Maximo = max(Grises); %Nivel m醲imo de gris
Minimo = min(Grises); %Nivel m韓imo de gris
Umbral = Minimo + (Maximo - Minimo) / 2; %Umbral Inicial
Umbralp = 0; %Umbral Anterior
while (abs(Umbral - Umbralp) > 1)
Mayores = find(Imagen > Umbral);
Menores = find(Imagen <= Umbral);
m1 = mean(Imagen(Mayores));
m2 = mean(Imagen(Menores));
Umbralp = Umbral;
Umbral = round((m1+m2)/2); %Calcular el umbral 髉timo
end
Umbral = Umbral/255;
end
%==========================================================================
function y = Esgray(x)
%==========================================================================
y = ndims(x)==2 && ~isempty(x);
if islogical(x)
y = false;
elseif ~isa(x, 'uint8') && ~isa(x, 'uint16') && y
% At first just test a small chunk to get a possible quick negative
[m,n] = size(x);
chunk = x(1:min(m,10),1:min(n,10));
y = min(chunk(:))>=0 && max(chunk(:))<=1;
% If the chunk is an intensity image, test the whole image
if y
y = min(x(:))>=0 && max(x(:))<=1;
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -