UPD:添加了作業 MWE。
我正在嘗試決議游戲中的 HP 數量。我知道影像的寬度并只獲得 HP 條填充部分的寬度的想法。然后只是計算它。
以前它運行良好。但是最近游戲有了一些更新,顏色也發生了變化。我知道。只是一種顏色。
這是我完全作業的 MWE 代碼:您可以嘗試使用帖子末尾附加的源檔案
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])
現在顏色有點紫色。我知道它使用 HSV 顏色,但真的無法弄清楚如何調整它以使其作業。|
作業形象:

不作業的影像:

Source images can be grabbed here:

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348692.html
標籤:python opencv image-processing image-recognition
