0. 前言
使用MATLAB实现定时截取桌面固定区域并OCR。
对于数据实时变动比较大、肉眼难易分辨的数据均可采用该方法,使用本地定时器完成。
同时,本文也可应对大批量的数据处理。
本例程对应用场景进行了简化处理,为:
固定时间间隔——60秒,截取桌面右下角时间的分钟10次。
1. screencapture下载
对于截屏,MATLAB有现成的函数,直接调用即可:
ScreenCapture - screenshot of component, figure or screen
https://www.mathworks.com/matlabcentral/fileexchange/24323-screencapture-screenshot-of-component-figure-or-screen?s_tid=srchtitle_screencapture_2
本文例程中只需要知道当前屏幕分辨率及以下使用方法即可:
% 当前屏幕分辨率1440*900
% handle为0表示截取桌面
% position: [x,y,width,height],以左下角屏幕为起点
% imageData = screencapture(handle, position, target, 'PropName',PropValue, ...)
如果你有更多使用需要,可以查看screencapture.m
头部注释。
函数不同情况下的具体使用方法screencapture.m
中介绍的都比较清楚:
% screencapture - get a screen-capture of a figure frame, component handle, or screen area rectangle
%
% ScreenCapture gets a screen-capture of any Matlab GUI handle (including desktop,
% figure, axes, image or uicontrol), or a specified area rectangle located relative to
% the specified handle. Screen area capture is possible by specifying the root (desktop)
% handle (=0). The output can be either to an image file or to a Matlab matrix (useful
% for displaying via imshow() or for further processing) or to the system clipboard.
% This utility also enables adding a toolbar button for easy interactive screen-capture.
%
% Syntax:
% imageData = screencapture(handle, position, target, 'PropName',PropValue, ...)
%
% Input Parameters:
% handle - optional handle to be used for screen-capture origin.
% If empty/unsupplied then current figure (gcf) will be used.
% position - optional position array in pixels: [x,y,width,height].
% If empty/unsupplied then the handle's position vector will be used.
% If both handle and position are empty/unsupplied then the position
% will be retrieved via interactive mouse-selection.
% If handle is an image, then position is in data (not pixel) units, so the
% captured region remains the same after figure/axes resize (like imcrop)
% target - optional filename for storing the screen-capture, or the
% 'clipboard'/'printer' strings.
% If empty/unsupplied then no output to file will be done.
% The file format will be determined from the extension (JPG/PNG/...).
% Supported formats are those supported by the imwrite function.
% 'PropName',PropValue -
% optional list of property pairs (e.g., 'target','myImage.png','pos',[10,20,30,40],'handle',gca)
% PropNames may be abbreviated and are case-insensitive.
% PropNames may also be given in whichever order.
% Supported PropNames are:
% - 'handle' (default: gcf handle)
% - 'position' (default: gcf position array)
% - 'target' (default: '')
% - 'toolbar' (figure handle; default: gcf)
% this adds a screen-capture button to the figure's toolbar
% If this parameter is specified, then no screen-capture
% will take place and the returned imageData will be [].
%
% Output parameters:
% imageData - image data in a format acceptable by the imshow function
% If neither target nor imageData were specified, the user will be
% asked to interactively specify the output file.
%
% Examples:
% imageData = screencapture; % interactively select screen-capture rectangle
% imageData = screencapture(hListbox); % capture image of a uicontrol
% imageData = screencapture(0); % capture image of entire screen
% imageData = screencapture(0, [20,30,40,50]); % capture a small desktop region
% imageData = screencapture(gcf,[20,30,40,50]); % capture a small figure region
% imageData = screencapture(gca,[10,20,30,40]); % capture a small axes region
% imshow(imageData); % display the captured image in a matlab figure
% imwrite(imageData,'myImage.png'); % save the captured image to file
% img = imread('cameraman.tif');
% hImg = imshow(img);
% screencapture(hImg,[60,35,140,80]); % capture a region of an image
% screencapture(gcf,[],'myFigure.jpg'); % capture the entire figure into file
% screencapture(gcf,[],'clipboard'); % capture the entire figure into clipboard
% screencapture(gcf,[],'printer'); % print the entire figure
% screencapture('handle',gcf,'target','myFigure.jpg'); % same as previous, save to file
% screencapture('handle',gcf,'target','clipboard'); % same as previous, copy to clipboard
% screencapture('handle',gcf,'target','printer'); % same as previous, send to printer
% screencapture('toolbar',gcf); % adds a screen-capture button to gcf's toolbar
% screencapture('toolbar',[],'target','sc.bmp'); % same with default output filename
%
% Technical description:
% http://UndocumentedMatlab.com/blog/screencapture-utility/
2. 实现流程
- 设置截屏存储的文件路径
为防止文件夹重名截屏结果被覆盖,代码采用系统当前时间为文件名,这样每次截屏的结果存放在不同的文件夹里,不会起冲突
如识别结果不理想,可以随时查看本地图片进行校验。在代码OCR调试成功后,便可将存储文件的代码屏蔽掉,只保留最后的识别结果。 - 设置定时器周期及执行次数
- 截屏,并保存
- OCR识别,并判断
ocr函数使用见MATLAB帮助即可
如果识别结果错误,则将数据置零
3. MATLAB代码
% Author: Shaw
% Description: 定时多次截取桌面指定区域并OCR保存为DATA
% Date: 2021/11/26
close all
clear all
clc
% 全局变量
global i DATA count path file_name
% 以当前时间创建文件夹,用以存储截屏后的图片
file_clock = clock;
file_name = ['data-', num2str(file_clock(1)), '-', num2str(file_clock(2)), '-', num2str(file_clock(3)), '-', ...
num2str(file_clock(4)), '-', num2str(file_clock(5)), '-', num2str(round(file_clock(6)))];
path = 'E:\ScreenCapture\';
mkdir(path,file_name)
% 执行次数计数
i = 0;
% OCR异常计数
count = 0;
% 周期:单位秒
Period = 60;
% 执行次数:单位次
TasksToExecute = 10;
% 定时器设置
t = timer('Period', Period, 'TasksToExecute', TasksToExecute, 'TimerFcn', @tTimerFcn, 'ExecutionMode', 'fixedRate');
t.start
function tTimerFcn(~, ~)
global i DATA count path file_name
i = i + 1
% 确实识别区域
imageData = screencapture(0,[1400,0,15,30]);
% 将图片放大8倍
imagePro = imresize(imageData, 8);
% 保存图片
cd([path,file_name])
imwrite(imagePro,['Image' , num2str(i) , '.png']);
cd(path)
% OCR识别
ocrResults = ocr(imagePro);
% 将识别结果转化为字符串
recognizedText = string(ocrResults.Text);
% 剔除因图像放大而导致的字符串识别结果中的空格
recognizedTextModify = strrep(recognizedText,' ','');
% 判断识别结果
[recognizedNum,tf] = str2num(recognizedTextModify);
if tf
DATA(i) = recognizedNum;
else
count = count + 1;
DATA(i) = 0;
end
end
4. 实现效果
存储的截屏图片为:
绘制数据DATA如下:
结语
第51篇
这部分工作虽然是一个月前做的,但中间停滞到现在并没有做完,后续如有补充则再对该文进行修改。
个人水平有限,有问题欢迎各位大神批评指正!