计算图像在屏幕上出现的次数
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
Mario Picture
这个怎么样,你可以在while-loop之外使用一个变量来存储当前计数的硬币,然后重新运行(读取不同的mario_image)再次计数并比较变量之间是否存在差异if有更新。
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
有很多方法可以做到这一点,
例如,创建一个列表,其中包含要检查匹配的图像[屏幕截图],并将所有代码放入列表项的 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
要创建图像列表,您可以拍摄一些快照并用名称存储它们,或者使用您的代码多次拍摄快照,但您必须在拍摄新快照之前更改桌面图像才能看到任何差异。
您可以使用时间戳定期拍摄快照,以便您有时间更改输入图像。最简单的方法是预先保存屏幕截图,然后像我在上面的代码中看到的那样阅读它们。
此代码获取屏幕截图,然后通过将其与给定模板进行比较,在屏幕上查找给定对象,然后计算找到该对象的次数。这可以在下面的马里奥硬币图片中看到,其中程序将识别每个马里奥硬币,然后计算总共有多少。 我的问题是我希望程序在 运行 时继续计算硬币数,这样如果在屏幕上添加或减去硬币,程序会更新计数。
例如:点数 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
Mario Picture
这个怎么样,你可以在while-loop之外使用一个变量来存储当前计数的硬币,然后重新运行(读取不同的mario_image)再次计数并比较变量之间是否存在差异if有更新。
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
有很多方法可以做到这一点, 例如,创建一个列表,其中包含要检查匹配的图像[屏幕截图],并将所有代码放入列表项的 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
要创建图像列表,您可以拍摄一些快照并用名称存储它们,或者使用您的代码多次拍摄快照,但您必须在拍摄新快照之前更改桌面图像才能看到任何差异。 您可以使用时间戳定期拍摄快照,以便您有时间更改输入图像。最简单的方法是预先保存屏幕截图,然后像我在上面的代码中看到的那样阅读它们。