MATLAB Answers

## Detection of line segments and length

Asked by Victoria

### Victoria (view profile)

on 29 Jun 2018
Latest activity Commented on by Victoria

### Victoria (view profile)

on 6 Jul 2018
Accepted Answer by Anton Semechko

### Anton Semechko (view profile)

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!

Anton Semechko

### Anton Semechko (view profile)

on 2 Jul 2018
I think that a tractography-based approach will be more appropriate than Hough transform for the types of features you want to extract (i.e., curved filaments). Tractography  is typically used for reconstruction of white matter and muscle fiber architecture from diffusion tensor images. However, the same principles can be adapted to your data by replacing diffusion tensors with image structure tensors [2,3].
 Mori & van Zijl, 2002, 'Fiber tracking: Principles and strategies - a technical review'
 Weickert, 1999, 'Coherence-enhancing diffusion filtering'
 Weickert & Scharr, 2000, 'A scheme for coherence-enhancing diffusion filtering with optimized rotation invariance'
Anton Semechko

### Anton Semechko (view profile)

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 (view profile)

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.

## 1 Answer

Answer by Anton Semechko

### Anton Semechko (view profile)

on 1 Jul 2018
Accepted Answer

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 Comments

Sign in to comment.