OpenCV和Python-无法检测到蓝色物体

Eam*_*orr 3 python opencv image-processing

我在看这个问题:

如何使用opencv检测蓝色物体

然而,经过反复尝试,我仍然不知道如何检测蓝色物体。

这是我的代码:

import cv2
import numpy as np

cam=cv2.VideoCapture(0)
n=0

while True:
    print n
    returnVal,frame=cam.read()

    img=cv2.GaussianBlur(frame, (5,5), 0)
    img=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    blue_lower=np.array([150,150,0],np.uint8)
    blue_upper=np.array([180,255,255],np.uint8)
    blue=cv2.inRange(img,blue_lower,blue_upper)

    cv2.imshow('img',blue)

    n=n+1
    key = cv2.waitKey(10) % 0x100
    if key == 27: break #ESC 
Run Code Online (Sandbox Code Playgroud)

我可以通过设置以下几行来检测红色物体:

red_lower=np.array([0,150,0],np.uint8)
red_upper=np.array([10,255,255],np.uint8)
Run Code Online (Sandbox Code Playgroud)

当我使用第一个代码在网络摄像头前放一张蓝色纸时,它只是显示为黑色。

有人可以帮我将蓝色的RGB转换为HSV吗?

提前谢谢了,

w-m*_*w-m 6

蓝色在HSV中以360之外的240度左右的色调表示。OpenCV-HSV中的色相范围为0-180,将值存储在8位中。因此,蓝色在OpenCV-HSV中表示为H周围的值240 / 2 = 120

为了正确检测蓝色,可以选择以下值:

blue_lower=np.array([100,150,0],np.uint8)
blue_upper=np.array([140,255,255],np.uint8)
Run Code Online (Sandbox Code Playgroud)