【发布时间】:2013-03-09 13:49:11
【问题描述】:
我目前正在做一个 matlab 项目,我必须从图像中分离出条形码并读取条形码信息。
我使用的方法是霍夫变换,一旦变换完成,我将使用houghpeaks 和houghlines,这样我就可以确定条形码上每条线之间的距离。我的图像可能是水平的或垂直的。
我遇到的问题是houghpeak 检测和houghline 绘图。当我的图像有垂直线时,它不会检测到条形码的每一行。我在条形码图像下方有一个链接,我正在绘制线条,我希望在我指定的长度上的每条垂直线(在此图像的情况下为 65)都有一条线叠加在其上,以便我可以采取这个信息并测量每条线之间的距离
我选择 65 作为我的“MinLength”的原因是因为如果我不指定这个高值,我会在图像的其他部分绘制水平线。
我尝试实现 sobel 边缘检测,以便我可以指定水平/垂直方向,但使用它时出错:('Reference to non-existent field 'point1')。
我对@987654327@ 参数也不是很清楚,我已经阅读了有关它的matlab 帮助,但我仍然没有理解它。我玩过不同的价值观来尝试理解它,但我不太清楚。
我还尝试使用不仅仅是条形码的图像来实现代码。
在这张图片中,它也只是拾取似乎是随机的霍夫峰,因此没有绘制我想要的霍夫线。
所以我的问题是真的有人能告诉我为什么代码没有在图像中条形码的每个“行”上绘制所有霍夫线。
下面是代码。
提前致谢!
I = imread('barcode (2).jpg');
I = im2double(I);
I = rgb2gray(I);
BW = edge(I,'canny');
[H,T,R] = hough(BW);
figure(1),imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P = houghpeaks(H,26,'threshold',ceil(0.5*max(H(:))));
x = T(P(:,2));
y = R(P(:,1));
plot(x,y,'s','color','white');
% Find lines and plot them
lines = houghlines(BW,T,R,P,'FillGap',2,'MinLength',65);
figure, imshow(BW), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');
【问题讨论】: