how can i track GREEN color in openCV? while red&blue worked
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.
Choosing the correct upper and lower HSV boundaries for color detection with`cv::inRange` (OpenCV)
Problem 1 : Different applications use different scales for HSV. For example gimp uses H = 0-360, S = 0-100 and V = 0-100
. But OpenCV uses H: 0-179, S: 0-255, V: 0-255
. Here i got a hue value of 22 in gimp. So I took half of it, 11, and defined range for that. ie (5,50,50) - (15,255,255)
.
Problem 2: And also, OpenCV uses BGR format, not RGB. So change your code which converts RGB to HSV as follows:
cv.CvtColor(frame, frameHSV, cv.CV_BGR2HSV)
Now run it. I got an output as follows:
Hope that is what you wanted. There are some false detections, but they are small, so you can choose biggest contour which is your lid.
EDIT:
As Karl Philip told in his comment, it would be good to add new code. But there is change of only a single line. So, I would like to add the same code implemented in new cv2
module, so users can compare the easiness and flexibility of new cv2
module.
import cv2
import numpy as np
img = cv2.imread('sof.jpg')
ORANGE_MIN = np.array([5, 50, 50],np.uint8)
ORANGE_MAX = np.array([15, 255, 255],np.uint8)
hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
frame_threshed = cv2.inRange(hsv_img, ORANGE_MIN, ORANGE_MAX)
cv2.imwrite('output2.jpg', frame_threshed)
It gives the same result as above. But code is much more simpler.
OpenCV Android Green Color Detection
Green color is HSV space has H = 120 and it's in range [0, 360].
OpenCV halves the H values to fit the range [0,255], so H value instead of being in range [0, 360], is in range [0, 180].
S and V are still in range [0, 255].
As a consequence, the value of H for green is 60 = 120 / 2.
You upper and lower bound should be:
// sensitivity is a int, typically set to 15 - 20
[60 - sensitivity, 100, 100]
[60 + sensitivity, 255, 255]
UPDATE
Since your image is quite dark, you need to use a lower bound for V. With these values:
sensitivity = 15;
[60 - sensitivity, 100, 50] // lower bound
[60 + sensitivity, 255, 255] // upper bound
the resulting mask would be like:
You can refer to this answer for the details.
Related Topics
How to Validate Ip Address in Python
What Is Different Between Makedirs and Mkdir of Os
How to Make a Cross-Module Variable
How to Make Ball Bounce Off Wall with Pygame
Plotting a 3D Cube, a Sphere and a Vector in Matplotlib
Function Not Changing Global Variable
Logging Uncaught Exceptions in Python
Appending the Same String to a List of Strings in Python
Detect Socket Hangup Without Sending or Receiving
How to Set Explicitly the Terminal Size When Using Pexpect
Python and Regular Expression with Unicode
In Matplotlib, What Does the Argument Mean in Fig.Add_Subplot(111)
Replace Values in List Using Python
In Practice, What Are the Main Uses for the "Yield From" Syntax in Python 3.3