从MSS细分屏幕截图

问题描述

我正在尝试捕捉数独难题。目前可以捕获页面并打印第一个数字,但是我想将图像拆分成9x9的较小版本,然后传递给PyTesseract进行单独检查。 PyTesseract当前正在检查屏幕截图中是否有数字,并显示数字或0。我在确定如何根据图像大小的宽度和高度拆分numpy数组时遇到问题。

import time
import cv2
import mss
import numpy
import mouse
try:
    from PIL import Image
except ImportError:
    import Image
import PyTesseract
import re

x=0
y=0
w = 300
h = 300

with mss.mss() as sct:
    # Part of the screen to capture
    while "Screen capturing":
        # Press "q" to quit
        if cv2.waitKey(25) & 0xFF == ord("q"):
            cv2.destroyAllWindows()
            break
        x,y=mouse.get_position()[0],mouse.get_position()[1]
        monitor = {"top": y,"left": x,"width": w,"height": h}
        # Get raw pixels from the screen,save it to a Numpy array
        img = numpy.array(sct.grab(monitor),dtype=numpy.uint8)
        # I want to split it into 9x9 columns like a sudoku and send them to PyTesseract.
                
        im = numpy.flip(img[:,:,:3],2)  # BGRA -> RGB conversion
        text = PyTesseract.image_to_string(im,config='--psm 6')
        #print(text)
            number = re.search('\d+',text)
        if number:
            print('First number found = {}'.format(number.group()))
        else:
            print('0')
        cv2.imshow("OpenCV/Numpy normal",img)
        time.sleep(2)

在逻辑上是这样的,但是我不知道如何将该数组转换为可以使用的格式。

for i in range(9):
    for j in range(9):
        h1=int((h/9)*i)
        h2=int((h/9)*(i+1))
        w1=int((w/9)*j)
        w2=int((w/9)*(j+1))
        img2=img[h1:h2][w1:w2]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)