How to convert from figure coordinate to coordinates of the screen (monitor)

5 views (last 30 days)
I run the code shown below to take screen shot of the current screen. Monitor resolution is 1920*1080. I am storing the screen capture as an image named imgdata. Suppose if I detect a particular item from the image (say a folder, based on its color), i am able to get its centroid. How to convert these coordinates of centroid to screen coordinates?
% Take screen capture
robot = java.awt.Robot();
pos = [0 0 1920 1080]; % [left top width height]
rect = java.awt.Rectangle(pos(1),pos(2),pos(3),pos(4));
cap = robot.createScreenCapture(rect);
% Convert to an RGB image
rgb = typecast(cap.getRGB(0,0,cap.getWidth,cap.getHeight,[],0,cap.getWidth),'uint8');
imgData = zeros(cap.getHeight,cap.getWidth,3,'uint8');
imgData(:,:,1) = reshape(rgb(3:4:end),cap.getWidth,[])';
imgData(:,:,2) = reshape(rgb(2:4:end),cap.getWidth,[])';
imgData(:,:,3) = reshape(rgb(1:4:end),cap.getWidth,[])';

Accepted Answer

Walter Roberson
Walter Roberson on 24 Dec 2017
Remember that X is columns and Y is rows.
And remember to subtract 1 to go from the 1-based indexing of MATLAB arrays to the 0-based screen coordinates.
  4 Comments
Ali razi
Ali razi on 9 Oct 2022
Any idea how to convert this line
pos = [0 0 1920 1080]; % [left top width height]
so it will auto detect the screen dimension?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!