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 view (last 30 days)
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);

Accepted Answer

Kan-Hua
Kan-Hua on 11 Dec 2013
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 Comments
Image Analyst
Image Analyst on 14 Dec 2013
Why'd you accept it then? With my demo there is no such error. Check out my demos to see how it should be done.
Kan-Hua
Kan-Hua on 16 Dec 2013
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. :)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 11 Dec 2013
See my demos of imfreehand, attached.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!