필터 지우기
필터 지우기

Draw a rectangle on an image in gui with mouse hover

조회 수: 6 (최근 30일)
Abdulllah
Abdulllah 2019년 1월 11일
댓글: Abdulllah 2019년 1월 14일
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
Abdulllah
Abdulllah 2019년 1월 11일
Thank you for reply. I want to draw the rectangle when mouse hovers on the image and when the mouse is out of image then rectangle should get removed. Like mouse on image, draw rectangle on image, mouse not on image no rectangle.
Then when the mouse is on the image and there is a left click on the image then I want to execute some other set of statements. May be you can call it a function
call_when_image_clicked()
Abdulllah
Abdulllah 2019년 1월 11일
I am sorry, I forget to reply second question. These statemens drawing the rectangle on image ver well.
[r,c,~]=size(Image);
rectangle('Position', [-2,-2,c+4,r+4],'EdgeColor','r');
New Bitmap Image.bmp

댓글을 달려면 로그인하십시오.

채택된 답변

Kevin Phung
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!
  댓글 수: 1
Abdulllah
Abdulllah 2019년 1월 14일
Hello, Thank you for your reply. I can now WindowButtonMotionFcn and can draw the rectangle. but I am I unable to find the postions of the image. If I find position of the image using
get(eventdata.axes1,'Position')
I get values in points (). and if I use
image_pos = get(eventdata.figure1,'Position')
I get value in even -ve (1084.2 -55.8 2033.6 1115.2). I dont know why negative,
I am using this logic now to draw the rectangle under the WindowButtonMotionFcn.
pos1 = getpixelposition(eventdata.axes1,true)
currentp=src.CurrentPoint
image_pos = get(eventdata.figure1,'Position')
x1=680;
y1=1046;
matlabImage = imread(strcat(num2str(randArr(3)),'.tif'));
[r,c,~]=size(matlabImage);
x2=1397;
y2=512;
if ((currentp(1)<x2&&currentp(1)>x1)&&currentp(2)>y2&&currentp(2)<y1)
% axes(hObject.figure1);
rectangle('Position', [-2,-2,c+4,r+4],'EdgeColor','r','LineWidth',3);
else
rectangle('Position', [-2,-2,c+4,r+4],'EdgeColor',[0,0,0]','LineWidth',3);
end
I need to determine the values of x1,y1,x2,y2.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by