使用 Matlab 代码本地化和读取条码

问题描述

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

%% 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',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');

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