简体   繁体   中英

how can i track GREEN color in openCV? while red&blue worked

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM