当前位置:文档之家› 基于MATLAB的QR二维码解码技术的程序

基于MATLAB的QR二维码解码技术的程序

function varargout = QRMain(varargin)% QRMAIN MATLAB code for QRMain.fig% QRMAIN, by itself, creates a new QRMAIN or raises the existing% singleton*.%% H = QRMAIN returns the handle to a new QRMAIN or the handle to% the existing singleton*.%% QRMAIN('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in QRMAIN.M with the given input arguments. %% QRMAIN('Property','Value',...) creates a new QRMAIN or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before QRMain_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to QRMain_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 QRMain% Last Modified by GUIDE v2.5 11-Apr-2017 20:19:20% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @QRMain_OpeningFcn, ...'gui_OutputFcn', @QRMain_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before QRMain is made visible.function QRMain_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 QRMain (see V ARARGIN)% Choose default command line output for QRMainhandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes QRMain wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = QRMain_OutputFcn(hObject, eventdata, handles)% varargout cell array for returning output args (see V ARARGOUT);% 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 structurevarargout{1} = handles.output;% --- Executes on button press in OpenQRPic.function OpenQRPic_Callback(hObject, eventdata,handles) %%%%%%%%打开图像global im;[filename,pathname]=uigetfile({'*.*';'*.bmp';'*.jpg';'*.tif';'*.jpg'},'选择图像');if isequal(filename,0)||isequal(pathname,0)errordlg('您还没有选取图片!!','温馨提示');%如果没有输入,则创建错误对话框return;elseimage=[pathname,filename];%合成路径+文件名im=imread(image);%读取图像figureimshow(im);%在坐标axes1显示原图像title('原始QR图像');end% hObject handle to OpenQRPic (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 DecodeQR.function DecodeQR_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 GrayGen.function GrayGen_Callback(hObject, eventdata,handles) %%%%%%%%%%%%%%%%%%% 灰度化处理global im;global II;I=im;[w,h,l]=size(I); %图像大小II=[];for i=1:hfor j=1:wII(j,i)=0.3*I(j,i,1)+0.59*I(j,i,2)+0.11*I(j,i,3); %灰度化处理公式endendfigure,imshow(II,[]) %显示图像title('QR二维码灰度化处理');% --- Executes on button press in SmoothGen.function SmoothGen_Callback(hObject, eventdata, handles)%%%%%%%%%%%%%%%%%%% 平滑处理global II;global I3;III=uint8(II); %图像转换0-255Ix=imnoise(III,'salt & pepper',0.02); %对灰度化图像人为加噪声I3=medfilt2(Ix,[3,3]); %平滑处理figureimshow(Ix)title('QR二维码加噪处理');figureimshow(I3)title('QR二维码平滑处理');% --- Executes on button press in Binaryzation.function Binaryzation_Callback(hObject, eventdata, handles)%%%%%%%%%%%%%%%%%%% 二值化处理global I3;global I_otsu;I_otsu=otsut(I3); %二值化处理figureimshow(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 ImageRotate.function ImageRotate_Callback(hObject, eventdata, handles) %%%% 旋转校正 4.bmpglobal im;%倾斜校正:二值化,取边缘,Hough变换得到角度,旋转I=im;bw=rgb2gray(I); %rgb转换为灰度图bw=im2bw(I,graythresh(bw)); %二值化过程bw=double(bw);BW=edge(bw,'canny'); %canny边缘处理BW1=BW;figureimshow(BW1);title('边缘处理图像'); %显示图像[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;endend% 强调最长的部分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))angle=atan(K1)*180/pi %显示角度A = imrotate(I,-angle,'bilinear');% imrate 是逆时针的所以取一个负号figure,imshow(A);%imwrite(A,'4qingxie_jiaozheng')% --- Executes on button press in AberrationAdj.function AberrationAdj_Callback(hObject, eventdata, handles) %%%% 畸变校正11.bmpglobal im;global Ijibian;syms a1a2a3a4b1b2b3b4real;I=im;x=[1:256];y=[1:256];control_outpoint=[14 64 %原图像顶点26 221206 38246 196];control_inputpoint=[1 1 %几何畸变图像顶点1 255255 1255 255];x1=control_inputpoint(:,1); %原图像第一列y1=control_inputpoint(:,2); %原图像第二列A=[control_outpoint(1,1) control_outpoint(1,2)control_outpoint(1,1)*control_outpoint(1,2) 1control_outpoint(2,1) control_outpoint(2,2)control_outpoint(2,1)*control_outpoint(2,2) 1control_outpoint(3,1) control_outpoint(3,2)control_outpoint(3,1)*control_outpoint(3,2) 1control_outpoint(4,1) control_outpoint(4,2)control_outpoint(4,1)*control_outpoint(4,2) 1];a=linsolve(A,x1); %等价于a=sym(A)/sym(x1) b=linsolve(A,y1); %等价于a=sym(A)/sym(y1)f1=@(x,y) a(1)*x+a(2)*y+a(3)*x*y+a(4); %定义变换函数f2=@(x,y) b(1)*x+b(2)*y+b(3)*x*y+b(4);for i=1:256for j=1:256x1(i,j)=f1(i,j);y1(i,j)=f2(i,j);I1(i,j)=interp2(x,y,I,x1(i,j),y1(i,j),'bilinear'); %双线性插值%I1(i,j)=interp2(x1(i,j),y1(i,j),I,x,y,'bilinear');endendIjibian=I1;figureimshow(I1);function edit3_Callback(hObject, eventdata, handles)% hObject handle to edit3 (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 edit3 as text% str2double(get(hObject,'String')) returns contents of edit3 as a double% --- Executes during object creation, after setting all properties.function edit3_CreateFcn(hObject, eventdata, handles)% hObject handle to edit3 (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 QRGen.function QRGen_Callback(hObject, eventdata, handles) %%%% 自制QR (用英文)global KL;str3=get(handles.edit3,'string') %得到string数据KL=qrgen(str3,300,300); %生成相应QR二维码figureimshow(KL,[])imwrite(KL,'KL.png')% --- Executes on button press in QRgenDecode.function QRgenDecode_Callback(hObject, eventdata,handles) %%%%%%%%自制QR解码显示global KL;I_jiema=imread('KL.bmp');str2=zxing_decode(I_jiema) %解码过程set(handles.edit1,'String',str2);% --- Executes on key press with focus on QRgenDecode and none of its controls. function QRgenDecode_KeyPressFcn(hObject, eventdata, handles)% hObject handle to QRgenDecode (see GCBO)% eventdata structure with the following fields (see UICONTROL)% Key: name of the key that was pressed, in lower case% Character: character interpretation of the key(s) that was pressed% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in pushbutton11.function pushbutton11_Callback(hObject, eventdata, handles)% hObject handle to pushbutton11 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) AllFigureHandle=get(0,'Children'); % Contain Figure and GUIflag=rem(AllFigureHandle,1);%OnlyPictureHandle=AllFigureHandle(find(flag==0)); % Exclude GUI,because GUI'handle is decimalclose(OnlyPictureHandle);% --- If Enable == 'on', executes on mouse press in 5 pixel border.% --- Otherwise, executes on mouse press in 5 pixel border or over DecodeQR. function DecodeQR_ButtonDownFcn(hObject, eventdata, handles)% hObject handle to DecodeQR (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)。

相关主题