Pixel Values from the Computer Screen
3 views (last 30 days)
Show older comments
Hi,
I am trying to track the change of specific pixels on my computer screen, which are going to indicate the feedback on/off position, by providing a green light, for another application. Is it possible to track the pixel values for directly the pixels on the main computer screen by using MATLAB ? Overall, I am trying to write a code which will alert me if the feedback is closed and the light is switched off. I would appreciate if there is a solution by using MATLAB but I am open for other solution methods as well.
1 Comment
Voss
on 22 Dec 2021
The way I would first try to approach this problem would be (1) to programmatically take a screenshot and then (2) check the resulting image for the existence of the green light in the specific location. Part 2 is specific to your requirements, but a couple of ways you might try to do part 1 are:
(A) use a java.awt.Robot (from within MATLAB), which has a method createScreenCapture for capturing a screenshot. Here is how I have used it in the past:
r = java.awt.Robot();
monitor_positions = get(groot(),'MonitorPositions');
monitor_positions(:,1) = monitor_positions(:,1)-1;
monitor_positions(:,2) = 0;
pos = monitor_positions(1,:);
cap = r.createScreenCapture(java.awt.Rectangle(pos(1),pos(2),pos(3),pos(4)));
w = cap.getWidth();
h = cap.getHeight();
rgb = typecast(cap.getRGB(0,0,w,h,[],0,w),'uint8');
rgb = cat(3,reshape(rgb(3:4:end),w,[]).',reshape(rgb(2:4:end),w,[]).',reshape(rgb(1:4:end),w,[]).');
This will give you rgb, a 3d matrix of type uint8 representing a screenshot of the 1st monitor (i.e., the monitor whose screen position is given by the first row of get(groot(),'MonitorPositions')).
(The systems I use this on only ever have one monitor, and on the mulitple-monitor system I'm using now, it gives some incomplete/incorrect result. But I'll leave it here as a reference/starting off point for you to try to adapt it to work on your system.)
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!