Detecting cursor position inside or outside an object.

조회 수: 4 (최근 30일)
Ravi Rastogi
Ravi Rastogi 2011년 7월 20일
Hi Everyone,
I have a problem and can not think of a how to approach it? Here it goes. I have an binary image containing a random line object inside a rectangle. Using the bwboundaries, i know the boundary points of the rectangle and random object. Now when i move the cursor, i get its current position as shown below.
[B,L,N,A] = bwboundaries(BW);
%Determine the screen size
SS = get(0,'ScreenSize');sh=SS(4); sw=SS(3);
%Determine current location of cursor
PL = get(0,'PointerLocation');
Yc=PL(1); Xc=sh-PL(2);
How can i know, if my current cursor location (Xc,Yc) is: 1) Outside the rectangle? Then perform single "beep" 2) inside the rectangle but outside the random object? perform "beep; pause(0.5); beep" 3) inside the object? Perform "beep; pause(0.5); beep; pause(0.5); beep"
Thanks RR

채택된 답변

Jan
Jan 2011년 7월 20일
The is the pointer location relative to the screen:
PL = get(0,'PointerLocation');
But I assume you want it either relative to the figure or an axes:
PLfig = get(gcf, 'CurrentPoint');
PLaxes = get(gca, 'CurrentPoint');
For live updates of the figure's current point value an WindowButtonMotionFcn must be defined.
For the in-polygon test see Walter's answer.
  댓글 수: 1
Ravi Rastogi
Ravi Rastogi 2011년 7월 20일
The position of the cursor is with respect to the screen, which is changes to match it with image matrix. Your suggestion is a cool way though!! thanks

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 7월 20일
There are also MATLAB File Exchange (FEX) for this, such as inpoly()
  댓글 수: 1
Ravi Rastogi
Ravi Rastogi 2011년 7월 21일
hi walter, thanks for your suggestion. For my application i just ended up using colors to distinguish different parts. Thanks for your advice. Great help!!

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

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by