Draw a rectangle on an image in gui with mouse hover
5 次查看(过去 30 天)
显示 更早的评论
This question has two parts.
First I want to draw a rectangle on an image in gui only when the mouse hovers on the image. Secondly, if the user clicks the image execute some statements. Right now, I can only draw the rectangle on the image in following manner,
axes(handles.axes1);
[r,c,~]=size(Image);
rectangle('Position', [-2,-2,c+4,r+4],'EdgeColor','r');
3 个评论
采纳的回答
Kevin Phung
2019-1-11
编辑:Kevin Phung
2019-1-11
Hello,
You can use the 'WindowButtonMotionFcn' property of your figure,
f = figure;
set(f,'WindowButtonMotionFcn',@cursorPos)
with the callback function retrieving the position of your cursor:
function cursorPos(my_fig,event)
pos = get(my_fig,'CurrentPoint');
You know the position of your image with:
image_pos = get(image_handle,'Position')
so you can add the logic where if the position is within the bounds of the image: draw a rectangle; else, delete existing rectangle.
If you call the handle of your figure and see all the properties, you can see the various types of callbacks your function can trigger, for example:
WindowButtonDownFcn: ''
WindowButtonMotionFcn: ''
WindowButtonUpFcn: ''
WindowKeyPressFcn: ''
WindowKeyReleaseFcn: ''
WindowScrollWheelFcn: ''
------
The 'WindowButtonDownFcn' should be used for:
set(f,'WindowButtonDownFcn',@call_when_image_clicked)
Hope this helps!
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!