用Matlab代码实现条码的定位和读取

用Matlab代码实现条码的定位和读取,matlab,barcode-scanner,Matlab,Barcode Scanner,我有以下MATLAB代码来本地化和读取图片中的条形码(JPG、PNG): ##MATLAB代码## 此代码给出了以下结果: [https://i.stack.imgur.com/e5ckj.jpg][1] %%现在的问题是如何跟踪条形码并消除其他区域。 %%我需要的答案是:130760000102(“条形码”) %%我不知道如何完成代码。我已更新此代码,以提供图片中条形码的值: S = imread('picture.jpg'); % Image NRows = size(S,1);

我有以下MATLAB代码来本地化和读取图片中的条形码(JPG、PNG): ##MATLAB代码##

此代码给出了以下结果: [https://i.stack.imgur.com/e5ckj.jpg][1]

%%现在的问题是如何跟踪条形码并消除其他区域。 %%我需要的答案是:130760000102(“条形码”)
%%我不知道如何完成代码。

我已更新此代码,以提供图片中条形码的值:

S = imread('picture.jpg');      % Image
NRows = size(S,1);
NColoms = size(S,2);

S = imcrop(S,[0 NRows/2 NColoms NRows/1.2]);

depth = 10;
horizantalRule=depth+1;
I = rgb2gray(S);   % Gray Image
I = imadjust(I);  
I = im2bw(I);      % Binary Image
imshow(I);  
NumofRows=size(I,1);
NumofColoms=size(I,2);  

%% Localisation Sobel-Technique
% Detection des contours
I_Sobel = rgb2gray(S);
[~,threshold] = edge(I_Sobel,'sobel');
fudgeFactor = 0.5;
% Binarisation
BWs = edge(I_Sobel,'sobel',threshold * fudgeFactor);
se90 = strel('line',3,90);
se0 = strel('line',3,0);
% Elimination des pixels des contours
BWsdil = imdilate(BWs,[se90 se0]);
BWdfill = imfill(BWsdil,'holes');
% Selection du pixels d'intensite maximal dans l'image
BWnobord = imclearborder(BWdfill,4);
% lissage d'image et selection de la region du code barres
remove_small_region = bwareaopen(BWnobord,50);
% Outline mask
BWoutline = bwperim(BWnobord);
Segout = S; 
Segout(BWoutline) = 255; 
%imshow(Segout)
imshow(labeloverlay(S,BWnobord));
此代码执行以下操作: 从RGB到二进制(BW)的特征图片,并给出仅包含条形码的图片的结果。 问题是: 如何改进此代码以处理任何图片(找到条形码并给出结果)

S = imread('picture.jpg');      % Image
NRows = size(S,1);
NColoms = size(S,2);

S = imcrop(S,[0 NRows/2 NColoms NRows/1.2]);

depth = 10;
horizantalRule=depth+1;
I = rgb2gray(S);   % Gray Image
I = imadjust(I);  
I = im2bw(I);      % Binary Image
imshow(I);  
NumofRows=size(I,1);
NumofColoms=size(I,2);  

%% Localisation Sobel-Technique
% Detection des contours
I_Sobel = rgb2gray(S);
[~,threshold] = edge(I_Sobel,'sobel');
fudgeFactor = 0.5;
% Binarisation
BWs = edge(I_Sobel,'sobel',threshold * fudgeFactor);
se90 = strel('line',3,90);
se0 = strel('line',3,0);
% Elimination des pixels des contours
BWsdil = imdilate(BWs,[se90 se0]);
BWdfill = imfill(BWsdil,'holes');
% Selection du pixels d'intensite maximal dans l'image
BWnobord = imclearborder(BWdfill,4);
% lissage d'image et selection de la region du code barres
remove_small_region = bwareaopen(BWnobord,50);
% Outline mask
BWoutline = bwperim(BWnobord);
Segout = S; 
Segout(BWoutline) = 255; 
%imshow(Segout)
imshow(labeloverlay(S,BWnobord));