簡體   English   中英

計算圖像在屏幕上顯示的次數

[英]Counting how many times an image appears on screen

此代碼截取屏幕截圖,然后在屏幕上查找給定對象,將其與給定的模板進行比較,然后計算找到對象的次數。 這可以在下面看到馬里奧硬幣圖片,其中程序將識別每個馬里奧硬幣,然后計算總數。 我的問題是我希望程序在運行時繼續計算硬幣,這樣如果在屏幕上添加或減去硬幣,程序就會更新計數。

例如:計數19個硬幣,計數19個硬幣,計數19個硬幣,(增加兩個硬幣),計數21個硬幣,計數21個硬幣等。

import cv2 as cv2
import numpy
import pyautogui

# Takes a screen shot and saves the file in the specified location
loc1 = (r'Capture.png')
pyautogui.screenshot(loc1)

# Reads the screen shot and loads the image it will be compared too
img_rgb = cv2.imread(loc1)
count = 0
n = 0

while n < 5:
    # Reads the file
    template_file_ore = r"mario.png"
    template_ore = cv2.imread(template_file_ore)
    w, h = template_ore.shape[:-1]

    # Compares screen shot to given image, gives error thresh hold
    res = cv2.matchTemplate(img_rgb, template_ore, cv2.TM_CCOEFF_NORMED)
    threshold = 0.80
    loc = numpy.where(res >= threshold)

    # Puts red box around matched images and counts coins
    for pt in zip(*loc[::-1]):
        cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
        count = count + 1

        print(count)
    n = n + 1

馬里奧圖片

怎么樣,你可以使用存儲當前計數硬幣的while循環外的變量,然后再次重新運行(讀取不同的mario_image)計數,並比較變量是否存在變量之間的差異。

currently_counted_coins =0 #init
...
#after for loop
difference = count-currently_counted_coins # if difference <0 coins removed else added
#update
currently_counted_coins += difference # to keep a total number of coins  

我最終搞清楚只需要在“for”循環中重新運行整個代碼,如下所示。

    import cv2 as cv2
    import numpy
    import pyautogui

    # Takes a screen shot and saves the file in the specified location
    loc1 = (r'Capture.png')
    pyautogui.screenshot(loc1)

    # Reads the screen shot and loads the image it will be compared too
    img_rgb = cv2.imread(loc1)
    count = 0
    n = 0

    while n < 20:
        # Reads the file
        template_file_ore = r"mario.png"
        template_ore = cv2.imread(template_file_ore)
        w, h = template_ore.shape[:-1]

        # Compares screen shot to given image, gives error thresh hold
        res = cv2.matchTemplate(img_rgb, template_ore, cv2.TM_CCOEFF_NORMED)
        threshold = 0.80
        loc = numpy.where(res >= threshold)

        # Puts red box around matched images and counts coins
        for pt in zip(*loc[::-1]):
            loc1 = (r'Capture.png')
            pyautogui.screenshot(loc1)

         # Reads the file
            template_file_ore = r"mario.png"
            template_ore = cv2.imread(template_file_ore)
            w, h = template_ore.shape[:-1]

         # Compares screen shot to given image, gives error thresh hold
            res = cv2.matchTemplate(img_rgb, template_ore, cv2.TM_CCOEFF_NORMED)
            threshold = 0.80
            loc = numpy.where(res >= threshold)

          # Reads the screen shot and loads the image it will be compared too
            img_rgb = cv2.imread(loc1)
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
            count = count + 1

            print(count)
        n = n + 1

有很多方法可以做到這一點,例如創建一個包含圖像的列表[screenshots],你要檢查匹配,並將所有代碼放在for循環迭代列表項中。

list_images = ['image1.png','image2.png',..]

for img in list_images:
  # here put your code 
  img_to_be_checked = cv2.imread(img)
  # continue with your code in the while loop

要創建圖像列表,您可以拍攝一些快照並使用名稱存儲它們,或使用您的代碼多次拍攝快照,但在拍攝新快照之前必須更改桌面圖像以查看任何差異。 您可以使用時間戳定期拍攝快照,以便您有時間更改輸入圖像。 最簡單的方法是預先保存屏幕截圖,然后在上面的代碼中看到你的時候閱讀它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM