文章目录

  • 0. 前言
  • 1. screencapture下载
  • 2. 实现流程
  • 3. MATLAB代码
  • 4. 实现效果
  • 结语

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. 实现流程

  1. 设置截屏存储的文件路径
    为防止文件夹重名截屏结果被覆盖,代码采用系统当前时间为文件名,这样每次截屏的结果存放在不同的文件夹里,不会起冲突
    如识别结果不理想,可以随时查看本地图片进行校验。在代码OCR调试成功后,便可将存储文件的代码屏蔽掉,只保留最后的识别结果。
  2. 设置定时器周期及执行次数
  3. 截屏,并保存
  4. OCR识别,并判断
    ocr函数使用见MATLAB帮助即可
    如果识别结果错误,则将数据置零

3. MATLAB代码

% Author: Shaw
% Description: 定时多次截取桌面指定区域并OCR保存为DATA
% Date: 2021/11/26close 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.startfunction tTimerFcn(~, ~)global i DATA count path file_namei = 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 tfDATA(i) = recognizedNum;elsecount = count + 1;DATA(i) = 0;endend

4. 实现效果

存储的截屏图片为:

绘制数据DATA如下:

结语

第51篇

这部分工作虽然是一个月前做的,但中间停滞到现在并没有做完,后续如有补充则再对该文进行修改。

个人水平有限,有问题欢迎各位大神批评指正!

MATLAB 定时截取桌面固定区域并OCR相关推荐

  1. html局部可复制,截取网页局部区域css样式的方法和系统的制作方法

    截取网页局部区域css样式的方法和系统的制作方法 [技术领域] [0001]本发明涉及计算机网络技术领域,特别是涉及一种截取网页局部区域CSS样式的方法和系统. [背景技术] [0002]CSS(Ca ...

  2. 高德地图-2D地图下区域遮掩(只显示固定区域里的内容)

    最近遇到一个新的需求需用用到高德地图 公司需要只显示固定区域范围的地图,其余地方的地图都用透明遮罩覆盖 完成后如下图所示: 地图体验网址 刚开始的时候研究了半天高德地图的的JS API中只有一个区域遮 ...

  3. python图片截取斜四边形_opencv 截取任意四边形区域的图像

    截取任意四边形区域的图像. mask就是结果. 需要定义四边形区域,分别是 tl, tr, bl, br std::map> generateBorders(const std::vector& ...

  4. php 只打印某个区域,PHP打印代码页面固定区域

    在使用PHP软件的朋友们肯定碰上过没有打印代码的情况!PHP打印代码页面固定区域让您在这里直接输入代码就能解决问题啦!想要继续打印工作的话您可以使用狂飙php最新版和PHP开发课安卓版中进行了解!PH ...

  5. MATLAB视频截取和缩放

    MATLAB视频截取和缩放 1 按帧截取 2 按时间截取 3 视频缩放 4 总体代码 利用Matlab进行视频处理时,经常需要做的是对视频进行截取,这里截取的方式有两种:按时间截取和按帧截取.截取之后 ...

  6. css3 text-overflow制作固定区域的博客列表

    <!DOCTYPE HTML> <html lang="en-US"><head><meta charset="UTF-8&qu ...

  7. sqlserver数据库,使用substring函数截取不固定位置字符串。

    sqlserver数据库,使用substring函数截取不固定位置字符串. 当我们在向页面写入数据库查询出来的数据的时候,有一些不必要的字符串,相信大家肯定会在后台的java代码中进行处理再返回到页面 ...

  8. Python桌面自定义---实现定时更换桌面壁纸

    Python桌面自定义---实现定时更换桌面壁纸 1 效果 2 获取大量壁纸 3 Python代码实现定时更换壁纸 1 效果   大致效果如下,设置过定时更换壁纸的应该都知道是啥场景. 2 获取大量壁 ...

  9. python定时换桌面壁纸

    使用Python从本地文件夹中直接调取图片,自动定时更换桌面壁纸,于是试了一试,效果贼棒! import random import ctypes import time import os path ...

最新文章

  1. 推荐百度地图的新功能--“三维”
  2. 5.16GW光伏扶贫,各省费用如何筹措?
  3. 画出该lti系统的幅频特性响应曲线_一文带你通俗理解幅频响应和相频响应
  4. IJCAI 2021 | 中科院计算所:自监督增强的知识蒸馏方法
  5. fork()使用(一)
  6. ios 桥接文件找不到文件_电脑文件搜索神器,没有找不到的东西
  7. 枚举求解:试把一个正整数n拆分为若干个(不少于2个)连续正整数之和。例如:n=15,有3种拆分:15=1+2+3+4+5,15=4+5+6,15=7+8。 对于给定的正整数n,求出所有符合这种拆分要求
  8. 特征经验分享以及管理文件,远程运行的小技巧
  9. android 应用搬家 分区,把安装在SD卡的应用存在DATA分区的数据移到SD卡上
  10. 【优化求解】基于matlab差分进化算法求解函数极值问题【含Matlab源码 1199期】
  11. VB 共享软件防破解设计技术初探(二)
  12. 【文献学习】DeepReceiver: A Deep Learning-Based Intelligent Receiver for Wireless Communications in the Ph
  13. 基于RFID定位技术的智能仓储管理系统--RFID智能仓储--新导智能
  14. 打开 IBM Rational Rose Enterprise Edition 报错的处理
  15. 英语中提醒注意安全句子
  16. php cbd架构,ThinkPHP教程--15--CBD模式
  17. 你以为接下所有需求就能俘获产品MM的心?带她去浪才是你要做的!
  18. Writeup For WeChall
  19. 王晓昀-PowerDesigner与模型驱动开发-UMLChina讲座-音频和幻灯
  20. 马云:等阿里IPO时你就知道我们赚多少了

热门文章

  1. 机器学习中Gradient Descent (Vanilla)梯度下降法的过程
  2. 计算机网络——码元常见编码方式
  3. 远程桌面控制工具(向日葵)下载安装
  4. python 破解字体反爬 (一)
  5. C# 简单实现轮盘抽签软件程序(范本)
  6. python怎么读1003python怎么读_python怎么读,python是什么意思
  7. 朋友圈美食“小心机”拍摄技巧
  8. 企业架构:简单分析流程工业的数字化转型
  9. [java小代码收集] 取出对像的类名称过滤掉包名
  10. 计算机毕业设计SSM党建系统【附源码数据库】