function varargout = txfg(varargin)
% TXFG M-file for txfg.fig
% TXFG, by itself, creates a new TXFG or raises the existing
% singleton*.
% H = TXFG returns the handle to a new TXFG or the handle to
% the existing singleton*.
% TXFG('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TXFG.M with the given input arguments.
% TXFG('Property','Value',...) creates a new TXFG or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before txfg_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to txfg_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 txfg
% Last Modified by GUIDE v2.5 17-May-2013 22:41:16
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @txfg_OpeningFcn, ...
'gui_OutputFcn', @txfg_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 txfg is made visible.
function txfg_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 txfg (see VARARGIN)
% Choose default command line output for txfg
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes txfg wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = txfg_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 wenjian_Callback(hObject, eventdata, handles)
% hObject handle to wenjian (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function suanfaxz_Callback(hObject, eventdata, handles)
% hObject handle to suanfaxz (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function fangchafa_Callback(hObject, eventdata, handles)
% hObject handle to fangchafa (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global rgbimage;
x=rgbimage;
%figure;
%imshow(x);
[m,n]=size(x);
N=m*n;
num=zeros(1,256);
p=zeros(1,256);
for i=1:m
for j=1:n
num(x(i,j)+1)=num(x(i,j)+1)+1;
end
end
for i=0:255;
p(i+1)=num(i+1)/N;
end
totalmean=0;
for i=0:255;
totalmean=totalmean+i*p(i+1);
end
maxvar=0;
for k=0:255
kk=k+1;
zerosth=sum(p(1:kk));
firsth=0;
for h=0:k
firsth=firsth+h*p(h+1);
end
var=totalmean*zerosth-firsth;
var=var*var;
var=var/(zerosth*(1-zerosth)+0.01);
var=sqrt(var);
if(var>maxvar)
maxvar=var;
point=k;
end
end
threshold=point;
for i=1:m
for j=1:n
if x(i,j)>point
bin(i,j)=1;
else
bin(i,j)=0;
end
end
end
axes(handles.axes7);
imshow(bin);
% --------------------------------------------------------------------
function zishiying_Callback(hObject, eventdata, handles)
% hObject handle to zishiying (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global rgbimage;
SE = strel('diamond',4);
BW1 = rgbimage;
BW2 = imerode(BW1,SE);
BW3 = imdilate(BW2,SE);
BW4 = BW1-BW3;
%rgb转灰度
if isrgb(BW4)==1
I_gray=rgb2gray(BW4);
else
I_gray=BW4;
end
figure('NumberTitle', 'off', 'Name', '自适应处理结果')
subplot(2,2,1);
imshow(I_gray);
title('灰度图像');
I_double=double(I_gray);%转化为双精度
[wid,len]=size(I_gray);
colorlevel=256; %灰度级
hist=zeros(colorlevel,1);%直方图
%threshold=128; %初始阈值
%计算直方图
for i=1:wid
for j=1:len
m=I_gray(i,j)+1;
hist(m)=hist(m)+1;
end
end
hist=hist/(wid*len);%直方图归一化
miuT=0;
for m=1:colorlevel
miuT=miuT+(m-1)*hist(m);
end
xigmaB2=0;
for mindex=1:colorlevel
threshold=mindex-1;
omega1=0;
omega2=0;
for m=1:threshold-1
omega1=omega1+hist(m);
end
omega2=1-omega1;
miu1=0;
miu2=0;
for m=1:colorlevel
if m<threshold
miu1=miu1+(m-1)*hist(m);
else
miu2=miu2+(m-1)*hist(m);
end
end
miu1=miu1/omega1;
miu2=miu2/omega2;
xigmaB21=omega1*(miu1-miuT)^2+omega2*(miu2-miuT)^2;
xigma(mindex)=xigmaB21;
if xigmaB21>xigmaB2
finalT=threshold;
xigmaB2=xigmaB21;
end
end
fT=finalT/255%阈值归一化
T=graythresh(I_gray)%matlab函数求阈值
for i=1:wid
for J=1:len
if I_double(i,j)>finalT
bn(i,j)=1;
else
bin(i,j)=0;
end
end
end
subplot(2,2,2);
imshow(bin);
title('bin');
subplot(2,2,3.2);
plot(1:colorlevel,xigma);
% --------------------------------------------------------------------
function yibanyuzhi_Callback(hObject, eventdata, handles)
% hObject handle to yibanyuzhi (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global rgbimage;
f=rgb2gray(rgbimage);
f=im2double(f);
T=0.5*(min(f(:))+max(f(:)));% 计算初始阈值
done=false;
while ~done
g=f>=T;%阈值比较
Tn=0.5*(mean(f(g))+mean(f(~g)));% Tnext=0.5*(mean(d(g))+mean(d(~g)))计算当前阈值下,分割后图像均值,并作为下一次的阈值
done=abs(T-Tn)<0.1;%判断是否可以终止分割
T=Tn;
end
display('迭代阈值(T)');
T
r=im2bw(f,T);
figure('NumberTitle', 'off', 'Name', '一般阈值法处理结果')
subplot(2,2,1);
imshow(f);
title('原始图像');
subplot(2,2,2),imshow(r);
title('全图阈值迭代法');
Th=graythresh(f);
display('阈值(T)-Otsu法');
Th
s=im2bw(f,Th);
subplot(2,2,3),title('全图阈值-Otsu法');
se=strel('disk',10);
ft=imtophat(f,se);
Thr=graythresh(ft);
display('局部阈值')
Thr
lt=im2bw(ft,Thr);
subplot(2,2,4),imshow(lt);
title('局部阈值');
% --------------------------------------------------------------------
function zuidashangfa_Callback(hObject, eventdata, handles)
% hObject handle to zuidashangfa (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global rgbimage;
a=rgbimage;
count=imhist(a);
[m,n]=size(a);
N=m*n;
L=256;
count=count/N;%%每一个像素的分布概率
count
for i=1:L
if count(i)~=0
st=i-1;
break;
end
end
st
for i=L:-1:1
if count(i)~=0
nd=i-1;
break;
end
end
nd
f=count(st+1:nd+1); %f是每个灰度出现的概率
size(f)
E=[];
for Th=st:nd-1 %%%设定初始分割阈值为Th
av1=0;
av2=0;
Pth=sum(count(1:Th+1));
%第一类的平均相对熵为
for i=0:Th
av1=av1-count(i+1)/Pth*log(count(i+1)/Pth+0.00001);
end
%第二类的平均相对熵为
for i=Th+1:L-1
av2=av2-count(i+1)/(1-Pth)*log(count(i+1)/(1-Pth)+0.00001);
end
E(Th-st+1)=av1+av2;
end
position=find(E==(max(E)));
th=st+position-1
for i=1:m
for j=1:n
if a(i,j)>th
MATLAB.zip_灰度图值
版权申诉
102 浏览量
2022-09-24
19:33:11
上传
评论
收藏 116KB ZIP 举报
寒泊
- 粉丝: 75
- 资源: 1万+
评论0