1

我正在尝试在 matlab 中制作条形码扫描仪。在条形码中,每个白条都是 1,每个黑条都是 0。我正在尝试获取这些条。但这就是问题所在:

在此处输入图像描述

正如您所看到的,条形的宽度不一样,有一次它们是 3 像素……然后是 2 像素等等……更糟糕的是,它们的图像也不同。所以我的问题是。在不知道 1 条宽度的情况下如何获取这些条的值。或者我如何给它们所有相同的宽度。(2个相同的酒吧可以彼此相邻)。无法检测条形之间的过渡,因为在一定数量的像素之后可能会出现过渡......然后可能会有另一个条形或同一个条形。但是因为不可能知道这个特定数量的像素,所以不可能检测到过渡。由于栏没有标准宽度,因此也无法使用某种窗口。那么我该如何规范呢?

条码: 在此处输入图像描述

提前谢谢!

4

1 回答 1

2

让我们假设这些条是严格垂直的(如您的示例中所示)。这是一个可能的工作流程:

%# read the file
filename = 'CW4li.jpg';
x = imread(filename);
%# convert to grayscale
x = rgb2gray(x);

%# get only the bars area
xend = find(diff(sum(x,2)),1);
x(xend:end,:) = [];

%# sum intensities along the bars
xsum = sum(x);

%# threshold the image by half of all pixels intensities
th = ( max(xsum)-min(xsum) ) / 2;
xth = xsum > th;

%# find widths
xstart = find(diff(xth)>0);
xstop = find(diff(xth)<0);
if xstart(1) > xstop(1)
    xstart = [1 xstart];
end
if xstart(end) > xstop(end)
    xstop = [xstop numel(xth)];
end

xwidth = xstop-xstart;

%# look at the histogram
hist(xwidth,1:12)

%# it's clear that single bar has 2 pixels (can be automated), so
barwidth = xwidth / 2;

更新

要获得相对条形宽度,我们可以将宽度(以像素为单位)划分为最小条形宽度:

barwidth = xwidth ./ min(xwidth);

我相信这是一个很好的假设,宽度为 1 时总会有一个条。

如果您不会获得整数值(例如由于噪声),请尝试将数字四舍五入为最接近的整数并获得残差。您可以总结这些残差以获得识别的质量评估。

一些聚类算法(如 k-mean 聚类)也可能运作良好。

于 2011-11-22T17:51:07.307 回答