function varargout = untitledx(varargin)
% UNTITLEDX MATLAB code for untitledx.fig
% UNTITLEDX, by itself, creates a new UNTITLEDX or raises the existing
% singleton*.
%
% H = UNTITLEDX returns the handle to a new UNTITLEDX or the handle to
% the existing singleton*.
%
% UNTITLEDX('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLEDX.M with the given input arguments.
%
% UNTITLEDX('Property','Value',...) creates a new UNTITLEDX or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitledx_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitledx_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 untitledx
% Last Modified by GUIDE v2.5 26-May-2015 15:49:16
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitledx_OpeningFcn, ...
'gui_OutputFcn', @untitledx_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 untitledx is made visible.
function untitledx_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 untitledx (see VARARGIN)
% Choose default command line output for untitledx
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitledx wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitledx_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)
for i=1:20
strtemp=strcat('C:\Users\lii\Desktop\4\',int2str(i),'.','jpg');
img=imread(strtemp);
axes(handles.axes1);
imshow(img);
img1=rgb2gray(img);
[m,n]=size(img1);
x0=round(0.05*m);
y0=round(0.07*n);
img2=img1(x0:x0+99,y0:y0+149);
img5=img(x0:x0+99,y0:y0+149,:);
img3=edge(img2, 'canny');
E(:,:,i)=img3;
a=(i-1)/i;
if(i==1)
s(:,:,1)=(1-a)*E(:,:,1);
else
s(:,:,i)=a*s(:,:,i-1)+(1-a)*E(:,:,i);
end
end
se = strel('disk',6);%用于膨胀腐蚀及开闭运算等操作的结构元素对象
b=imdilate(s(:,:,20),se);
img4=img5;
for i=1:100
for j=1:150
if b(i,j)==1
img4(i,j,:)=img5(i,j,:);
else
img4(i,j,:)=255;
end
end
end
axes(handles.axes2);
imshow(img4);
imwrite(img4,'C:\Users\lii\Desktop\待识别.jpg','jpg');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%形状检索
rgb_img=imread('C:\Users\lii\Desktop\待识别.jpg');
gray_img=rgb2gray(rgb_img);
% subplot(2,1,1);imshow(gray_img);
[row,col]=size(gray_img);
gray_img=double(gray_img);
% 使用canny算子,提取边缘
edge_canny=edge(gray_img,'canny');
edge_num=sum(sum(edge_canny));%边缘总点数
% subplot(2,1,2);imshow(edge_canny);
%计算梯度矢量Gx,Gy
for cir1=2:row-1
for cir2=2:col-1
Gx(cir1,cir2)=sum(gray_img(cir1-1:cir1+1,cir2+1))...
-sum(gray_img(cir1-1:cir1+1,cir2-1))...
+gray_img(cir1,cir2+1)-gray_img(cir1,cir2-1);
Gy(cir1,cir2)=sum(gray_img(cir1+1,cir2-1:cir2+1))...
-sum(gray_img(cir1-1,cir2-1:cir2+1))...
+gray_img(cir1+1,cir2)-gray_img(cir1-1,cir2);
Gx(cir1,cir2)=Gx(cir1,cir2)+(Gx(cir1,cir2)==0)*1e-6; % 为避免分母为0,加上一个很小的值。
theta(cir1,cir2)=atan2(Gy(cir1,cir2),Gx(cir1,cir2))*180/pi;
%atan2:计算边缘方向;范围:[-pi,pi];弧度->角度: [-180,180]
end
end
% 将[-180,180]每10度分为一组,那么方向被量化为36 bin
TH=[-170,-160,-150,-140,-130,-120,-110,-100,-90, -80,-70,-60,-50,-40,-30,-20,-10, 0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180;...
-180,-170,-160,-150,-140,-130,-120,-110,-100,-90,-80,-70,-60,-50,-40,-30,-20,-10,0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170];
% 存储各方向像素数目的数组
bar_hist= zeros(1,36);
%edge_theta=theta&edge_canny;
for cir1=1:row
for cir2=1:col
for k=1:36
if (edge_canny(cir1,cir2)==1 & theta(cir1,cir2)<TH(1,k) & theta(cir1,cir2)>=TH(2,k))
bar_hist(k)=bar_hist(k)+1;
end
end
end
end
disp(bar_hist);
x=1:36;
T0=bar_hist;
axes(handles.axes3);
bar(x,bar_hist(x));
% --- 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)
tic;
rgb_img=imread('C:\Users\lii\Desktop\待识别.jpg');
gray_img=rgb2gray(rgb_img);
[row,col]=size(gray_img);
gray_img=double(gray_img);
% 使用canny算子,提取边缘
edge_canny=edge(gray_img,'canny');
edge_num=sum(sum(edge_canny));%边缘总点数
%计算梯度矢量Gx,Gy
for cir1=2:row-1
for cir2=2:col-1
Gx(cir1,cir2)=sum(gray_img(cir1-1:cir1+1,cir2+1))...
-sum(gray_img(cir1-1:cir1+1,cir2-1))...
+gray_img(cir1,cir2+1)-gray_img(cir1,cir2-1);
Gy(cir1,cir2)=sum(gray_img(cir1+1,cir2-1:cir2+1))...
-sum(gray_img(cir1-1,cir2-1:cir2+1))...
+gray_img(cir1+1,cir2)-gray_img(cir1-1,cir2);
Gx(cir1,cir2)=Gx(cir1,cir2)+(Gx(cir1,cir2)==0)*1e-6; % 为避免分母为0,加上一个很小的值。
theta(cir1,cir2)=atan2(Gy(cir1,cir2),Gx(cir1,cir2))*180/pi;
%atan2:计算边缘方向;范围:[-pi,pi];弧度->角度: [-180,180]
end
end
% 将[-180,180]每10度分为一组,那么方向被量化为36 bin
TH=[-170,-160,-150,-140,-130,-120,-110,-100,-90, -80,-70,-60,-50,-40,-30,-20,-10, 0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180;...
-180,-170,-160,-150,-140,-130,-120,-110,-100,-90,-80,-70,-60,-50,-40,-30,-20,-10,0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170];
% 存储各方向像素数目的数组
bar_hist= zeros(1,36);
%edge_theta=theta&edge_canny;
for cir1=1:row
for cir2=1:col
for k=1:36
if (edge_canny(cir1,cir2)==1 & theta(cir1,cir2)<TH(1,k) & theta(cir1,cir2)>=TH(2,k))
bar_hist(k)=bar_hist(k)+1;
end
end
end
end
disp(bar_hist);
x=1:36;
T0=bar_hist;
% figure;
% bar(x,bar_hist(x));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%循环输入图像
for i=1:11
strtemp=strcat('C:\Users\lii\Desktop\01\',int2str(i),'.','jpg');
rgb_img=imread(strtemp);
gray_img=rgb2gray(rgb_img);
[m n]=size(gray_img);
gray_img=double(gray_img);
%%%%%%%%%%%%%%%%%%%%%
%计算每一副图片的边缘直方图
% 使用canny�
评论0