Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV & Python -- Can't detect blue objects

I was looking at this question:

How to detect blue color object using opencv

Yet after much trial and error, I still can't figure out how to detect blue objects.

Here is my code:

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 

I can detect red objects by setting the following lines:

red_lower=np.array([0,150,0],np.uint8)
red_upper=np.array([10,255,255],np.uint8)

When I put a blue piece of paper in front of my webcam using the first code, it just shows up black.

Can someone please help me to convert RGB for blue colours into HSV?

Many thanks in advance,

like image 757
Eamorr Avatar asked Sep 11 '25 08:09

Eamorr


1 Answers

Blue is represented in HSV at a hue of around 240 degrees out of 360. The Hue range in OpenCV-HSV is 0-180, to store the value in 8 bits. Thus, blue is represented in OpenCV-HSV as a value of H around 240 / 2 = 120.

To detect blue correctly, the following values could be chosen:

blue_lower=np.array([100,150,0],np.uint8)
blue_upper=np.array([140,255,255],np.uint8)
like image 56
w-m Avatar answered Sep 13 '25 00:09

w-m