matlab - 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.
解决方案
我已更新此代码以提供图片中条形码的值:
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),并从只有条形码的图片中给出结果。问题是:如何改进这段代码来处理任何图片(找到条形码并给出结果)?
推荐阅读
- python - Python新手,我需要做这个循环吗?我知道它真的很混乱
- persistence - Tabulator column.setWidth() 不保存到历史记录
- javascript - 为什么我的代码在完成之前的代码之前跳到下一个 .then ?
- pymodbus - 具有多个从站的 Pymodbus 串行转发器
- python - 将文件作为元组的元组列表读取
- python - 数据框透视,排列新列值而不创建额外的 NaN 值
- git - 有没有办法设置一个后备 git commit 模板?
- reactjs - 如何修复此错误在现有状态转换期间无法更新
- java - 使用来自 Java 的用户输入运行 cmd-line 程序
- python - 使用 pandas 从 csv 文件保存和检索时丢失 numpy 数组维度