让我们假设这些条是严格垂直的(如您的示例中所示)。这是一个可能的工作流程:
%# 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 聚类)也可能运作良好。