Advertisement
backlog

hough1.m

Apr 1st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.39 KB | None | 0 0
  1. fs=imread('t2.jpg');
  2. i=rgb2gray(fs);
  3. %i=imread('text.png');
  4. rot=imrotate(i,33,'crop');
  5.  
  6. imshow(rot)
  7.  
  8. bw=edge(rot,'canny');
  9. hold on
  10. imshow(bw)
  11.  
  12. %Compute the Hough transform of the binary image returned by edge.
  13.  
  14. [h,theta,rho] = hough(bw);
  15.  
  16. % display h returned by hough trans
  17.  
  18. %Find the peaks in the Hough transform matrix, H, using the houghpeaks function.
  19.  
  20. p=houghpeaks(h,7,'threshold',ceil(0.3*max(h(:))));
  21.  
  22.  
  23. %Superimpose a plot on the image of the transform that identifies the peaks.
  24. x = theta(p(:,2));
  25. y = rho(p(:,1));
  26. plot(x,y,'s','color','black');
  27.  
  28. %Find lines in the image using the houghlines function.
  29.  
  30. lines = houghlines(bw,theta,rho,p,'FillGap',5,'MinLength',7);
  31.  
  32. %Create a plot that displays the original image with the lines superimposed on it.
  33.  
  34.  
  35.  
  36. figure, imshow(rot), hold on
  37. max_len = 0;
  38. for k = 1:length(lines)
  39.    xy = [lines(k).point1; lines(k).point2];
  40.    plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','cyan');
  41.  
  42.    % Plot beginnings and ends of lines
  43.   plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
  44.   plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
  45.  
  46.    % Determine the endpoints of the longest line segment
  47.    len = norm(lines(k).point1 - lines(k).point2);
  48.    if ( len > max_len)
  49.       max_len = len;
  50.       xy_long = xy;
  51.    end
  52. end
  53. % highlight the longest line segment
  54. %plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement