Search code examples
matlabmouseevent

Get mouse position on screen (no figure)


I'm using a projector to project something on the ground at a certain angle. But to do that whilst maintaining proportions and not let it be distorted by the keystone effect, I need to find a homography matrix.

What I'd like to do is take an object of known size (a sheet of A4 paper) and place it on the ground. Then click on its edges to get their positions on screen, and then calculate what I need.

I thought of using the get(0,'Pointerlocation') but do not know how to get its value "on click". Maybe use in combination with input(prompt)?

Small clarification, I am not using a figure behind this.


Solution

  • If there really is no figure and you are trying to get coordinates outside of the Matlab window/figure and perhaps on a separate video app, you could try something like the following.

    It basically has a countdown for you to get your mouse anywhere on the screen and it will return the pixel position of your mouse on the montitor. You don't have to click on anything. Just make sure you can see the Main Matlab window so you know when to move the mouse.

    function screenCoord = getCorners()
    screenCoord = zeros(4,2);
    
    for ii = 1:4
        fprintf('Move cursor to Position %d: \n',ii)
        countDown()
        screenCoord(ii,:) = get(0,'PointerLocation');
    end
    
    function countDown()
    fprintf('Capturing in: ');
    for i = 5:-1:1
        fprintf('%d..',i);pause(1)
    end
    fprintf('0..\n')
    

    Output:

    >> screenCoords = getCorners
    Move cursor to Position 1: 
    Capturing in: 5..4..3..2..1..0..
    Move cursor to Position 2: 
    Capturing in: 5..4..3..2..1..0..
    Move cursor to Position 3: 
    Capturing in: 5..4..3..2..1..0..
    Move cursor to Position 4: 
    Capturing in: 5..4..3..2..1..0..
    
    screenCoords =
    
       113   922
       943   904
       976   356
       323   376
    

    EDIT: An alternative to the countdown would be to use the input function as long as you can keep the Main Matlab window as the focus. Just replace the call to countDown() with

    input('Press Enter when ready.');