You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenCV图像蓝色检测出现误识别,如何实现精准检测?

精准检测图像中蓝色的调整方案

1. 修正代码基础错误

  • 导入语句拆分:原代码import os import numpy as np需拆分为两行,且遗漏cv2导入
  • 路径转义处理:Windows路径中的\需转义为\\,或使用原始字符串避免解析错误
  • bitwise_and参数修正:两个输入需为原图像(而非HSV图像),否则输出显示异常

修正后的基础代码片段:

import os
import numpy as np
import cv2

img_path = os.path.join(r"G:\oak", "window", "1.jpg")  # 用原始字符串避免转义问题
img = cv2.imread(img_path)
if img is None:
    print("图像读取失败,请检查路径")
    exit()

resized_img = cv2.resize(img, (int(img.shape[1]/2), int(img.shape[0]/2)))
hsv_image = cv2.cvtColor(resized_img, cv2.COLOR_BGR2HSV)

2. 优化HSV蓝色阈值

原阈值中饱和度(S)和亮度(V)下限过低,会误识别低饱和度的灰色、暗褐色区域。建议根据实际场景调整:

  • 标准亮蓝色参考范围:low_blue = np.array([90, 50, 50]),high_blue = np.array([130, 255, 255])
  • 若需检测深蓝/暗蓝色,可适当降低V下限(但不低于30),同时保持S下限在40以上

不确定阈值时,用交互式工具实时调整:

def nothing(x):
    pass

cv2.namedWindow("Trackbars")
cv2.createTrackbar("Hue Min", "Trackbars", 90, 179, nothing)
cv2.createTrackbar("Hue Max", "Trackbars", 130, 179, nothing)
cv2.createTrackbar("Sat Min", "Trackbars", 50, 255, nothing)
cv2.createTrackbar("Sat Max", "Trackbars", 255, 255, nothing)
cv2.createTrackbar("Val Min", "Trackbars", 50, 255, nothing)
cv2.createTrackbar("Val Max", "Trackbars", 255, 255, nothing)

while True:
    h_min = cv2.getTrackbarPos("Hue Min", "Trackbars")
    h_max = cv2.getTrackbarPos("Hue Max", "Trackbars")
    s_min = cv2.getTrackbarPos("Sat Min", "Trackbars")
    s_max = cv2.getTrackbarPos("Sat Max", "Trackbars")
    v_min = cv2.getTrackbarPos("Val Min", "Trackbars")
    v_max = cv2.getTrackbarPos("Val Max", "Trackbars")
    
    lower = np.array([h_min, s_min, v_min])
    upper = np.array([h_max, s_max, v_max])
    mask = cv2.inRange(hsv_image, lower, upper)
    result = cv2.bitwise_and(resized_img, resized_img, mask=mask)
    
    cv2.imshow("Mask", mask)
    cv2.imshow("Result", result)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

拖动滑块直到误识别区域消失,记录对应HSV数值替换原代码阈值。

3. 形态学操作去除噪点

即使阈值合适,仍可能存在小噪点,用腐蚀+膨胀过滤:

# 创建3x3结构元素
kernel = np.ones((3,3), np.uint8)
# 先腐蚀去噪,再膨胀恢复目标区域形态
blue_mask = cv2.erode(blue_mask, kernel, iterations=1)
blue_mask = cv2.dilate(blue_mask, kernel, iterations=1)

完整修正代码

import os
import numpy as np
import cv2

img_path = os.path.join(r"G:\oak", "window", "1.jpg")
img = cv2.imread(img_path)
if img is None:
    print("图像读取失败,请检查路径是否正确")
    exit()

resized_img = cv2.resize(img, (int(img.shape[1]/2), int(img.shape[0]/2)))
hsv_image = cv2.cvtColor(resized_img, cv2.COLOR_BGR2HSV)

# 调整后的蓝色HSV阈值(可通过交互式工具优化)
low_blue = np.array([90, 50, 50])
high_blue = np.array([130, 255, 255])

blue_mask = cv2.inRange(hsv_image, low_blue, high_blue)

# 形态学去噪
kernel = np.ones((3,3), np.uint8)
blue_mask = cv2.erode(blue_mask, kernel, iterations=1)
blue_mask = cv2.dilate(blue_mask, kernel, iterations=1)

# 正确提取蓝色区域
blue = cv2.bitwise_and(resized_img, resized_img, mask=blue_mask)

cv2.imshow("Original", resized_img)
cv2.imshow("Blue Detection", blue)
cv2.waitKey(0)
cv2.destroyAllWindows()

内容的提问来源于stack exchange,提问作者niloofar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 22:32:56