Detection of line segments and length

조회 수: 10 (최근 30일)
Victoria
Victoria 2018년 6월 29일
댓글: Victoria 2018년 7월 6일

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
Anton Semechko
Anton Semechko 2018년 7월 5일

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 2018년 7월 6일
I guess that's one of my biggest problems as well. But thank you for the help!

댓글을 달려면 로그인하십시오.

채택된 답변

Anton Semechko
Anton Semechko 2018년 7월 1일
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.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by