In this post, I’m going to show you how to detect a red‑colored spot in an image using JavaCV (OpenCV) and HSV color space thresholding – a building block for many object tracking applications.
Instead of struggling with RGB values that change drastically under different lighting, we switch to the HSV color model. Hue (H) represents the pure pigment, while Saturation and Value are separated. That makes color filtering far more robust. Red is special: its hue wraps around 0°, so the full range is 0–10° and 160–180°. In this example we target only the upper range (160–180) for bright red.
Java Source Code:
//imports
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
public class ColorDetect {