Segmentation of interconnected lines

3 views (last 30 days)
Hi!
I'm working on a project that require to segment shapes or features from an image. My actual approach is to first detect all lines with Hough Transform and after try to segment each line by its slope, hower I've tried k-means, DBSCAN, and I didn't get the result that I would like to achieve.
I would like to know if someone can help me with a idea or documentation.
Thank You!
  5 Comments
James Ben
James Ben on 6 Nov 2022
Edited: James Ben on 6 Nov 2022
I would like to extract each shape separately from the image. I've done a look on the bwlabel() method due the curved lines, but I didn't know how to implement in my code.
Here is my actual code:
RGB = imread('link_PRE.png');
I = im2gray(RGB);
% Extract edges
BW = edge(I,'canny');
% % Calculate Hough Transform
[H,T,R] = hough(BW,"RhoResolution",0.5,"Theta",-90:0.1:89);
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
%Find lines and plot them.
lines = houghlines(BW,T,R,P,'FillGap',50,'MinLength',0.5);
figure, imshow(I), 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 beginnings and ends of lines
%plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
%plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 10 Nov 2022
Moved: Image Analyst on 10 Nov 2022
You can get all the angles like this:
lines = houghlines(BW,T,R,P,'FillGap',50,'MinLength',0.5)
allAngles = [lines.theta]
If you want to get each segment individually find crossing points and delete them, then label the image. Untested code:
% Find branchpoints
bpImage = bwmorph(BW, 'branchpoints');
% Erase branchpoints from main binary image.
BW(bpImage) = false;
% Label what's left.
[labeledImage, numSegments] = bwlabel(BW, 4);
% Show them all one at a time.
for k = 1 : numSegments
thisImage = ismember(labeledImage, k);
imshow(thisImage);
drawnow;
% Delay a bit so we can see it.
pause(0.5);
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!