user2190665
user2190665

Reputation:

Detecting specific lines using hough transform/houghlines in matlab

I am currently doing a matlab project where I must isolate a barcode from a image and read the barcode information. The method that I am using is the Hough Transform, once the transform has completed I am using houghpeaks and houghlines so that I can determine the distance between each of the lines on the barcode. My image may be horizontal or vertical.

The problem that I am having is with the houghpeak detection and the houghline plotting. When I have a image that has vertical lines it is not detecting every line of the barcode. I have a link below of a barcode image that I am plotting lines onto, I would like for every vertical line over my specified length(65 in the case of this image) to have a line superimposed on it so that I can then take this information and measure the distance between each line

enter image description here

My reason for choosing 65 as my 'MinLength' is because I am getting horizontal lines plotted in other parts of the image if I dont specify this high value.

I have tried to implement sobel edge detection so that I could specify horizontal/vertical direction but get a error using it: ('Reference to non-existent field 'point1').

I am also not too clear on the 'FillGap' parameter, I have read the matlab help on it but I am still not getting my head around it. I have played around with different values to try and gain a understanding of it but I am not too clear.

I have also tried to implement the code using a image that has more than just a barcode.

enter image description here

In this image it is also only picking up what seems to be random houghpeaks and therefore not plotting the hough lines that I would like it to.

So my question is really can anybody tell me why the code is not plotting all of the hough lines on each 'line' of the barcode in the image.

Below is the code.

Thanks in advance!

 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');

Upvotes: 1

Views: 8743

Answers (1)

Stefan
Stefan

Reputation: 311

Instead of doing line detection to compute your barcode, why don't you plot a profile through the entire length of the barcode. This way you can detect peaks and troughs to distinguish black and white sections.

You would need way less computing power and your code would be much easier.

Upvotes: 1

Related Questions