What is the range of HSV values for BROWN color in openCV?

15,641

Solution 1

You can check out my answer here which goes in-depth on how to plot/view a range of different HSV values.

You can also check out external sites which let you play around with values to get an idea of the colorspace. In HSV, browns correspond to higher saturation level (lower values become more gray) and middle value levels (low is black, high is white), and the hue is between (roughly) 20 degrees for a reddish brick color to 40 degrees for a sand color. But in OpenCV, the hue degrees are divided by two to get them to fit under 255, so thats more like 10 to 20 for the hue values.

Using the code in the linked answer, I generated this gif of a range of brown values from [10, 100, 20] to [20, 255, 200] in HSV:

Brown values

This seems to encompass most ideas of brown, but you can play with the values if you want some darker, lighter, grayer, etc ones to appear.

Another way to play around with finding good thresholding values is to create a program that lets you try it out. For example, I built a tool in python and OpenCV similar to the magic wand selector in photoshop, which lets you define a threshold, click a pixel, and it will select pixels of a similar color connected to where you clicked. This tool will print out the highest and lowest color values, and also will print out the mean color and standard deviation for better control of thresholding values similar to the color you selected. You can see a little gif of it being used here.

One more way to play around with values is to create trackbars with OpenCV giving all the minimum and maximum thresholding values, and having the screen update with the thresholded image every time you change a value. I built a tool for this as well in python and OpenCV. You can see a little gif of it being used here.

If you peruse the code for both those projects, you can get an idea of how to build similar programs to toy around with values to get more comfortable working in different colorspaces.

Solution 2

Also, from Wiki: Color coordinates

"HSV (h, s, v) (30°, 100%, 59%)",

so "ideal" Open CV Hue value for BROWN is 15, because Hue for OpenCV 8-bit images is Hue/2, so 30 / 2 = 15. Also 100% for Saturation means 255 (because 255 is max value of Scalar channel in Open CV) and 59% for Value for means (59 / 100) * 255 = 150 for Scalar channel. So, "ideal" BROWN Scalar() is:

Scalar ideal_brown = new Scalar(15,255,150);

To add some "near to BROWN" colored pixels, you can:

1) subtract some value from Hue for "more red" colored pixels;

2) add some value to Hue for "more green" colored pixels;

3) subtract some value from Saturation for "less colored" pixels (you can't add because 255 is already max value);

4) subtract some value from Value for "more darker" pixels;

5) add some value to Value for "more lighter" pixels.

So,

Scalar min_brown = new Scalar(15-<delta1_for_hue>,255-<delta1_for_saturation>,150-<delta1_for_value>);
Scalar max_brown = new Scalar(15+<delta2_for_hue>,255,<delta2_for_saturation>,150+<delta2_for_value>);

Also, take a look at this answer of Miki.

Share:
15,641
Jepoy
Author by

Jepoy

Aspiring computer programming specialist. A student at University of the East - Caloocan, Philippines. Currently learning C++ and game development using Unreal Development Kit(UDK). Loves to play RPG games.

Updated on June 05, 2022

Comments

  • Jepoy
    Jepoy almost 2 years

    I am new to openCV. I am already able to count green and yellow pixels of an image with a mask. I would like to know what is the HSV range of Brown in openCV.

        private void testBrownPixelCount(Mat originalMat) {
        if(isMatEmpty(originalMat)){
            Log.i(TAG, "Empty Original Mat at testBrownPixCoutn()");
        }else{
            //Perform:
            Mat maskMat = Imgcodecs.imread(testFilePath);
            Mat bgr = new Mat();
            Imgproc.cvtColor(maskMat, bgr, Imgproc.COLOR_BGR2RGB);
            Mat maskMatHsv = new Mat();
            Imgproc.cvtColor(bgr, maskMatHsv , Imgproc.COLOR_RGB2HSV);
    
            //iF NOt; use RGB2HSV:
            Mat brownMat = new Mat();
            Scalar min_brown = new Scalar(20,100,100);
            Scalar max_brown = new Scalar(30,255,255);
            Core.inRange(maskMatHsv, min_brown, max_brown, brownMat);
            Log.i(TAG, "Brown Mat Non-zeros:" + Core.countNonZero(brownMat));
    
            Bitmap bitmapMat = Bitmap.createBitmap(brownMat.cols(), brownMat.rows(), Bitmap.Config.ARGB_8888);
            Utils.matToBitmap(brownMat, bitmapMat);
            imgView_testView.setImageBitmap(bitmapMat);
    
        }
    

    Which results to black imageView, it can't even detect the yellow ones.

  • Jepoy
    Jepoy over 6 years
    I've read the link about your in-depth explanation of HSV values and I learned a lot. In fact, I think with that explanation, I can test out HSV values by myself. I really appreciate it and please continue to share knowledge and help people.