How can I select a segment from a list?

3 views (last 30 days)
After some image processing I have a vector of line objects (lines) containing the starting and the ending point of all the segments I have found. I need to plot all this segment on the original image and the user should be able to select only those of some interest. I was thinking to make the user to selectmone end of each segment and then iteratively calculate the distance D
D= sqrt(((x1-x2)^2)-((y1-y2)^2));
beetween the selected points and those of the segments. The problem is that it not allways select the right segment and I can't get why. Can you help me with this or suggest anoder solution? Thank you all
close all
clear all
clc
I=imread('Image.jpg');
IBW = rgb2gray(I);
EDG= edge (IBW, 'Canny');
[H,T,R] = hough(EDG);
imshow(H,[],'XData',T,'YData',R,...
'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P = houghpeaks(H,40,'threshold',ceil(0.02*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');
%find lines and plot
% lines = houghlines(EDG,T,R,P);
lines = houghlines(EDG,T,R,P,'MinLength',200);
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');
end
for q = 1: length(lines)
x1(q)=lines(q).point1(1);
y1(q)=lines(q).point1(2);
x2(q)=lines(q).point2(1);
y2(q)=lines(q).point2(2);
end
%% PROBLEM FROM HERE***************************************
[Xp,Yp]=getpts
P=[Xp,Yp];
for k=1:length(P)
for i=1: length(lines)
Dist(i)= Distanza(x1(i), P(k,1),y1(i),P(k,2));
end
MinD= min(Dist);
for i=1: length(lines)
if MinD==Dist(i)
LineaSel=i;
end
end
ArrayLinee(k)=lines(LineaSel);
end
image()
imshow(I)
for k = 1:length(ArrayLinee)
xy = [ArrayLinee(k).point1; ArrayLinee(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','red');
end

Answers (1)

Image Analyst
Image Analyst on 1 Feb 2016
Would bwselect() work for you? It lets the user select desired blobs from teh binary image and returns only those.
BW2 = bwselect(BW,n) displays the image BW on the screen and lets you select the (r,c) coordinates using the mouse. If you omit BW, bwselect operates on the image in the current axes. Use normal button clicks to add points. Press Backspace or Delete to remove the previously selected point. A shift-click, right-click, or double-click selects the final point; press Return to finish the selection without adding a point.
  2 Comments
Dario Bortolotti
Dario Bortolotti on 2 Feb 2016
Thank you for you answer, but I need to trace back the coordinates of the segment chosen from the user, I don't need the actual segment drown in the image. If I have got what bwselect does wrong, please, correct me.
Image Analyst
Image Analyst on 3 Feb 2016
Then I guess I don't know what "trace back the coordinates of the segment chosen from the user" means. Please show an input and an output image and show what the user picked to make you get that output image.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!