Detection of line segments and length
10 次查看(过去 30 天)
显示 更早的评论
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
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"?
采纳的回答
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.
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!