简体   繁体   中英

Python OpenCv parse progress bar

UPD: Added working MWE.

I am trying to parse the amount of HP iт the game. The idea that I know the width of image and just get the width of filled part of the HP bar. And then just calculate it.

Previously it worked well. But recently game got some update and the color is changed. I know. Just a color .

Here is my fully worked MWE code : You can try it with sourcr files attached in the end of the post

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)

I tried to use these values. But it dos not work. openCv does not recognize them:

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

And now the color is a little bit purple.I know that it uses HSV color but really not able to figure aout how to adjust it to make it work. |

Working image: 在职的

Not working image: 不工作

Source images can be grabbed here: https://drive.google.com/file/d/1dJ4ePw_7oJov_OU5n6IO6fwdm_N3W5k2/view?usp=sharing

After a bit of guessing, I came up with these values. Hope they work:

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM