plotting evenly spaced lines across object

Dear all,
After finding an object boundary, I would like to plot evenly spaced lines (lets say every 5 degrees angle moving clockwise) that will cross the object center and stop at the object periphery at both sides. Something like cutting a cake into pieces.
For example,for an elliptical object,after finding long axis of it (let's call it 0-180 degrees axis), I would like to plot lines every given angle starting from 0-180 axis for entire 180 degrees. So if I would move every 5 degrees, I would get 36 lines.
Does anyone know how to do it and how to get the length of these lines? much appreciate jakub

 採用された回答

Sven
Sven 2013 年 3 月 9 日
編集済み: Sven 2013 年 3 月 9 日

1 投票

Hi Jakub,
I think this does exactly what you're looking for. I've commented the code so it's easy to follow. Note that I've used the intersections entry on the file exchange.
% Make a blob
BW = false(20);
BW(4:16,6:12) = true;
% Get its boundary and a center location
bb = bwboundaries(BW);
bbXY = bb{1}(:,[2 1]);
centXY = mean(bbXY,1);
figure, imagesc(BW), hold on, plot(bbXY(:,1),bbXY(:,2),'g',centXY(1),centXY(2),'yo')
% Make a line that is sure to extend past the object
cutLineXY = [-1 0; 1 0] * sum(size(BW).^2);
% Define how many times we will rotate it
thetas = 0:15:360;
sth = sind(thetas);
cth = cosd(thetas);
for i = 1:length(thetas)
% Rotate the line
R = [cth(i) sth(i); -sth(i) cth(i)];
rotLineXY = cutLineXY * R;
% Shift it to the center
rotLineXY = rotLineXY + [centXY;centXY];
plot(rotLineXY(:,1),rotLineXY(:,2),'k')
% Check where it intersects our boundary
[X,Y] = intersections(rotLineXY(:,1),rotLineXY(:,2),bbXY(:,1),bbXY(:,2));
% Every 2nd intersection will be "inside" the blob
for cutNo = 1:2:length(X)
plot(X(cutNo:cutNo+1), Y(cutNo:cutNo+1),'-w.')
end
end
Does that answer your question?

5 件のコメント

Jakub
Jakub 2013 年 3 月 11 日
Hey Sven,
Sorry for the late reply. Awesome, this is exactly what I needed. And would you know a fast way to put X and Y coordinates of the same line from all lines into array so that I could calculate the lengths.
The idea behind all of this is that I try to quantify direction of the shape change by compering lengths of the lines in two consecutive frames.
Much appreciate your help, cheers, jakub
Sven
Sven 2013 年 3 月 11 日
Instead of plotting, you could just store the XY coordinates of each line.
Before the loop you could set:
% Gotta be a cell in case you could get more than 1 interection.
allLines = cell(size(thetas));
Then inside the loop:
allLines{i} = [X(:) Y(:)];
If you do that you'll collect all of your lines in a cell for future calculations.
Jakub
Jakub 2013 年 3 月 12 日
Cheers Sven!!! Thanks again for your help.
jakub
Jakub
Jakub 2013 年 7 月 2 日
Hi Sven,
Sorry for bothering you again. The code works very well, it plots all the lines correctly, however, when I want to see the XY coordinates of intersections, there are only 2 numbers instead of 25 if one use thetas spacing of 0:15:360. Would you know where could be teh problem?
Much appreciate, jakub
Jakub
Jakub 2013 年 7 月 2 日
I am sorry again, please ignore previous message; all clear, it is just not my day,
cheers, j

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2013 年 3 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by