Python OpenCv解析进度条

Python OpenCv parse progress bar

UPD:添加了工作 MWE。

我正在尝试解析游戏中的 HP 量。我知道图像的宽度并且只得到 HP 条的填充部分的宽度的想法。然后计算一下。

以前效果很好。但最近游戏得到了一些更新,颜色发生了变化。我知道。只是一个颜色.

这是我完全有效的 MWE 代码:您可以尝试使用附在 post[=39= 末尾的源文件 ]

import cv2
import numpy as np


def parse_hp(hp_area):
    width = int(hp_area.shape[1] * 5)
    height = int(hp_area.shape[0] * 5)
    dim = (width, height)

    # resize image
    resized = cv2.resize(hp_area, dim, interpolation=cv2.INTER_AREA)
    # Color segmentation
    hsv = cv2.cvtColor(resized, cv2.COLOR_BGR2HSV)
    lower_red = np.array([0, 50, 50])
    upper_red = np.array([5, 255, 255])
    mask = cv2.inRange(hsv, lower_red, upper_red)
    res = cv2.bitwise_and(resized, resized, mask=mask)

    # Contour exctraction
    imgray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(imgray, (5, 5), 0)
    ret, thresholded = cv2.threshold(blurred, 50, 255, 0)
    contours, h = cv2.findContours(thresholded, 1, 2)

    if contours:
        cnt = contours[0]
        approx = cv2.approxPolyDP(cnt, 0.01 * cv2.arcLength(cnt, True), True)
        if cv2.contourArea(cnt) > 25:  # to discard noise from the color segmentation
            contour_poly = cv2.approxPolyDP(cnt, 3, True)
            center, radius = cv2.minEnclosingCircle(contour_poly)

            cv2.circle(resized, (int(center[0]), int(center[1])), int(radius), (0, 255, 0), 2)
            cv2.imshow("Found limits", resized)
            cv2.waitKey(0)

            resized_width = int(resized.shape[1])
            hp_width = radius * 2

            return int(hp_width * 100 / resized_width)
    else:
        return -1


if __name__ == "__main__":
    hp_area = cv2.imread("/Users/vetalll/Documents/Cv2Working.png")
    result = parse_hp(hp_area)
    print(result)

我尝试使用这些值。但它不起作用。 openCv 无法识别它们:

lower_red = np.array([355, 44, 45])
upper_red = np.array([356, 41, 43])

现在颜色有点purple.I知道它使用 HSV 颜色但真的不知道如何调整它以使其工作。 |

工作图像:

图片无效:

源图片可以在这里抓取: https://drive.google.com/file/d/1dJ4ePw_7oJov_OU5n6IO6fwdm_N3W5k2/view?usp=sharing

经过一番猜测,我得出了这些值。希望他们工作:

import cv2
import numpy as np


def parse_hp(hp_area):
    width = int(hp_area.shape[1] * 5)
    height = int(hp_area.shape[0] * 5)
    dim = (width, height)

    # resize image
    resized = cv2.resize(hp_area, dim, interpolation=cv2.INTER_AREA)
    # Color segmentation
    hsv = cv2.cvtColor(resized, cv2.COLOR_RGB2HSV)
    lower_red = np.array([120, 170, 0])
    upper_red = np.array([245, 255, 255])
    mask = cv2.inRange(hsv, lower_red, upper_red)
    res = cv2.bitwise_and(resized, resized, mask=mask)

    # Contour exctraction
    imgray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(imgray, (5, 5), 0)
    ret, thresholded = cv2.threshold(blurred, 50, 255, 0)
    contours, h = cv2.findContours(thresholded, 1, 2)

    if contours:
        cnt = contours[0]
        approx = cv2.approxPolyDP(cnt, 0.01 * cv2.arcLength(cnt, True), True)
        if cv2.contourArea(cnt) > 25:  # to discard noise from the color segmentation
            contour_poly = cv2.approxPolyDP(cnt, 3, True)
            center, radius = cv2.minEnclosingCircle(contour_poly)

            cv2.circle(resized, (int(center[0]), int(center[1])), int(radius), (0, 255, 0), 2)
            cv2.imshow("Found limits", resized)
            cv2.waitKey(0)

            resized_width = int(resized.shape[1])
            hp_width = radius * 2

            return int(hp_width * 100 / resized_width)
    else:
        return -1


if __name__ == "__main__":
    hp_area = cv2.imread("Cv2NotWorking.png")
    result = parse_hp(hp_area)
    print(result)