Drawing circles/squares on an image where the user specifies
I have a GUI in MATLAB created using guide
. So far the user can import an image and save the image. My next step is to allow the user to click on the image to place a circle/square, doesn't matter which, the easier one to implement. Ideally I would like to make it so the latest one created is undoable but the basic implementation is paramount.
I'm assuming for the actual drawing of the circle that using PLOT will fine, as mentioned in several other questions. What I'm not sure of is how I would go about getting the location in the image of the mouse click to then place the PLOT at that location.
EDIT: Here's the main bit of code that I have for the working parts at t开发者_StackOverflow中文版he moment.
function V1Open_Callback(hObject, eventdata, handles)
% hObject handle to V1Open (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
hMainGui = getappdata(0, 'hMainGui');
fileName = uigetfile('*.jpg');
setappdata(hMainGui, 'fileName', fileName);
updateAxes1
function updateAxes1
hMainGui = getappdata(0, 'hMainGui');
fileName = getappdata(hMainGui, 'fileName');
imshow(imread(fileName))
% --- Executes on button press in V1Save.
function V1Save_Callback(hObject, eventdata, handles)
% hObject handle to V1Save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
imsave;
This question is a bit old but you can simply do this, assuming hAxes is the handle of the axes of your image:
Radius=0.5;
xy1 = get(hAxes,'Currentpoint');
x1 = xy1(1,1);
y1 = xy1(1,2);
Rec=rectangle('Position',[x1-Radius,y1-Radius,2*Radius,2*Radius],'Curvature',[1,1],'HitTest','off');
You can change of course the radius to any value. The get(hAxes,'Currentpoint') function retrieves the last place where you clicked relatively to your axes. the 'curvature' set to 1,1 makes the rectangle a circle... 0,0 will let it be a rectangle of course.
edit: typo
Are you familiar with the [x, y] = ginput
? That's the fundamental function to get the position where your user clicked the mouse. For more details can be found for example here.
You may also want to show us your current code, inorder to able us to answer to your question more specific manner!
I use the image processing toolbox supplied functions like imellipse and imrect to do this:
Have a button the user can click to start to place the ellipse, this drops them into the figure and allows them to place and size the ellipse. Once they're done resizing it they can double click and you get the results. I tend to store a handle to the ellipse object, and its actual position.
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
axes(handles.axes1);
if (isfield(handles, 'ellipse'))
delete(handles.ellipse);
end
handles.ellipse = imellipse();
handles.eps = getPosition(handles.ellipse);
guidata(hObject, handles);
精彩评论