function varargout = main(varargin)
% MAIN MATLAB code for main.fig
% MAIN, by itself, creates a new MAIN or raises the existing
% singleton*.
%
% H = MAIN returns the handle to a new MAIN or the handle to
% the existing singleton*.
%
% MAIN('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MAIN.M with the given input arguments.
%
% MAIN('Property','Value',...) creates a new MAIN or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before main_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to main_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 main
% Last Modified by GUIDE v2.5 20-Apr-2014 14:55:44
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @main_OpeningFcn, ...
'gui_OutputFcn', @main_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 main is made visible.
function main_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 main (see VARARGIN)
% Choose default command line output for main
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes main wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = main_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 im;
[filename,pathname]=uigetfile({'*.*';'*.bmp';'*.jpg';'*.tif';'*.jpg'},'选择图像');
if isequal(filename,0)||isequal(pathname,0)
errordlg('您还没有选取图片!!','温馨提示');%如果没有输入,则创建错误对话框
return;
else
image=[pathname,filename];%合成路径+文件名
im=imread(image);%读取图像
axes(handles.axes1);
imshow(im);%在坐标axes1显示原图像
title('原始QR图像');
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)
% 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)%%%%%%%%%%%%%%%%%%% QR解码
global I_otsu; %global 定义全局变量
global im;
global KL;
global Ijibian;
I_jiema=Ijibian;
I_jiema=I_otsu; %二值
I_jiema=KL;
I_jiema=im;
%
str=zxing_decode(I_jiema) %解码
%set(handles.edit1,'string',[get(handles.edit1,'string') str]);
%set(handles.text,'string',[get(handles.text,'string') str]);
set(handles.edit1,'String',str); %显示字符
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles) %%%%%%%%%%%%%%%%%%% 灰度化处理
global im;
global II;
I=im;
[w,h,l]=size(I); %图像大小
II=[];
for i=1:h
for j=1:w
II(j,i)=0.3*I(j,i,1)+0.59*I(j,i,2)+0.11*I(j,i,3); %灰度化处理公式
end
end
figure,imshow(II,[]) %显示图像
title('QR二维码灰度化处理');
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)%%%%%%%%%%%%%%%%%%% 平滑处理
global II;
global I3;
III=uint8(II); %图像转换0-255
Ix=imnoise(III,'salt & pepper',0.02); %对灰度化图像人为加噪声
I3=medfilt2(Ix,[3,3]); %平滑处理
figure
imshow(Ix)
title('QR二维码加噪处理');
figure
imshow(I3)
title('QR二维码平滑处理');
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)%%%%%%%%%%%%%%%%%%% 二值化处理
global I3;
global I_otsu;
I_otsu=otsut(I3); %二值化处理
figure
imshow(I_otsu,[])
title('QR二维码二值化处理');
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles) %%%% 旋转校正 4.bmp
global im;
%倾斜校正 :二值化,取边缘,Hough变换得到角度,旋转
I=im;
bw=rgb2gray(I); %rgb转换为灰度图
bw=im2bw(I,graythresh(bw)); %二值化过程
bw=double(bw);
BW=edge(bw,'canny'); %canny边缘处理
BW1=BW;
figure
imshow(BW1);title('canny 边界图像'); %显示图像
[H,T,R]=hough(BW);
figure,imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
xlabel('\theta'),ylabel('\rho');
axis on, axis normal,hold on;
P=houghpeaks(H,4,'threshold',ceil(0.3*max(H(:)))); %hough变化峰值检测
x=T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');
lines=houghlines(BW,T,R,P,'FillGap',50,'MinLength',7); %hough检测线段
figure,imshow(BW),title('直线标识图像');
max_len = 0;
hold on;
for k=1:length(lines) %主要把线条和点显示出来
xy=[lines(k).point1;lines(k).point2];
% 标出线段
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% 标出线段的起始和终端点
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
len=norm(lines(k).point1-lines(k).point2);
Len(k)=len;
if (len>max_len)
max_len=len;
xy_long=xy;
end
end
% 强调最长的部分
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','blue');
[L1 Index1]=max(Len(:));
% 最长线段的起始和终止点
x1=[lines(Index1).point1(1) lines(Index1).point2(1)];
y1=[lines(Index1).point1(2) lines(Index1).point2(2)];
% 求得线段的斜率
K1=-(lines(Index1).point1(2)-lines(Index1).point2(2))/...
(lines(Index1).point1(1)-lines(Index1).point2(1))
an