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

coordinate-transformationcoordinatescreatescreencapturefigurefigure coordinates to screen coordinatesfigure windowimage resizingjava robotjava.awt.robotrobotscreen capture

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,[])';

Best Answer

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.