hi,, i'm new in matlab.. i want to draw using the mouse with this code, but this code can only make one line,, i want to make it more..,

1 次查看(过去 30 天)
hFH = imfreehand (); % Get the xy coordinates of where they drew.
xy = hFH.getPosition; % get rid of imfreehand remnant.
delete(hFH); % Overlay what they drew onto the image.
hold on; % Keep image, and direction of y axis.
x = xy(:, 1);
y = xy(:, 2);
% X = [X x];
% Y = [Y y];
line(x, y, 'Color','k','LineWidth', 4);

采纳的回答

Kan-Hua
Kan-Hua 2013-12-11
Hi,
The simplest way is initialising an axes object that imfreehand() function can draw on to, and put your function into an infinite loop, like this:
ax=axes();
while true
hFH = imfreehand(ax); % Get the xy coordinates of where they drew.
xy = hFH.getPosition; % get rid of imfreehand remnant.
delete(hFH); % Overlay what they drew onto the image.
hold on; % Keep image, and direction of y axis.
x = xy(:, 1);
y = xy(:, 2);
% X = [X x];
% Y = [Y y];
line(x, y, 'Color','k','LineWidth', 4);
end
Is this what you are looking for?
  3 个评论
Kan-Hua
Kan-Hua 2013-12-16
The error message happens because the function iamfreehand() is waiting for your response, but the user close the figure to end the program, so iamfreehand() function cannot return any output to hFH. This does not affect the program itself, but if you are really bothered by the error message, you can add a try/catch to catch that error message, like this:
close all;
ax=axes();
while true
try
hFH = imfreehand(ax); % Get the xy coordinates of where they drew.
xy = hFH.getPosition; % get rid of imfreehand remnant.
catch err
if strcmp(err.identifier,'images:roiParseInputs:invalidHandle');
disp('program exit');
break;
end
end
delete(hFH); % Overlay what they drew onto the image.
hold on; % Keep image, and direction of y axis.
x = xy(:, 1);
y = xy(:, 2);
% X = [X x];
% Y = [Y y];
line(x, y, 'Color','k','LineWidth', 4);
end
In Image Analyst's code, he set a dialog to ask whether the user want to continue in his main loop, so he can avoid this error message.
To Image Analyst: Thank you for your code though. I actually learned a lot from your code. :)

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2013-12-11
See my demos of imfreehand, attached.

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by