Tix
Tix

Reputation: 449

OpenCV: Finding the upper and lower bounds for a color HSV

The following code reads the pixel in the center and returned three values which i assumed was H = data[0], S data[1], V = data[2], how do I get the upper and lower bounds HSV value?

Note: The color pixel I'm reading is Green.

E/data: H:90.0 S:113.0 V:144.0

if (getIntent().hasExtra("byteArray")) {

            bitmap = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"), 0, getIntent().getByteArrayExtra("byteArray").length);

            int width= bitmap.getWidth();
            int height=bitmap.getHeight();

            int centerX=width/2;
            int centerY=height/2;
            srcMat = new Mat();
            Utils.bitmapToMat(bitmap, srcMat);
            Imgproc.cvtColor(srcMat, srcMat, Imgproc.COLOR_BGR2HSV);
            srcMat.convertTo(srcMat, CvType.CV_64FC3); //http://answers.opencv.org/question/14961/using-get-and-put-to-access-pixel-values-in-java/
            double[] data = srcMat.get(centerX, centerY);
            Log.e("data", String.valueOf("H:"+data[0]+" S:"+data[1]+" V:"+data[2]));
            Log.e("dlength", String.valueOf(data.length));
            Mat matHSV = new Mat(0,0,CvType.CV_64FC3);

Also by adding the following three lines of code, i'll receive an error saying bitmap == null, so im not really sure if the pixel reading worked or not.

matHSV.put(0,0,data);
Utils.matToBitmap(matHSV, bb);
imgDisplay.setImageBitmap(bb);

Image I'm Reading: enter image description here

Upvotes: 0

Views: 1469

Answers (1)

Andrii Omelchenko
Andrii Omelchenko

Reputation: 13343

Just convert Mat to HSV model with Imgproc.cvtColor() method:

Imgproc.cvtColor(hsvMat, hsvMat, Imgproc.COLOR_RGB2HSV);

Than find min/max Hue value over each pixel (with Mat.get(int,int) method) - and thats min and max is will be the answer: lower and upper bounds for a color in HSV model.

NB! Hue for OpenCV 8-bit images is Hue/2 because Hue value in "normal" model is between 0 and 360, and grater than 255 - max value for byte. So if You need color bounds for "normal" HSV model (not HSV for Android OpenCV), You shuld multiply it by 2.

Upvotes: 1

Related Questions