首页 > 解决方案 > Localize and Read BarCodes with Matlab Code

问题描述

I have the following MATLAB code to localize and read barcodes in pictures (JPG,PNG): ## MATLAB CODE ##

%% Read picture
I = imread("4.png");
%% Gray Version
Igray = rgb2gray(I);  
%% Localisation Sobel-Technique 
[~,threshold] = edge(Igray,'sobel');
fudgeFactor = 0.5;
%Binary image
BWs = edge(Igray,'sobel',threshold * fudgeFactor);
%imshow(BWs)
title('Binary Gradient Mask')
se90 = strel('line',3,90);
se0 = strel('line',3,0);
% cleaned Binary image
BWsdil = imdilate(BWs,[se90 se0]);
%imshow(BWsdil)
title('Dilated Gradient Mask')
% Filled with holes
BWdfill = imfill(BWsdil,'holes');
%imshow(BWdfill)
title('Binary Image with Filled Holes')
% BW image
BWnobord = imclearborder(BWdfill,4);%%% High contrast balck/white area. %%%
imshow(BWnobord), title('BW, Image')
rms = bwareaopen(BWnobord,50);
figure(2), imshow(rms, []), title('Last Update');

This code gives me this result: [https://i.stack.imgur.com/e5ckj.jpg][1]

%% Now the problem is how can I Traite the barcode and eliminate other areas. %% The answer I need is: 130760000102 ("barcodes") %% i don't know how to complete the code.

标签: matlabbarcode-scanner

解决方案


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

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),并从只有条形码的图片中给出结果。问题是:如何改进这段代码来处理任何图片(找到条形码并给出结果)?


推荐阅读