Getting points with ginput from image and plotting simultaneously each selected point

27 ビュー (過去 30 日間)
I am trying to select an unlimited number of points from an image ([x,y] = ginput), and each time I select a point I want to plot it, so I can have a real time reference of each point I get.
I have tried with this code, but I don't get it to work.
Here is the code I use:
I = imread('cameraman.tif'); % example
imshow(I);
axis on;
button = 1;
i = 1;
n = 1;
while button(i) == 1
[x,y, button] = ginput(1)
x_n(n) = x(i); % save all points you continue getting
x_n(n) = y(i);
hold on
plot(x(i),y(i),'r')
drawnow
n=n+1;
end
I have read some answers where they use for loops with a limited number of points to be selected, but I must have an unlimited number of points, that's why I use as loop condition the pressed button.
Thank you for any help in advance.
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 6 月 3 日
When the user finally clicks a button other than 1, do you want to plot the final point, or do you want to consider that to immediately terminate without entering the point the user is at?
Alfonso
Alfonso 2018 年 6 月 3 日
Each time the user clicks the button with value 1 it means the user selected a new point, so it must plot the point before the user continues selecting more points.
When the user clicks a button different to 1, this means the user has finished, so the while loop breaks.

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

採用された回答

Walter Roberson
Walter Roberson 2018 年 6 月 3 日
編集済み: Walter Roberson 2019 年 11 月 16 日
n = 0;
while true
[x, y, button] = ginput(1);
if isempty(x) || button(1) == 1; break; end
n = n+1;
x_n(n) = x(1); % save all points you continue getting
y_n(n) = y(1);
hold on
plot(x(1), y(1), 'r')
drawnow
end
  7 件のコメント
LO
LO 2020 年 7 月 1 日
編集済み: LO 2020 年 7 月 1 日
is the typo the index for x and y = 1 ?
would x and y alone be Ok ? it seems to work but haven't tested large numbers
Walter Roberson
Walter Roberson 2020 年 7 月 1 日
I think the typo got corrected already ? I do not remember now.
The index for x and y should be 1, since you ginput(1) .

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by