Detection of line segments and length

21 views (last 30 days)

I am trying to have MATLAB detect multiple line segments in a images and then tell the length (in pixels). I have tried the following code:

    I=imread('GFP1C2.tif');
canny=edge(I,'canny');
[H,T,R]=hough(canny);
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.8*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(canny),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(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
    plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
    len=norm(lines(k).point1-lines(k).point2);
    if (len>max_len)
        max_len=len;
        xy_long=xy;
    end
end

This code will neither accurately detect the line segments nor will tell me the length of the segment. Help is appreciated; thanks!

  10 Comments
Anton Semechko
Anton Semechko on 5 Jul 2018

Hi, Victoria,

I was playing around with curvature filters and your image. What I found suggests that the filaments in your image do not have a coherent direction and thus cannot be easily segmented. Rather, the structure in your image is more like a matrix composed of interweaving fibers:

In view of this, how would you define the beginning and end of a single "line segment"?

Victoria
Victoria on 6 Jul 2018
I guess that's one of my biggest problems as well. But thank you for the help!

Sign in to comment.

Accepted Answer

Anton Semechko
Anton Semechko on 1 Jul 2018
It doesn't look very clear to me, and is maybe the reason why the Hough transform isn't generating the output you want. For example, have you considered enhancing the filaments in your image prior to edge detection?
% Sample image
im=imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/123386/GFP1C28cropped.jpg');
figure
subtightplot(2,2,1), imshow(im)
subtightplot(2,2,3), imshow(edge(im,'canny'))
% Enhance fillaments with Frangi vesselness filter
opt.FrangiScaleRange=[1 3];
opt.FrangiScaleRatio=1;
opt.FrangiBetaOne=1;
opt.FrangiBetaTwo=20;
opt.BlackWhite=false;
[im_2,im_s]=FrangiFilter2D(double(im),opt);
subtightplot(2,2,2), imshow(im_2)
subtightplot(2,2,4), imshow(edge(im_2,'canny'))
Notice how much less cluttered is the edges image after enhancement. Try experimenting with different filter settings to see what works best for you.
Functions used in this demo can be downloaded from here and here.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!