5

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?

helb
  • 7,609
  • 8
  • 36
  • 58
Madalin
  • 385
  • 1
  • 5
  • 18

3 Answers3

7

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);
samgak
  • 23,944
  • 4
  • 60
  • 82
  • But I got this error : "The method alpha(int) is undefined for the type Color" how can I fix this? – hklel Aug 07 '15 at 18:13
  • @AlexLing make sure you imported the Color class correctly: import android.graphics.Color; – samgak Aug 07 '15 at 21:22
  • @samgak thx for your reply. Yeah I didn't import it correctly cuz I am using pure Java, not android. But do you know how can I do this in pure Java? ( I am not an android developer and I need this in a desktop application) Thanks. – hklel Aug 08 '15 at 01:57
  • 1
    @AlexLing use the RGBtoHSB function of the Java AWT Color class: http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html – samgak Aug 08 '15 at 03:34
4

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].

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
0

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));
}
Alex K.
  • 842
  • 7
  • 17
  • Could you please specify what are Scalar and Mat? What is Imgproc? And how do you get a Scalar type from a pixel in a Bitmap to pass as argument? Thanks! – Federico Alvarez Jul 11 '19 at 14:16