2016-12-05 114 views
0

我在MATLAB下面的代碼來解碼條形碼:Matlab的條碼閱讀器

barcode = imread('barcode.jpg'); 
imshow(barcode); 
pause; 

barcode = rgb2gray(barcode); 
barcode = histeq(barcode); 
barcode = imresize(barcode, .35, 'bilinear'); 

imshow(barcode); 
pause; 

%level = graythresh(barcode); 
%bw = im2bw(barcode, level); 
h = edge(barcode, 'sobel', [], 'horizontal'); 

% dilate the horizontal edges vertically to get rid of noise 
se = strel('line',20,1); 
h = imdilate(h, se); 
imshow(h); 
pause; 

v = edge(barcode, 'sobel', [], 'vertical'); 
se = strel('line',1,20); 
v = imdilate(v, se); 
imshow(v); 
pause; 

regions = ~h & v; 
imshow(regions); 
pause; 

% dilate image to get barcode region 
se = strel('line',9,1); 

% get the barcode region 
regions = imdilate(regions,se); 
imshow(regions); 
pause; 

%regions = imcomplement(regions); 

[labels,numLabels]=bwlabel(regions); 
imshow(label2rgb(labels, @spring, 'c', 'shuffle')); 
pause; 


% find largest region 
index = 0; 
largest_size = 0; 
for i = 0:max(unique(labels)) 
    if sum(sum(regions & (labels == i))) > largest_size 
     largest_size = sum(sum(regions & (labels == i))); 
     index = i; 
    end 
end 

% show largest region 
imshow(regions & (labels == index)); 
pause; 

region = regions & (labels == index); 
region = imcomplement(region); 

% extract region from original barcode image 
[rows, cols] = size(region); 
for i = 1:rows 
    for j = 1:cols 
     if region(i,j) == 1 
      barcode(i,j) = 255; 
     end 
    end 
end 

imshow(barcode); 
pause; 

% we now have extracted the barcode 

% sharpen it a little bit 
yar = fspecial('unsharp'); 
barcode = imfilter(barcode,yar,'replicate'); 
imshow(barcode); 
pause; 

% get the highest/lowest x & y values 
x1 = cols; 
x2 = 1; 
y1 = rows; 
y2 = 1; 
for i = 1:rows 
    for j = 1:cols 
     if region(i,j) == 1 
      if i < y1 
       y1 = i; 
      end 
      if i > y2 
       y2 = i; 
      end 
      if j < x1 
       x1 = j; 
      end 
      if j > x2 
       x2 = j; 
      end 
     end 
    end 
end 

[r,c] = size(barcode); 

for i = 1:r 
    for j = 1:c 
     if barcode(i,j) >= 194 
      barcode(i,j) = 255; 
     end 
     if barcode(i,j) < 194 
      barcode(i,j) = 0; 
     end 
    end 
end 

%imshow(barcode); 

% COPIED/PASTED FROM http://www.mathworks.com/matlabcentral/fileexchange/21899-recognize-barcode/content/runprogram.m 
I=barcode; 
I=im2bw(I);    
imshow(I); 
hold on; 
[x,y]=ginput(2);  %get data from click mouse 
u1=x(1); 
u2=y(1); 
v1=x(2); 
v2=y(2); 
line(x,y);    %paint the line between 2 point at click mouse 
a = (u1:1:v1);   %find out function line between 2 point 
if (u1~=v1) && (u2~=v2) 
    g = round(((v2-u2)/(v1-u1))*a + u2 -(u1/(v1-u1))); 
elseif (u1==v1) 
    a = u1; 
end      
i=1;     %find the value each pixel that the line go through 
h=v1-u1+1; 
b=(1:1:h); 
for a=u1:v1 
    if (I(g,a)==0) 
     b(i)=1; 
     i=i+1; 
    elseif (I(g,a)==1) 
     b(i)=0; 
     i=i+1; 
    end 
end 
c=b; 
i=1;    %find number pixels of each line in barcode picture; 
s=(1:1:60); 
for k=1:60 
    j=0; 
    curr=c(i); 
    while (c(i)==curr)&&(i<=h) 
      j=j+1; 
      i=i+1; 
    end 
    s(k)=j; 
end 
mau=s(2);  %the first line is the sample for barcode,in another line is ratio with this 
q=s./mau; 
p=round(q);  

doc1=(1:1:6); %decode 
k=1; 
for i=5:4:25 
    if (p(i)==3)&& (p(i+1)==2) &&(p(i+2)==1) &&(p(i+3)==1) 
     doc1(k)=0; 
    elseif (p(i)==2)&& (p(i+1)==2) &&(p(i+2)==2) &&(p(i+3)==1) 
     doc1(k)=1; 
    elseif (p(i)==2)&& (p(i+1)==1) &&(p(i+2)==2) &&(p(i+3)==2) 
     doc1(k)=2; 
    elseif (p(i)==1)&& (p(i+1)==4) &&(p(i+2)==1) &&(p(i+3)==1) 
     doc1(k)=3; 
    elseif (p(i)==1)&& (p(i+1)==1) &&(p(i+2)==3) &&(p(i+3)==2) 
     doc1(k)=4; 
    elseif (p(i)==1)&& (p(i+1)==2)&&(p(i+2)==3) &&(p(i+3)==1) 
     doc1(k)=5; 
    elseif (p(i)==1)&& (p(i+1)==1) &&(p(i+2)==1) &&(p(i+3)==4) 
     doc1(k)=6; 
    elseif (p(i)==1)&& (p(i+1)==3) &&(p(i+2)==1) &&(p(i+3)==2) 
     doc1(k)=7; 
    elseif (p(i)==1)&& (p(i+1)==2) &&(p(i+2)==1) &&(p(i+3)==3) 
     doc1(k)=8; 
    elseif (p(i)==3)&& (p(i+1)==1) &&(p(i+2)==1) &&(p(i+3)==2) 
     doc1(k)=9; 
    end 
    k=k+1; 
end 
doc2=(1:1:6); 
k=1; 
for i=34:4:54 
    if (p(i)==3)&& (p(i+1)==2) &&(p(i+2)==1) &&(p(i+3)==1) 
     doc2(k)=0; 
    elseif (p(i)==2)&& (p(i+1)==2) &&(p(i+2)==2) &&(p(i+3)==1) 
     doc2(k)=1; 
    elseif (p(i)==2)&& (p(i+1)==1) &&(p(i+2)==2) &&(p(i+3)==2) 
     doc2(k)=2; 
    elseif (p(i)==1)&& (p(i+1)==4) &&(p(i+2)==1) &&(p(i+3)==1) 
     doc2(k)=3; 
    elseif (p(i)==1)&& (p(i+1)==1) &&(p(i+2)==3) &&(p(i+3)==2) 
     doc2(k)=4; 
    elseif (p(i)==1)&& (p(i+1)==2) &&(p(i+2)==3) &&(p(i+3)==1) 
     doc2(k)=5; 
    elseif (p(i)==1)&& (p(i+1)==1) &&(p(i+2)==1) &&(p(i+3)==4) 
     doc2(k)=6; 
    elseif (p(i)==1)&& (p(i+1)==3) &&(p(i+2)==1) &&(p(i+3)==2) 
     doc2(k)=7; 
    elseif (p(i)==1)&& (p(i+1)==2) &&(p(i+2)==1) &&(p(i+3)==3) 
     doc2(k)=8; 
    elseif (p(i)==3)&& (p(i+1)==1) &&(p(i+2)==1) &&(p(i+3)==2) 
     doc2(k)=9; 
    end 
    k=k+1; 
end 

的問題是,它似乎不正確讀取!

我在ginput部件上也遇到了一些麻煩,用戶必須在條形碼圖像上創建一條線以便讀取線上的像素。

你們能幫我找到的實際問題?

謝謝!

+0

1.包括你的形象在你的問題2.所有的條形碼不具有相同的加密3.精確,你的代碼的一部分,不工作? – obchardon

+0

我認爲它不起作用從我在哪裏用ginput命令在圖像上畫線... –

回答

0

首先,確保條形碼,你正試圖決定的類型是什麼你提供它,因爲有許多不同類型的條形碼。 =

我曾嘗試你提供的代碼只是一個條形碼的圖像,它似乎有找到條形碼區域,以及您從MathWorks的複製不可靠解碼條碼的代碼問題;

我建議考慮看看下面的鏈接;它提供了一個simulink模型,但是,所有的代碼都可以被看到或移動到一個m文件中,或者你可以調整你的代碼到它裏面......它被設置用於EAN13(UPC)。

至於ginput不工作,我personnally不會留給用戶選擇掃描線。通過在圖像上引入一個簡單的旋轉,您就可以在多個角度獲得多條線,並且可以有效地掃描任何方向(如在雜貨店的掃描儀)。然而,據說,你需要對輸入u1 u2 v1 v2進行四捨五入,因爲它們可以是小數,你在代碼中稍後將它們用作索引。

https://www.mathworks.com/help/vision/examples/barcode-recognition.html

+0

首先感謝您的幫助!現在,還有一件事。我如何將代碼移動到m文件? –

+0

您可以打開每個塊,然後打開塊編輯器... Ctrl + C,Ctrl + V –