Barcode detection using Hough Transform & Edge Detection

狂风中的少年 提交于 2019-12-13 02:38:34

问题


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

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.


回答1:


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.

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.

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];
   }
}

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.

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. 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


来源:https://stackoverflow.com/questions/17783752/barcode-detection-using-hough-transform-edge-detection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!