使用Hough变换和条形码检测条形码。边缘检测 [英] Barcode detection using Hough Transform & Edge Detection

查看:312
本文介绍了使用Hough变换和条形码检测条形码。边缘检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Matlab code for barcode detection using Hough tranform & edge detection techniques is required.

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

I tried built in matlab functions for this but could not get the result because i know very little about Hough transform, edge detection or barcode detection

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

So, any kind of help is much appreciated. So far i did this ..

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 

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

now i need an algorithm for barcode scanning/decoding ... & also need suggestion for better ways to do Hough Transform.

推荐答案

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

So, the Hough transform MUST be used? Barcode analysis is usually much easier than this, since you know a priori that all the barcode lines are parallel. If you can guarantee that they will be scanned relatively accurately (ie: the bar code scanner is operated roughly perpendicular to the barcode lines, give or take 30 degrees each way), you can just take a single "slice" of the barcode image, so it goes from an MxN image to a 1xN image.

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

You then just convert the image to plain black and white, either using thresholding, or segmentation (ie: K-means segmentation). You can then perform noise removal (a median or mode filter would be ideal) so that a single noisy pixel doesn't split a single barcode line into two separate lines.

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

Finally, a single for loop can be used to calculate the width of each column:

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 /边缘检测滤波器只会创造更多的工作,而不会使问题更容易解决。

In general, edge detection isn't useful for barcodes, because they already are parallel edges anyway, and the edge detection algorithm introduces the need for some basic filtering and possibly thresholding to reduce the grayscale image to black-and-white. Using a simple LaPlace/edge-detection filter for this type of problem just creates more work, without making the problem any easier to solve.

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

Also, Hough Transforms (ie: the trivial, non-parametric form) are useful for detecting shapes (a common undergraduate problem is counting the number of pencils in a picture, where there is overlap, or orthogonal pencils). The more complicated/parametric form of the transform is useful for detecting arbitrary objects in pictures.


  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

  1. Thresholding http://en.wikipedia.org/wiki/Thresholding_%28image_processing%29
  2. K-means segmentation http://en.wikipedia.org/wiki/K_means
  3. LaPlace filters http://www.owlnet.rice.edu/~elec539/Projects97/morphjrks/laplacian.html
  4. Mode filters http://www.roborealm.com/help/Mode.php

这篇关于使用Hough变换和条形码检测条形码。边缘检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆