function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_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 untitled
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_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 untitled is made visible.
function untitled_OpeningFcn(hObject, ~, 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 untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
ha=axes('units','normalized','position',[0 0 1 1]);
uistack(ha,'down');
II=imread('GUI.jpg');
image(II);
colormap gray;
set(ha,'handlevisibility','off','visible','off');
I=imread('GUI.jpg');
set(handles.pushbutton1,'cdata',I);
I=imread('GUI.jpg');
set(handles.pushbutton2,'cdata',I);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_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)
% 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)
figure;
image_background=imread('背景1.png');
subplot(2,2,1);imshow(image_background);title('背景图')
image_gray1=rgb2gray(image_background);
image_original=imread('1-1.png');
subplot(2,2,2);imshow(image_original);title('原图');
image_gray2=rgb2gray(image_original);
% M=double(I);
% J=M;
% for i=1:H-1
% for j=1:W-1
% J(i,j)=abs(M(i,j)-M(i+1,j+1))+abs(M(i+1,j)-M(i,j+1));
% figure;
% for i=1:m-1
% for j=1:n-1
% robertsNum = abs(f(i,j)-f(i+1,j+1)) + abs(f(i+1,j)-f(i,j+1));
% if(robertsNum > threshold)
% f2(i,j)=255;
% else
% f2(i,j)=0;
% figure;
Dk=imsubtract(image_gray2,image_gray1);
%将矩阵image_gray2,image_gray1中对应的元素值做差,差值返回到矩阵Dk中
%image_gray2,image_gray1是具有相同大小和相同种类的非稀疏实数矩阵
%imsubtract可与imabsdiff互换,但imabsdiff可避免差值差生负值
image_bw=im2bw(Dk,0.2);%Dk是输入图像矩阵资料,0.2是二值化程度值,介于0到1之间,image_bw是二值化图像矩阵
subplot(2,2,3);imshow(image_bw);title('差分法后二值化图像');
Image_BW_medfilt=medfilt2(image_bw,[3 3]);
Image_BW=edge(Image_BW_medfilt,'roberts');
%采用灰度或一个二值化图像Image_BW_medfilt作为它的输入,并返回一个与I相同大小的二值化图像Image_BW
%在函数检测到边缘的地方为1,其他地方为0。
subplot(2,2,4);imshow(Image_BW);title('Roberts算子处理后图像');
se=strel('disk',30);%根据disk圆盘指定的类型创建一个结构元素,半径30
image=imdilate(Image_BW,se);%对输入图像Image_BW用结构元素se进行膨胀
figure;imshow(image);title('膨胀后图像');
[image_bwlabel,num]=bwlabel(image,8);
%对目标区域进行标记
%返回一个和image大小相同的image_bwlabel矩阵,这里num返回的就是image中连通区域的个数。n的值为4或8,表示是按4连通寻找区域,还是8连通寻找,默认为8
%四连通或八连通是图像处理里的基本感念:8连通,是说一个像素,如果和其他像素在上、下、左、右、左上角、左下角、右上角或右下角连接着,则认为他们是联通的;4连通是指,如果像素的位置在其他像素相邻的上、下、左或右,则认为他们是连接着的,连通的,在左上角、左下角、右上角或右下角连接,则不认为他们连通
D=regionprops(image_bwlabel,'BoundingBox');
%求最小外接矩阵
%测量标注矩阵image_bwlabel中每一个标注区域的一系列属性。image_bwlabel中不同的正整数元素对应不同的区域
%'BoundingBox' 包含相应区域的最小矩形
figure;imshow(image_original);title('道路障碍物识别图像');
for i=1:num
hold on %在当前图的轴(坐标系)中画了一幅图,再画另一幅图时,原来的图还在,与新图共存,都看得到
rectangle('position',D(i).BoundingBox,'EdgeColor','r','LineWidth',5);
%用红框标出最小外接矩阵
%D(i).BoundingBox的作用是指定长方形的位置以及长、宽。
%D应该是自定义的一个表示某种状态的结构体数组变量,i表示数组的第i个元素,可能就是第i种状态
%BoundingBox表示这种状态的边界参数,包含位置,长、宽
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% 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)
figure;
image_background=imread('背景2.jpg');
subplot(2,2,1);imshow(image_background);title('背景图')
image_gray1=rgb2gray(image_background);
image_original=imread('2-1.jpg');
subplot(2,2,2);imshow(image_original);title('原图');
image_gray2=rgb2gray(image_original);
Dk=imsubtract(image_gray2,image_gray1);
image_bw=im2bw(Dk,0.2);
subplot(2,2,3);imshow(image_bw);title('差分法后二值化图像');
Image_BW_medfilt=medfilt2(image_bw,[3 3]);
Image_BW=edge(Image_BW_medfilt,'roberts');
subplot(2,2,4);imshow(Image_BW);title('Roberts算子处理后图像');
se=strel('disk',50);
image=imdilate(Image_BW,se);
figure;imshow(image);title('膨胀后图像');
% image_original1=imread('2-1.jpg');
% image_gray3=rgb2gray(image_original1);
%
% Dk1=imsubtract(image_gray3,image_gray1);
% image_bw1=im2bw(Dk1,0.2);
%
% Image_BW_medfilt1=medfilt2(image_bw1,[3 3]);
% Image_BW1=edge(Image_BW_medfilt1,'roberts');
%
% se=strel('disk',30);
% image1=imdilate(Image_BW1,se);
[image_bwlabel,num]=bwlabel(image1,8);
D=regionprops(image_bwlabel,'BoundingBox');
figure;imshow(image_original);title('道路障碍物识别图像');
for i=1:num
hold on
rectangle('position',D(i).BoundingBox,'EdgeColor','g','LineWidth',5);
end