Madalin
Madalin

Reputation: 385

Convert RGB to HSV in android

I want to get the ARGB values from a pixel and convert to HSV and then set the pixel with the new values.

I don't fully understand how to do that. Can anyone help me?

Upvotes: 5

Views: 10171

Answers (3)

Swapnil Naukudkar
Swapnil Naukudkar

Reputation: 626

convert RGBA to HSV with OpenCV:

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

 private Scalar converScalarRgba2HSV(Scalar rgba) {
    Mat  pointMatHsv= new Mat();
    Mat pointMatRgba = new Mat(1, 1, CvType.CV_8UC3, rgba);
    Imgproc.cvtColor(pointMatRgba,pointMatHsv, Imgproc.COLOR_RGB2HSV_FULL, 4);

    return new Scalar(pointMatHsv.get(0, 0));
}

Upvotes: 0

naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37993

Refer to Android documentation: public static void Color#RGBToHSV(int, int, int, float[])

This method converts RGB components to HSV and returns hsv - 3 element array which holds the resulting HSV components. hsv[0] is Hue [0 .. 360), hsv[1] is Saturation [0...1], hsv[2] is Value [0...1].

Upvotes: 4

samgak
samgak

Reputation: 24427

Let's say you have a Bitmap object and x and y co-ordinates. You can get the color from the bitmap as a 32-bit value like this:

int color = bitmap.getPixel(x, y);

You can separate out the argb components like this:

int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);

Then you can convert to HSV like this:

float[] hsv = new float[3];
Color.RGBToHSV(r, g, b, hsv);

Now you can manipulate the HSV values however you want. When you are done you can convert back to rgb:

color = Color.HSVToRGB(hsv);

or like this is you want to use the alpha value:

color = Color.HSVToRGB(a, hsv);

Then you can write the color back to the bitmap (it has to be a mutable Bitmap):

bitmap.setPixel(x, y, color);

Upvotes: 7

Related Questions