clear;
[chang,kuan,gao]=size(I);
for i=1:chang
for j=1:kuan
IR(i,j)=I(i,j,1);
IG(i,j)=I(i,j,2);
IB(i,j)=I(i,j,3);
end
end
IR=double(IR);
IG=double(IG);
IB=double(IB);
for i=1:chang
for j=1:kuan
if 1.6*IB(i,j)-IG(i,j)-IR(i,j)<0
CA(i,j)=0;
elseif 1.6*IB(i,j)-IG(i,j)-IR(i,j)>255
CA(i,j)=255;
else
CA(i,j)=1.6*IB(i,j)-IG(i,j)-IR(i,j);
end
end
end
%IGRAY=rgb2gray(I);
%{
for i=1:chang
for j=1:kuan
if CA(i,j)<200&IGRAY(i,j)<200
CA(i,j)=0;
end
end
end
%}
yuzhi=otsu(CA);
for i=1:chang
for j=1:kuan
if CA(i,j)<=yuzhi;
CA(i,j)=0;
else
CA(i,j)=255;
end
end
end
CA=uint8(CA);
imshow(CA);
for i=1:chang
for j=1:kuan
if CA(i,j)==0
I(i,j,1)=0;
I(i,j,2)=0;
I(i,j,3)=0;
end
end
end
figure;
I=uint8(I);
imshow(I);
2 二值分割程序
function [k1]=otsu(f)
%是otsu的分割算法,在论文基于三分量色差法的交通分割阀值分割那一块,所以要对照那块内容来看这个算法
[a,b]=size(f);%获取图片f的长和宽
N=a*b;%计算图像的总像素,长a和宽b的乘积
for i=1:256
n(i)=sum(sum(f==(i-1)));%统计像素值为i的像素个数,保存到n里面,n(i)就是像素值为i的像素个数
p(i)=n(i)/N;%统计每个像素值在该图像中的比例,也就是概率
end
for k=1:256
w0(k)=0;
w1(k)=0;
u0(k)=0;
u1(k)=0;
u(k)=0;
dx(k)=0;
end%上面是初始化,全部是0
for k=1:256
for i=1:k
w0(k)=w0(k)+p(i);%w0里面保存的是像素值为k的像素概率之和,也就是CO产生的概率
end
for i=1:k
u0(k)=u0(k)+((i*p(i))/w0(k));%求CO加权平均值,看公式10
end
for i=(k+1):256
w1(k)=w1(k)+p(i);%C1产生的概率
end
for i=(k+1):256
u1(k)=u1(k)+((i*p(i))/w1(k));%求C1的加权平均值
end
u(k)=w0(k)*u0(k)+w1(k)*u1(k);
dx(k)=w0(k)*w1(k)*(u0(k)-u1(k)) ^2;%公式13 看论文
end
[c,k]=max(dx);%求出最大值
c1=c;
k1=k;%我们只要k就可以了,就是我们的阀值
k=(k-1)/255;
y=k;
end
3 系统的主程序
function varargout = jiaotong(varargin)
% JIAOTONG MATLAB code for jiaotong.fig
% JIAOTONG, by itself, creates a new JIAOTONG or raises the existing
% singleton*.
%
% H = JIAOTONG returns the handle to a new JIAOTONG or the handle to
% the existing singleton*.
%
% JIAOTONG('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in JIAOTONG.M with the given input arguments.
%
% JIAOTONG('Property','Value',...) creates a new JIAOTONG or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before jiaotong_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to jiaotong_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 jiaotong
% Last Modified by GUIDE v2.5 20-May-2011 15:59:05
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @jiaotong_OpeningFcn, ...
'gui_OutputFcn', @jiaotong_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 jiaotong is made visible.
function jiaotong_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 jiaotong (see VARARGIN)
% Choose default command line output for jiaotong
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes jiaotong wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = jiaotong_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)
global tu
global tu1%全局变量
axes(handles.axes1)%在窗口1显示图片
[filename,pathname]=uigetfile({ ...;
'*.*','All Files(*.*)';},...
'选择文件');
%
if isequal([filename,pathname],[0,0])
return
else
%读取图片
pic = fullfile(pathname,filename);
b = imread(pic);
imshow(b);%显示图片
tu=b;
tu1=b;
title; (’原图’)
%handle.axes1=b;
end
% 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)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
global tu
I=tu;
flag1=get(handles.radiobutton1,'value');
flag2=get(handles.radiobutton2,'value');
flag3=get(handles.radiobutton3,'value'); %获取单选按钮的值,看是选取哪种类型的交通标志
if flag1==1%如果选了红色警示牌的话
[chase,erzhi,zuizhong]=biaozhifengeR(I);%调用红色分量计算函数
axes(handles.axes2)
imshow(chase);
title(’红色分量效果图’);
axes(handles.axes3)
imshow(erzhi)
title(’二值分割效果图’);
axes(handles.axes4)
imshow(zuizhong)
title(’最终分割效果图’) ;
end
if flag2==1 %如果选了蓝色指示牌的话
[chase,erzhi,zuizhong]=biaozhifengeB(I);%调用蓝色分量计算函数
axes(handles.axes2)
imshow(chase);
title(’蓝色分量效果图’) ;
axes(handles.axes3)
imshow(erzhi)
title (’二值分割效果图’);
axes(handles.axes4)
imshow(zuizhong)
title (’最终分割效果图’);
end
if flag3==1%如果选了绿色指示牌的话
[chase,erzhi,zuizhong]=biaozhifengeG(I); %调用蓝色分量计算函数
axes(handles.axes2)
imshow(chase);
title (’蓝色分量效果图’);
axes(handles.axes3)
imshow(erzhi)
title (’二值分割效果图’);
axes(handles.axes4)
imshow(zuizhong)
title(’最终分割效果图’) ;
end
% 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)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
global tu
I=tu;
flag1=get(handles.radiobutton1,'value');
flag2=get(handles.radiobutton2,'value');
flag3=get(handles.radiobutton3,'value');
if flag1==1%和上面是一样的,另外这里的函数又和biaozhifengeR的函数一样,具体解释看该函数文件,这里不再解释
axes(handles.axes2)
[chang,kuan,gao]=size(I);
for i=1:chang
for j=1:kuan
IR(i,j)=I(i,j,1);
end
end
imshow(IR);
title(’红色分量效果图’) ;
end
if flag3==1
axes(handles.axes2)
[chang,kuan,gao]=size(I);
for i=1:chang
for j=1:kuan
IG(i,j)=I(i,j,2);
end
end
imshow(IG);
title (’绿色分量效果图’);
end
if flag2==1
axes(handles.axes2)
[chang,kuan,gao]=size(I);
for i=1:chang
for j=1:kuan
IB(i,j)=I(i,j,3);
end
end
imshow(IB);
title(’蓝色分量效果图’) ;
end
% 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)
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
global tu
I=tu;
flag1=get(handles.radiobutton1,'value');
flag2=get(handles.radiobutton2,'value');
flag3=get(handles.radiobutton3,'value');
if flag1==1
axes(handles.axes3)
[chang,kuan,gao]=size(I);
for i=1:chang
for j=1:kuan
IR(i,j)=I(i,j,1);
IG(i,j)=I(i,j,2);
IB(i,j)=I(i,j,3);
end
end
IR=double(IR);
IG=double(IG);
IB=double(IB);
for i=1:chang
for j=1:kuan
if 1.6*IR(i,j)-IG(i,j)-IB(i,j)<0
CA(i,j)=0;
elseif 1.6*IR(i,j)-IG(i,j)-IB(i,j)>255
CA(i,j)=255;
else
CA(i,j)=1.6*IR(i,j)-IG(i,j)-IB(i,j);
end
end
end
yuzhi=otsu(CA);
for i=1:chang
for j=1:kuan
if CA(i,j)<=yuzhi;
CA(i,j)=0;
else
CA(i,j)=255;
end
end
end
CA=uint8(CA);
imshow(CA);
title (’二值分割效果图’);
end
if flag3==1
axes(handles.axes3)
[chang,kuan,gao]=size(I);
for i=1:chang
for j=1:kuan
IR(i,j)=I(i,j,1);
IG(i,j)=I(i,j,2);
IB(i,j)=I(i,j,3);
end
end
IR=double(IR);
IG=double(IG);
IB=double(IB);
for i=1:chang
for j=1:kuan
if 1.6*IG(i,j)-IR(i,j)-IB(i,j)<0
CA(i,j)=0;
elseif 1.6*IG(i,j)-IR(i,j)-IB(i,j)>255
CA(i,j)=255;
else
CA(i,j)=1.6*IG(i,j)-IR(i,j)-IB(i,j);
end
end
end
yuzhi=otsu(CA);
for i=1:chang
for j=1:kuan
if CA(i,j)<=yuzhi;
CA(i,j)=0;
else
CA(i,j)=255;
end
end
end
CA=uint8(CA);
imshow(CA);
title(’二值分割效果图’) ;
end
if flag2==1
axes(handles.axes3)
[chang,kuan,gao]=size(I);
for i=1:chang
for j=1:kuan
IR(i,j)=I(i,j,1);
IG(i,j)=I(i,j,2);
IB(i,j)=I(i,j,3);
end
end
IR=double(IR);
IG=double(IG);
IB=double(IB);
for i=1:chang
for j=1:kuan
if 1.6*IB(i,j)-IR(i,j)-IG(i,j)<0
CA(i,j)=0;
elseif 1.6*IB(i,j)-IR(i,j)-IG(i,j)>255
CA(i,j)=255;
else
CA(i,j)=1.6*IB(i,j)-IR(i,j)-IG(i,j);
end
end
end
yuzhi=otsu(CA);
for i=1:chang
for j=1:kuan
if CA(i,j)<=yuzhi;
CA(i,j)=0;
else
CA(i,j)=255;
end
end
end
CA=uint8(CA);
imshow(CA);
title(’二值分割效果图’) ;
end
% 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)
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
global tu
I=tu;
flag1=get(handles.radiobutton1,'value');
flag2=get(handles.radiobutton2,'value');
flag3=get(handles.radiobutton3,'value');
if flag1==1
[chase,erzhi,zuizhong]=biaozhifengeR(I);
axes(handles.axes4)
imshow(zuizhong)
title(’最终分割效果图’) ;
end
if flag2==1
[chase,erzhi,zuizhong]=biaozhifengeB(I);
axes(handles.axes4)
imshow(zuizhong)
title(’最终分割效果图’) ;
end
if flag3==1
[chase,erzhi,zuizhong]=biaozhifengeG(I);
axes(handles.axes4)
imshow(zuizhong)
title(’最终分割效果图’) ;
end
% 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)
4 警示牌标志的分割程序
function [chase,erzhi,zuizhong]=biaozhifengeR(I)
%是按照论文基于三色分差的交通标志分割所得
[chang,kuan,gao]=size(I);%获取图片I的长和宽
for i=1:chang
for j=1:kuan
IR(i,j)=I(i,j,1);%提取图片的红色分量
IG(i,j)=I(i,j,2); %提取图片的绿色分量
IB(i,j)=I(i,j,3); %提取图片的蓝色分量
end
end
IR=double(IR);
IG=double(IG);
IB=double(IB); %将提取的分量转换为双精度型,方便计算
for i=1:chang
for j=1:kuan
if 1.6*IR(i,j)-IG(i,j)-IB(i,j)<0%论文中式5,目的是为了是提取的红色分量颜色更亮,其他的颜色更暗,下面两个一样,其他两个函数文件是相应的功能
CA(i,j)=0;
elseif 1.6*IR(i,j)-IG(i,j)-IB(i,j)>255
CA(i,j)=255;
else
CA(i,j)=1.6*IR(i,j)-IG(i,j)-IB(i,j);
end
end
end
chase=uint8(IR); %将红色分量图保存在chase变量中
yuzhi=otsu(CA); %通过调用otsu来得到红色分量图的阀值yuzhi
for i=1:chang
for j=1:kuan
if CA(i,j)<=yuzhi;
CA(i,j)=0;
else
CA(i,j)=255; %低于阀值的都变为0也就是黑色,高于阀值的都变味白色,这样就成了二值图了
end
end
end
CA=uint8(CA); %将处理后的红色分量图转换为8位图像类型的图
%imshow(CA);
erzhi=CA;
for i=1:chang
for j=1:kuan
if CA(i,j)==0
I(i,j,1)=0;
I(i,j,2)=0;
I(i,j,3)=0; %对照处理过的红色分量二值图,如果是黑色的话,就使原彩色图变为黑色,如果是白色的地方,就让原始色彩图保留元色彩色
end
end
end
I=uint8(I);
zuizhong=I; %将处理后的彩色图赋值给zuizhong
%imshow(I);
5 蓝色指示牌标志的分割程序
function [chase,erzhi,zuizhong]=biaozhifengeB(I)
[chang,kuan,gao]=size(I);
for i=1:chang
for j=1:kuan
IR(i,j)=I(i,j,1);
IG(i,j)=I(i,j,2);
IB(i,j)=I(i,j,3);
end
end
IR=double(IR);
IG=double(IG);
IB=double(IB);
for i=1:chang
for j=1:kuan
if 1.6*IB(i,j)-IG(i,j)-IR(i,j)<0
CA(i,j)=0;
elseif 1.6*IB(i,j)-IG(i,j)-IR(i,j)>255
CA(i,j)=255;
else
CA(i,j)=1.6*IB(i,j)-IG(i,j)-IR(i,j);
end
end
end
chase=uint8(IB);
yuzhi=otsu(CA);
for i=1:chang
for j=1:kuan
if CA(i,j)<=yuzhi;
CA(i,j)=0;
else
CA(i,j)=255;
end
end
end
CA=uint8(CA);
%imshow(CA);
erzhi=CA;
for i=1:chang
for j=1:kuan
if CA(i,j)==0
I(i,j,1)=0;
I(i,j,2)=0;
I(i,j,3)=0;
end
end
end
I=uint8(I);
zuizhong=I;
%imshow(I);
6 绿色指示牌标志的分割程序
function [chase,erzhi,zuizhong]=biaozhifengeG(I)
[chang,kuan,gao]=size(I);
for i=1:chang
for j=1:kuan
IR(i,j)=I(i,j,1);
IG(i,j)=I(i,j,2);
IB(i,j)=I(i,j,3);
end
end
IR=double(IR);
IG=double(IG);
IB=double(IB);
for i=1:chang
for j=1:kuan
if 1.6*IG(i,j)-IR(i,j)-IB(i,j)<0
CA(i,j)=0;
elseif 1.6*IG(i,j)-IR(i,j)-IB(i,j)>255
CA(i,j)=255;
else
CA(i,j)=1.6*IG(i,j)-IR(i,j)-IB(i,j);
end
end
end
chase=uint8(IG);
yuzhi=otsu(CA);
for i=1:chang
for j=1:kuan
if CA(i,j)<=yuzhi;
CA(i,j)=0;
else
CA(i,j)=255;
end
end
end
CA=uint8(CA);
%imshow(CA);
erzhi=CA;
for i=1:chang
for j=1:kuan
if CA(i,j)==0
I(i,j,1)=0;
I(i,j,2)=0;
I(i,j,3)=0;
end
end
end
I=uint8(I);
zuizhong=I;
%imshow(I);