Color Thresholding
Checking if there is a color present in a segment of an image
Resources
WPLib's Page on Color Thresholding - Must Read!
Wizard.exe's Color Thresholding Tutorial - Solves the ultimate goal computer vision task.
RoboForce's Color Thresholding Tutorial - Detects yellow poles in FTC game Power Play.
Most vision tasks in FTC ask you to identify the position of an object that is randomly placed in one of three areas before the match starts.
A straightforward way to solve this task is to scan the image your camera has returned for the object. Since we know that the object will be in one of three predetermined locations, we can scan the corresponding segments in the image for the color of the object.
The region which contains the most pixels that match the color of the object is the correct region, making our pipeline return the object. Upon receiving the location of the object from the pipeline, you can tell your opMode to perform the corresponding action, for example, parking in a specific area of the field.
OpenCV
In order to turn a colored image, such as the one captured by your camera, into a binary image, with the target as the “foreground”, we need to threshold the image using the hue, saturation, and value of each pixel.
This model is known as the HSV model of representing an image.
WPLib DocumentationUnlike RGB, HSV allows you to not only filter based on the colors of the pixels but also on the intensity of color and the brightness.
Hue: Measures the color of the pixel.
Saturation: Measures the intensity of the color of the pixel.
Value: Measures the brightness of the pixel.
Because HSV takes account of brightness it can help mitigate changes in lighting making it superior to thresholding with the default BGR model.
Luckily, OpenCV allows you to convert between image models easily:
From there we define the regions that should be scanned:
We also have to define the actual threshold values for the color. We do this by establishing a lower bound and a higher bound. OpenCV has a built-in library function that returns a modified image that highlights the pixels that contain HSV values in between the established bounds.
Finally, we search through each of the pixels in the defined regions, the region with the most amount of white pixels is the correct location of the object.
Putting it all together we get the following pipeline:
Vuforia
The process of determining object location is very similar to that of OpenCV. However, there are a couple of key differences:
We do not convert from RGB to HSV, instead thresholding the image using RGB bounds.
We do not convert the image to binary format and count the white pixels as there is no built-in thresholding function.
Instead, we check we calculate the average RGB values of each rectangle region, and compare them to the threshold value.
Here is an example of a thresholding for the orange rings in ultimate goal.
Our logic was to pick two spots on the ring stack, one spot on the first ring, and one spot on the fourth ring. If both spots have orange, there are four rings, if only one spot has orange, there is one ring, if neither of the spots has orange, there are no rings. The Calculate Average RGB function is taking in the section of the mat to analyze and returns the average RGB of the region. The amount of blue is analyzed to determine if the color of the rings is present or not.
Last updated