HSV values in openCV

asked 2019-07-28 06:09:36 -0500

LiorA gravatar image

updated 2019-07-29 13:08:44 -0500

berak gravatar image


I've tried to isolate yellow-orange-ish color from an image, using inRange function.

I've used an online color picker (https://pinetools.com/image-color-picker)

and than tried to isolate by the values it gave me - even with half of Hue value.

but it failed.

The Hue value that the picker gave me is 30 (tried half - also incorrect) The true value is around 120 - I know this because I've started to 'play' with the value, until I get the desired result.

for future reference, What is wrong here ? The online picker ?

code :

public void colorMask(View view)
    //Image Input :
    Bitmap one =
            drawableToBitmap(getResources().getDrawable(R.drawable.dsc_1250, this.getTheme()));
    Mat img1 = new Mat();
    Utils.bitmapToMat(one, img1, true);// moving one to img1 Mat structure.
    //one.recycle(); //Undefined behaviour ..

    // downsize the image.
    Mat pyrDown = new Mat();
    Imgproc.resize(img1, pyrDown, new Size(img1.cols() / 4, img1.rows() / 4));

    Mat hsvImg = new Mat();
    cvtColor(pyrDown, hsvImg, COLOR_BGR2HSV);

    Mat yellowMask = new Mat();
    Mat greenMask = new Mat();

    inRange(hsvImg, new Scalar(100, 41, 40), new Scalar(130, 255, 255), yellowMask ); // try

    Mat res = new Mat();
    Mat NotYellowMask= new Mat();
    bitwise_not(greenMask, NotYellowMask);
    pyrDown.copyTo(res, yellowMask );

    Bitmap imageMatched =Bitmap.createBitmap(res.cols(), res.rows(), Bitmap.Config.RGB_565);
    Utils.matToBitmap(res, imageMatched);

edit retag flag offensive close merge delete


A piece of code could be helpful. How do you convert rgb image to hsv?

Witek gravatar imageWitek ( 2019-07-28 15:33:36 -0500 )edit

yes I did.. full code : https://pastebin.com/raw/RVB2GYGR

LiorA gravatar imageLiorA ( 2019-07-29 03:52:44 -0500 )edit

edited.. The true value is still a mystery for me.. around 120~150

LiorA gravatar imageLiorA ( 2019-07-29 12:55:50 -0500 )edit

your Mat (from Utils.matToBitmap) is probably RGBA, not BGR, thus you're looking at the wrong channel ordering ?

try COLOR_RGB2HSV instead.

(just saying, it might be a java/android specific problem)

berak gravatar imageberak ( 2019-07-29 13:01:40 -0500 )edit

my code is working.. I just dont understand why .. because the Hue values are allegedly incorrect ..

LiorA gravatar imageLiorA ( 2019-07-30 11:11:15 -0500 )edit

try with COLOR_RGB2HSV is it helping?

Witek gravatar imageWitek ( 2019-07-31 15:30:00 -0500 )edit

@berak@Witek You both are correct. Thank's !

LiorA gravatar imageLiorA ( 2019-08-02 07:50:56 -0500 )edit