I want to detect all red color rimmed traffic signs (Triangular and Circular). The algorithm has to be efficient and robust to work in real world situations, so i decided to use HSV space since it is light invariant.

I came across this [question](https://stackoverflow.com/questions/32522989/opencv-better-detection-of-red-color) of detecting red objects and the answer was to use this value ranges for HSV: The code is in C++:

```
inRange(hsv, Scalar(0, 70, 50), Scalar(10, 255, 255), mask1);
inRange(hsv, Scalar(170, 70, 50), Scalar(180, 255, 255), mask2);

Mat1b mask = mask1 | mask2;

```

Since I am using Java's OpenCV I tried [that](https://stackoverflow.com/questions/50105075/how-to-perform-a-union-operation-in-java-opencv?noredirect=1#comment87230105_50105075), but I found out it is not possible to do a Bitwise `OR` Operation.

So I tried to implement it manually instead of using OpenCV. I also tried same red color value ranges that is provided and sadly the results were horrible:

**Here is my code**

```
Mat hsv = new Mat();
Mat rgb = Highgui.imread(scene, Highgui.CV_LOAD_IMAGE_COLOR);
Imgproc.cvtColor(rgb, hsv, Imgproc.COLOR_RGB2HSV);
Mat thresh = new Mat(hsv.size(), CvType.CV_8UC1);

for(int x=0;x<hsv.rows();x++){
    for(int y=0;y<hsv.cols();y++)
    {
        double[] data = hsv.get(x,y);

        double H = data[0];
        double S = data[1];
        double V = data[2];
        if((( 0.0>=H && H<=10.0) && (70.0>=S && S<=255.0) && (50.0>=V && V<=255.0)) || (( 170.0>=H && H<=180.0) && (70.0>=S && S<=255.0) && (50.0>=V && V<=255.0)) ) {

            thresh.put(x,y, 255);
        }
        else
        {
            thresh.put(x,y, 0);
        }
    }
}

```

**Here is the results before and after thresholding**

[![Original](https://i.stack.imgur.com/m06y4.jpg)](https://i.stack.imgur.com/m06y4.jpg)

[![Threshold](https://i.stack.imgur.com/iVMX3.png)](https://i.stack.imgur.com/iVMX3.png)

Can someone provide me with right values?