使用Hough变换和条形码检测条形码。边缘检测

时间:2013-07-22 09:16:17

标签: matlab image-processing barcode hough-transform

使用Hough变换和条形码检测条形码的Matlab代码。边缘检测技术是必需的。

我试过内置matlab函数,但无法得到结果,因为我对Hough变换,边缘检测或条形码检测知之甚少

因此,非常感谢任何形式的帮助。到目前为止,我做到了这一点。

a=imread('BEAN13.jpg');

b = rgb2gray(a);
rotI = imrotate(b,30,'crop');
%figure,imshow(rotI)

BW = edge(rotI,'sobel');
[H,T,R] = hough(BW);
imshow(H,[],'XData',T,'YData',R,...
    'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P  = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');

lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow(rotI), 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 

现在我需要一种条形码扫描/解码算法......&还需要建议更好的方法来进行霍夫变换。

1 个答案:

答案 0 :(得分:3)

那么,必须使用Hough变换吗?条形码分析通常比这更容易,因为您事先知道所有条形码线是平行的。如果您可以保证它们将被相对准确地扫描(即:条形码扫描仪大致垂直于条形码线操作,每个方向给出或取30度),您可以只取一个条形码图像的“切片”,所以它从MxN图像变为1xN图像。

然后,您只需使用阈值处理或分割(即:K-means分割)将图像转换为纯黑色和白色。然后,您可以执行噪声消除(中位数或模式滤波器将是理想的),以便单个噪声像素不会将单个条形码线分成两个单独的行。

最后,可以使用单个for循环来计算每列的宽度:

pixel_color = img[0][0];
int i;
int width = image_width(img); // 'N'
unsigned bars[width] = { 0 };
int currentBar = 0;

for (i=0; i<width; i++) {
   bars[currentBar] += 1;
   if (img[0][i] != pixel_color) { // Detected a new bar
     currentBar++;
     pixel_color = img[0][i];
   }
}

通常,边缘检测对于条形码没有用,因为它们无论如何都已经是平行边缘,并且边缘检测算法引入了对一些基本滤波的需要并且可能进行阈值处理以将灰度图像缩小为黑白。对这类问题使用简单的LaPlace /边缘检测过滤器只会创造更多的工作,而不会使问题更容易解决。

此外,Hough变换(即:平凡的非参数形式)对于检测形状非常有用(常见的本科问题是计算图片中铅笔的数量,有重叠的铅笔或正交铅笔)。更复杂/参数化的变换形式对于检测图片中的任意对象非常有用。


  1. 阈值http://en.wikipedia.org/wiki/Thresholding_%28image_processing%29
  2. K-means分段http://en.wikipedia.org/wiki/K_means
  3. LaPlace过滤器http://www.owlnet.rice.edu/~elec539/Projects97/morphjrks/laplacian.html
  4. 模式过滤器http://www.roborealm.com/help/Mode.php
相关问题