This commit is contained in:
HuangHai
2026-01-14 09:56:04 +08:00
parent eed2b6f152
commit ee91bf76d2
14 changed files with 233 additions and 67 deletions

View File

@@ -93,6 +93,64 @@ def save_image(path, img):
logger.error(f"Error saving image {path}: {e}")
return False
def detect_price_click_point_cv(image_path):
"""
使用 HSV 颜色过滤定位详情页的橘红色价格区域,返回最左侧区域的中心点击点
"""
img = read_image(image_path)
if img is None:
return None
h, w = img.shape[:2]
# 1. 转换为 HSV 空间
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# 2. 橘红色的 HSV 范围 (适配特来电价格颜色)
lower_orange = np.array([0, 150, 150])
upper_orange = np.array([20, 255, 255])
mask = cv2.inRange(hsv, lower_orange, upper_orange)
# 3. 对掩码进行膨胀,连接数字
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50, 20))
dilated = cv2.dilate(mask, kernel)
# 4. 寻找轮廓
contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
detected_areas = []
for cnt in contours:
x, y, cw, ch = cv2.boundingRect(cnt)
# 1. 过滤掉宽度过大(可能是横幅广告)或过小(可能是杂点)的区域
# 2. 价格区域 P0 通常在屏幕的中部,且宽度约为屏幕的一半
if 200 < y < h * 0.8 and 100 < cw < w * 0.6 and ch > 30:
detected_areas.append([x, y, x + cw, y + ch])
if not detected_areas:
# 备选:如果 HSV 失败,尝试通过轮廓大小寻找
# 1.1556 这种大数字通常会有很明显的轮廓
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 10))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x, y, cw, ch = cv2.boundingRect(cnt)
if 200 < y < h * 0.6 and 150 < cw < 300 and 50 < ch < 150:
detected_areas.append([x, y, x + cw, y + ch])
if not detected_areas:
return None
# 5. 按 X 轴排序,取最左边的区域 (即用户确认的 P0)
# 但要排除掉可能在最左侧的导航栏返回按钮等小元素,所以前面加了宽度限制
detected_areas.sort(key=lambda b: b[0])
target = detected_areas[0]
center_x = (target[0] + target[2]) // 2
center_y = (target[1] + target[3]) // 2
return [center_x, center_y]
def detect_cards_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
"""
使用计算机图形学 (OpenCV) 检测列表中的场站卡片。