使用OpenCV和Python-2.7的屏幕截图 [英] Screen Capture with OpenCV and Python-2.7

查看:589
本文介绍了使用OpenCV和Python-2.7的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 2.7 OpenCV 2.4.9 .

我需要捕获显示给用户的当前帧,并将其作为Python中的 cv :: Mat 对象加载.

I need to capture the current frame that is being shown to the user and load it as an cv::Mat object in Python.

你们知道递归的快速方法吗?

Do you guys know a fast way to do it recursively?

我需要类似以下示例中的操作,以递归方式从网络摄像头捕获 Mat 帧:

I need something like what's done in the example below, that captures Mat frames from a webcam recursively:

import cv2

cap = cv2.VideoCapture(0)
while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow('WindowName', frame)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break

在该示例中,它使用了 VideoCapture类来处理从网络摄像头捕获的图像.

In the example it's used the VideoCapture class to work with the captured image from the webcam.

借助VideoCapture.read(),始终可以读取新帧并将其存储到 Mat 对象中.

With VideoCapture.read() a new frame is always being readed and stored into a Mat object.

我可以将"printscreens流" 加载到VideoCapture对象中吗?是否可以使用Python中的OpenCV在计算机屏幕上创建流,而不必每秒保存和删除许多 .bmp 文件?

Could I load a "printscreens stream" into a VideoCapture object? Could I create a streaming of my computer's screen with OpenCV in Python, without having to save and delete lots of .bmp files per second?

我需要将此框架用作 Mat 对象或 NumPy数组 ,以便执行某些计算机视觉带有这些帧的例程.

I need this frames to be Mat objects or NumPy arrays, so I can perform some Computer Vision routines with this frames in real time.

推荐答案

这是我使用@Raoul技巧编写的解决方案代码.

That's a solution code I've written using @Raoul tips.

我使用PIL ImageGrab模块抓取打印屏幕框架.

I used PIL ImageGrab module to grab the printscreen frames.

import numpy as np
from PIL import ImageGrab
import cv2

while(True):
    printscreen_pil =  ImageGrab.grab()
    printscreen_numpy =   np.array(printscreen_pil.getdata(),dtype='uint8')\
    .reshape((printscreen_pil.size[1],printscreen_pil.size[0],3)) 
    cv2.imshow('window',printscreen_numpy)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

这篇关于使用OpenCV和Python-2.7的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆