Detection of line segments and length

10 visualizaciones (últimos 30 días)
Victoria
Victoria el 29 de Jun. de 2018
Comentada: Victoria el 6 de Jul. de 2018

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 comentarios
Anton Semechko
Anton Semechko el 5 de Jul. de 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 el 6 de Jul. de 2018
I guess that's one of my biggest problems as well. But thank you for the help!

Iniciar sesión para comentar.

Respuesta aceptada

Anton Semechko
Anton Semechko el 1 de Jul. de 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.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by