I am using the following to allow the user to use the mouse press to draw a marker on an image loaded to an axes component (axes1). There are other axes also on my GUI, and I want to be able to constrain this mouse action only if the mouse is over axes 1. Currently it also draws a marker when over other axes.
I put this in the opening function.
set(gcf, 'WindowButtonDownFcn', @getMousePositionOnImage);
And then use this function for the mouse click.
function getMousePositionOnImage(src, event)handles = guidata(src);cursorPoint = get(handles.axes1, 'CurrentPoint');curX = cursorPoint(1,1);curY = cursorPoint(1,2);%check if mouse clicked outside of axes component
xLimits = get(handles.axes1, 'xlim');yLimits = get(handles.axes1, 'ylim');if (curX<min(xLimits)) (curX==min(xLimits))endif (curX>max(xLimits)) (curX==max(xLimits))endif (curY<min(yLimits)) (curY==min(yLimits))endif (curY>max(yLimits)) (curY==max(yLimits))endx=round(curX)y=round(curY)setappdata(0,'curX',x);setappdata(0,'curY',y);%Remove previous marker if present
delete( findobj(gca, 'type', 'line') );hold onplot(x,y,'ro','MarkerSize',10,'LineWidth',2)hold off;IMG = getimage(handles.axes1);delta=50;if (x<delta+1)|| (y<delta+1) ROI = IMG(1:delta, 1:delta,:); % This is the ROI (indexing is (ymin:ymax, xmin:xmax)
else ROI = IMG(y-delta:y+delta, x-delta:x+delta,:); % This is the ROI (indexing is (ymin:ymax, xmin:xmax)end%Now take a ROI around the marker position (i.e. where mouse is clicked)
%and draw in axes 3
axes(handles.axes3)cla[high,low]=Autoscaleimage(handles,ROI,3);imshow(ROI,[low,high]);hold onplot(delta+1,delta+1,'ro','MarkerSize',10,'LineWidth',2);hold off
Best Answer