matlab基於形態學實現二值化條形碼識別

1、簡介
1 圖像的二值化的基本原理
圖像的二值化處理就是講圖像上的點的灰度置爲0或255,也就是講整個圖像呈現出明顯的黑白效果。即將256個亮度等級的灰度圖像經過適當的閥值選取而得到仍然能夠反映圖像總體和局部特徵的二值化圖像。在數字圖像處理中,二值圖像佔有很是重要的地位,特別是在實用的圖像處理中,以二值圖像處理實現而構成的系統是不少的,要進行二值圖像的處理與分析,首先要把灰度圖像二值化,獲得二值化圖像,這樣子有利於再對圖像作進一步處理時,圖像的集合性質只與像素值爲0或255的點的位置有關,再也不涉及像素的多級值,使處理變得簡單,並且數據的處理和壓縮量小。爲了獲得理想的二值圖像,通常採用封閉、連通的邊界定義不交疊的區域。全部灰度大於或等於閥值的像素被斷定爲屬於特定物體,其灰度值爲255表示,不然這些像素點被排除在物體區域之外,灰度值爲0,表示背景或者例外的物體區域。若是某特定物體在內部有均勻一致的灰度值,而且其處在一個具備其餘等級灰度值的均勻背景下,使用閥值法就能夠獲得比較的分割效果。若是物體同背景的差異表現不在灰度值上(好比紋理不一樣),能夠將這個差異特徵轉換爲灰度的差異,而後利用閥值選取技術來分割該圖像。動態調節閥值實現圖像的二值化可動態觀察其分割圖像的具體結果。算法

二值化是圖像分割的一種方法。在二值化圖象的時候把大於某個臨界灰度值的像素灰度設爲灰度極大值,把小於這個值的像素灰度設爲灰度極小值,從而實現二值化。
根據閾值選取的不一樣,二值化的算法分爲固定閾值和自適應閾值。 比較經常使用的二值化方法則有:雙峯法、P參數法、迭代法和OTSU法等。app

function varargout = untitled(varargin)
% UNTITLED M-file 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

% Last Modified by GUIDE v2.5 03-Nov-2011 13:59:22

% 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, 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 untitled (see VARARGIN)

% Choose default command line output for untitled
handles.output = hObject;
image1=imread('1.bmp');
axes(handles.axes1);
imshow(image1);

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- 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 mouse press over axes background.

% --- 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)

global im;
%提取條形碼區域構成的矩形的左上角座標
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
image=im;
l1=rgb2gray(image);
level=graythresh(l1);
l2=im2bw(l1,level);
l3=~l2;
l4=bwareaopen(l3,50);
l5=~l4;
l6=edge(l1,'canny');
l7=imclose(l6,strel('rectangle',[2,19]));
l8=imopen(l7,strel('rectangle',[2,19]));
l9=imopen(l8,strel('rectangle',[2,19]));
[L,num]=bwlabel(l9,8);
STATS=regionprops(L,'all');
a=length(STATS);
%figure,imshow(L);
%hold on;
b=0;
for i=1:a
    temp=STATS(i).BoundingBox;
    s=temp(3)*temp(4);
    if s>b
        b=s;
        k=i;%記錄面積最大的標記區域的索引值
    end
end
%rectangle('position',STATS(k).BoundingBox,'edgecolor','r');%繪製最大標記區域的外接矩形
temp=STATS(k).BoundingBox;
Rx=round(temp(1));Ry=round(temp(2));%提取條形碼區域左上角的座標
Rwidth=round(temp(3));Rlength=round(temp(4));


%初始化
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
check_left = [13,25,19,61,35,49,47,59,55,11;...	%左邊數據編碼,奇
              39,51,27,33,29,57, 5,17, 9,23];	%左邊數據編碼,偶
check_right = [114,102,108,66,92,78,80,68,72,116];	%右邊數據編碼
first_num = [31,20,18,17,12,6,3,10,9,5];    %第一位數據編碼
bar = im;  %讀輸入條形碼圖片
bar_Gray = rgb2gray(bar);   %將RGB圖片轉換灰度圖

[a_hist x] = imhist(bar_Gray);   %繪製灰度直方圖,返回直方圖數據向量a_hist,和相應的色彩值向量x


    check_code = 10 - check_code;
end

if check_code==str2num(num_bar(13)) %判斷校驗碼是否正確
    code = num_bar
else
  %  fprintf(1,'Please Turn It Around!\n');
  str='Please Turn It Around!';
    set(handles.text4,'string',str);
    return
end
%c=num2str(code);
set(handles.text1,'string',code);
set(handles.text4,'string','');
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.

% --- 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)
set(handles.text1,'string','');
[filename,pathname]=...
    uigetfile({'*.jpg';'*.tif';'*.bmp';'*.gif';'*.*'},'選擇圖片');
if pathname == 0
    return;
end
str=[pathname filename];
global im;
im=imread(str);
axes(handles.axes1);
imshow(im);


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes1

相關文章
相關標籤/搜索