Question

I want to detect lines in a text document. Here is the original image this was eroded to make the task of edge detection easier using the erode function. Here is the eroded image.

Now to detect the lines I used houghlines, and used the following code in my script file.

I  = imread('c:\new.jpg');
rotI = imrotate(I,33,'crop');
bw_I = rgb2gray(rotI);
BW = edge(bw_I,'canny');
[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');
% Find lines and plot them
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

% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','blue');

This produced this result. Now I know that the intersecting points are the detected lines. What I want is to somehow show these lines detected onto the original image such as highlighting lines or underlining them. Is this possible? Which function would I use for that?

edit: What I wanted to say was that how do I translate the detected lines( intersecting points) from the last result to a more clearer result.

Was it helpful?

Solution

You want to apply imshow to the results of the edge function-call.

This part of the Matlab documentation explains what you are trying to accomplish:

  1. Read an image into the MATLAB workspace.

    I  = imread('circuit.tif');
    
  2. For this example, rotate and crop the image using the imrotate function.

    rotI = imrotate(I,33,'crop');
    fig1 = imshow(rotI);
    
  3. Find the edges in the image using the edge function.

    BW = edge(rotI,'canny');
    figure, imshow(BW);
    

It is this 3rd step you are after. You already ran the edge function.
Now, all that remains is visualizing the results BW with imshow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top