Does anyone know how I can get the current position of my cursor when I move over my figure with my cursor?

조회 수: 95 (최근 30일)
This position should be relative to the lower left corner of the figure and the unit should be pixels (to be able to define the position of the axes for example).
How to get the current pointer/mouse position in pixels relative to the lower left corner of the parent container (the red cross should have pixel coordinates [0,0])?
Orange = imread('77.jpg');
figure
imshow(Orange)

답변 (2개)

Voss
Voss 2021년 12월 17일
cp = get(gcf(),'CurrentPoint');
display(cp);
  댓글 수: 1
S.
S. 2021년 12월 19일
편집: S. 2021년 12월 19일
It doesn't seem to work. When I run it, I get cp = [0 0] and if I click on a different place in the parent container nothing happens. Why?

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


Image Analyst
Image Analyst 2021년 12월 19일
Use impixelinfo:
Orange = imread('77.jpg');
imshow(Orange)
axis('on', 'image')
impixelinfo
  댓글 수: 11
Image Analyst
Image Analyst 2022년 1월 6일
In all the years I've used MATLAB I've never had to mouse around outside an axes and get the (x,y) coordinates. I've only needed the coordinates inside the image itself.
You can get the 'Position' property for all the controls on the figure window, and then dynamically place things. Like if you want a scroll bar halfway between the left of the figure and the left of the axes you can do
pos = handles.axes1.InnerPosition;
pos2 = handles.listbox1.Position;
pos2(1) = pos(1)/2;
handles.listbox1.Position = pos2;
The only thing different than knowing the x value after interactively mousing around and finding it, is line 3 where it's computed relative to other things on the figure window.
S.
S. 2022년 1월 10일
Thanks for the answer.
I used now this:
while true
get(0, 'PointerLocation')
pause(0.01);
end
It gives the location compared to the lower left corner of my computer window. With a bit head calculation, I could place everything.

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

카테고리

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