使用OpenCV更快的图像?

发布于 2025-01-24 17:42:21 字数 2258 浏览 0 评论 0原文

我正在从事一个可以帮助我学习python的项目。我想创建一个可以自动化迷你游戏的程序。迷你游戏上方有一个指针,条上有三种颜色(绿色,黄色,红色)。游戏的目的是在指针在绿色或黄色部分上方时单击左鼠标。我在Gitlab上找到了一些代码,但这太慢了,所以我想知道是否有人可以给我提示它可以更快地运行。这是代码:

import numpy as np
import cv2
import mouse
import mss
import time

DEBUG = True

detection_threshold = 0.8
monitor = {'top': 0, 'left': 0, 'width': 2560, 'height': 1440}
target_offset_ver = 75  # Pixels
target_offset_hor = 10  # Pixels
target_color = [20, 138, 38]  # Green bar (BGR)

leaf_icon = cv2.imread("Pointer.bmp")
leaf_icon_depth, leaf_icon_width, leaf_icon_height = leaf_icon.shape[::-1]

with mss.mss() as sct:

    while True:

        # Get screen capture.
        img = np.array(sct.grab(monitor))
        img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)

        # Find the leaf icon on screen.
        res = cv2.matchTemplate(img, leaf_icon, cv2.TM_CCOEFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

        # If a leaf is detected.
        if max_val > detection_threshold:

            # Check if the leaf is above the green/yellow bar.
            if np.all(img[max_loc[1] + target_offset_ver, max_loc[0] + target_offset_hor] == target_color):
                print("Leaf is above green bar.")
                mouse.click("left")
            else:
                print("Leaf not in correct position.")

            if DEBUG:
                img[max_loc[1] + target_offset_ver, max_loc[0] + target_offset_hor] = [0, 255, 255]
                cv2.circle(img, (max_loc[0] + target_offset_hor, max_loc[1] + target_offset_ver), 2, 255, 1)

        else:
            print("No leaf detected.")

        if DEBUG:
            # Add a rectangle around the leaf on the image for debug purposes.
            top_left = max_loc
            bottom_right = (top_left[0] + leaf_icon_width, top_left[1] + leaf_icon_height)
            cv2.rectangle(img, top_left, bottom_right, 255, 2)
            cv2.imshow("derp", img)

        # Exit on 'q' press.
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

鼠标点击似乎有点延迟,并且错过了大约1/5的时间。我认为问题的一部分是来自res = cv2.matchtemplate(img,leaw_icon,cv2.tm_ccoeff_normed),大约需要0.25秒才能完成。这是因为我要捕获所有监视器(2560x1440)吗?我还能做些什么来使抓取图像和单击鼠标较小之间的延迟?

I am working on a project that would help me learn python. I wanted to create a program that would automate a minigame. The minigame have a pointer above a bar and the bar will have three colors in it (green, yellow, red). The point of the game is to click left mouse when the pointer is above the green or yellow section. I found some code on gitlab for this but it is a little too slow and so I was wondering if anyone could give me tips on getting it to run faster. Here's the code:

import numpy as np
import cv2
import mouse
import mss
import time

DEBUG = True

detection_threshold = 0.8
monitor = {'top': 0, 'left': 0, 'width': 2560, 'height': 1440}
target_offset_ver = 75  # Pixels
target_offset_hor = 10  # Pixels
target_color = [20, 138, 38]  # Green bar (BGR)

leaf_icon = cv2.imread("Pointer.bmp")
leaf_icon_depth, leaf_icon_width, leaf_icon_height = leaf_icon.shape[::-1]

with mss.mss() as sct:

    while True:

        # Get screen capture.
        img = np.array(sct.grab(monitor))
        img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)

        # Find the leaf icon on screen.
        res = cv2.matchTemplate(img, leaf_icon, cv2.TM_CCOEFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

        # If a leaf is detected.
        if max_val > detection_threshold:

            # Check if the leaf is above the green/yellow bar.
            if np.all(img[max_loc[1] + target_offset_ver, max_loc[0] + target_offset_hor] == target_color):
                print("Leaf is above green bar.")
                mouse.click("left")
            else:
                print("Leaf not in correct position.")

            if DEBUG:
                img[max_loc[1] + target_offset_ver, max_loc[0] + target_offset_hor] = [0, 255, 255]
                cv2.circle(img, (max_loc[0] + target_offset_hor, max_loc[1] + target_offset_ver), 2, 255, 1)

        else:
            print("No leaf detected.")

        if DEBUG:
            # Add a rectangle around the leaf on the image for debug purposes.
            top_left = max_loc
            bottom_right = (top_left[0] + leaf_icon_width, top_left[1] + leaf_icon_height)
            cv2.rectangle(img, top_left, bottom_right, 255, 2)
            cv2.imshow("derp", img)

        # Exit on 'q' press.
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

The mouse click seems to be a bit delayed and misses about 1/5 of the time. I think part of the issue is from res = cv2.matchTemplate(img, leaf_icon, cv2.TM_CCOEFF_NORMED) which takes about 0.25 seconds to complete. Is this because I'm capturing all of the monitor (2560x1440)? Is there anything else I could do to make the delay between grabbing the image and clicking the mouse smaller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文