电脑视觉VS屏幕截图?

时间:2019-02-01 23:42:15

标签: python opencv

我试图精确地测量一个白球的定时围绕轮缘旋转。 测量是在浏览器窗口中的实时流上完成的(不是已记录/保存的视频文件)。

到目前为止,我一直在使用屏幕截图方法。 那是;

  1. 屏幕快照是在希望球通过的监视器的指定像素位置拍摄的。

  2. 屏幕截图的颜色通道是平均的

  3. 如果屏幕截图的平均颜色大于预先计算的平均颜色=>白球已进入图片。

代码:

# Screenshot Color Function:

def average_image_color(filename):                        
    i = Image.open(filename)
    h = i.histogram()

    # split into red, green, blue
    r = h[0:256]
    g = h[256:256*2]
    b = h[256*2: 256*3]

    # perform the weighted average of each channel:
    # the *index* is the channel value, and the *value* is its weight
    return (
        sum( i*w for i, w in enumerate(r) ) / sum(r),
        sum( i*w for i, w in enumerate(g) ) / sum(g),
        sum( i*w for i, w in enumerate(b) ) / sum(b)
    )

if __name__ == '__main__':
    import sys
    if len(sys.argv) > 1:
        1#print (average_image_color(sys.argv[1]))
    #else:
        #print ('usage: average_image_color.py FILENAME')

#---Calibration

avc = average_image_color('SSC' + str(i)+ '.png')

tavc = sum(avc) #sum all colors because we are trying to detect the WHITE ball, so all colors will increase when ball is in pic

cx[i] = tavc

calibration = sum(cx.values())/len(cx.values())    #BALL CALIBRATION

 #---------BALL---SCREENSHOT------FOR-CALCULATIONS----------


            with mss.mss() as sct:      #BALL SCREENSHOT
                        monitorb #= {"top": 860, "left": 2316, "width": 80, "height": 25}
                        output = 'SS' + str(count)+'.png'.format(**monitorb)

                        # Grab the data
                        im = sct.grab(monitorb)

                        # Save to the picture file
                        mss.tools.to_png(im.rgb, im.size, output=output)

#average color
            avc = average_image_color('SS' + str(count)+ '.png')
            tavc = sum(avc)
            count += 1



 #--------BALL----VELOCITY-------------------------

            d = float(12.75*float(math.pi)*2)
            r = 12.75
            if tavc > calibration + 5 and START == True and tavc < calibration*2:                        
                start = float(time.time())
                START = False

                time.sleep(0.03)


            elif tavc > calibration + 5 and START == False and tavc < calibration*2:                                             
                end = float(time.time())

                t = float(end) - float(start)
                start = end

                v = d/t
                tdiff0 = time.time()


                tx[count0] = t
                vx[count0] = v

                #print('\nVelocity: ' + str(v))
                #print(str(v))
                count0 += 1

                time.sleep(0.03)

这个方法效果好。 它设法每约0.04秒拍摄一次屏幕截图。

但是问题是:

  1. 有时球太快并且滑过而没有被屏幕截图捕获。

  2. 有时球太慢..并导致重复计算,这就是为什么我添加time.sleep(0.03),但这样做增加所遇到的问题的可能性在1

  3. 校准颜色以使球和无球之间的差异可通过代码轻松区分是很痛苦的。颜色会改变所有时间造成的环境照明等。我必须不断重新校准。

所以我不知道我是否应该尝试使用OpenCV的代替或试图使这项工作?我从来没有使用OpenCV的,所以我不知道这需要多长时间,或者是值得的。 谢谢

PS:对代码显示不正确表示抱歉,不确定如何解决

0 个答案:

没有答案