0

red and blue works fine, what happened to GREEN. i've read the similar question and another one,still doesn't work. see my picture frame,mask,res

import cv2 as cv
import numpy as np
cap = cv.VideoCapture(0)
while(1):
    _, frame = cap.read()
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])
    lower_green = np.array([45,100,20])
    upper_green = np.array([75,255,255])
    lower_red = np.array([0,100,100])
    upper_red = np.array([10,255,255])
    mask1 = cv.inRange(hsv, lower_blue, upper_blue)
    mask2 = cv.inRange(hsv, lower_green, upper_green)
    mask3 = cv.inRange(hsv, lower_red, upper_red)
    res = cv.bitwise_and(frame,frame, mask= mask1+mask2+mask3)
    cv.imshow('frame',frame)
    cv.imshow('mask',mask1+mask2+mask3)
    cv.imshow('res',res)
    k = cv.waitKey(5) & 0xFF
    if k == 27:
        break
cv.destroyAllWindows()
Joesse Random
  • 55
  • 1
  • 9
  • Your mask might not be able to represent the Green color which you are trying to track. It seems like your Hue values for lower and upper green are actually redish colors. – Meto Jul 14 '20 at 08:43
  • i run `green=np.uint8([[[0,255,0]]]) hsv_green=cv.cvtColor(green,cv.COLOR_BGR2HSV) print(hsv_green)`,result is `[[[ 60 255 255]]]`, so i set lower and upper hue 45(60-15),75(60+15), could you tell me what's wrong with this?@Meto – Joesse Random Jul 14 '20 at 08:46
  • I really don't know why cvtColor() produces an output like that. If we think hue like a circle Red falls between 0 and 60 degrees, Yellow falls between 61 and 120 degrees, Green falls between 121 and 180 degrees. And if we scale it to opencv's value range, lower green should be 85. – Meto Jul 14 '20 at 09:13
  • lower green[85,100,20] upper green[105,255,255] works! why 85? i can't figure it out@Meto – Joesse Random Jul 14 '20 at 09:23
  • Take a look at here https://programmingdesignsystems.com/color/color-models-and-color-spaces/index.html . There is a 3D HSV model which can help you better grasp the concept. – Meto Jul 14 '20 at 09:35
  • If my comment solved your problem, could you mark this question as solved? – Meto Jul 16 '20 at 10:27
  • yeah you did solve it thanks! but seems like i can only mark an answer as accepted not a comment? @Meto maybe you could write it as an answer so i can accept it – Joesse Random Jul 16 '20 at 12:10

1 Answers1

0

In HSV color space, Hue represents the traditional colors which we perceive. Another main difference is that when RGB color space represented as a cube, HSV is a cylinder so the range of Hue is actually 0 to 360 degrees. Hue represents Green values between ~121 to ~180 degrees and when we rescale that to the input range of Opencv functions (0-255) the value of green should be between 85 to 128. If you are looking for a visual representation this page has a nice interactive model for both RGB and HSV color spaces.

Meto
  • 638
  • 7
  • 18