2

I want to change the hue for specific color on image. I have searched many times but found nothing. Currently I'm using this piece of code i get from SO for changing image hue.

public static class ColorFilterGenerator {
    static ColorFilter adjustHue(float value)
    {
        ColorMatrix cm = new ColorMatrix();

        adjustHues(cm, value);

        return new ColorMatrixColorFilter(cm);
    }

    static void adjustHues(ColorMatrix cm, float value) {
        value = cleanValue(value) / 180f * (float) Math.PI;
        if (value != 0) {
            float cosVal = (float) Math.cos(value);
            float sinVal = (float) Math.sin(value);
            float lumR = 0.213f;
            float lumG = 0.715f;
            float lumB = 0.072f;
            float[] mat = new float[]
                    {
                            lumR + cosVal * (1 - lumR) + sinVal * (-lumR), lumG + cosVal * (-lumG) + sinVal * (-lumG), lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0,
                            lumR + cosVal * (-lumR) + sinVal * (0.143f), lumG + cosVal * (1 - lumG) + sinVal * (0.140f), lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
                            lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)), lumG + cosVal * (-lumG) + sinVal * (lumG), lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0,
                            0f, 0f, 0f, 1f, 0f,
                            0f, 0f, 0f, 0f, 1f};
            cm.postConcat(new ColorMatrix(mat));
        }
    }

    static float cleanValue(float p_val)
    {
        return Math.min((float) 180.0, Math.max(-(float) 180.0, p_val));
    }
}

But this code changes the whole image hue while dragging the seekbar. Can someone please tell me how can i achieve this for a specific color (like only for red) ? Thank you.

ps_who
  • 137
  • 2
  • 9
  • Related: [Programmatically Lighten a Color](https://stackoverflow.com/q/141855/295004) – Morrison Chang Sep 27 '20 at 07:24
  • I didn't get the answer from this – ps_who Sep 27 '20 at 09:12
  • [HSV/HSL is just another representation of color like RGB](https://en.wikipedia.org/wiki/HSL_and_HSV). When you say you want to change a specific color and only that color you do realize you are asking for only selected values of selected pixels in any image array to be modified. Read [the section on end-user software](https://en.wikipedia.org/wiki/HSL_and_HSV#Use_in_end-user_software) and realize that when you say `red` the question becomes `the set of values (shades) of red that need to be transformed`. If your code is too slow look at: Renderscript, OpenGL ES shaders, JNI C/C++ code. – Morrison Chang Sep 27 '20 at 19:37
  • If you give some code examples it would be better for me to understand. – ps_who Sep 28 '20 at 05:44

1 Answers1

1

You could use OpenCV for Android. Then you can read the image in and convert to HSV and then change the hue directly.

Mat image_bgr = Imgcodecs.imread("image_path");
Mat image_hsv = image_bgr.clone();
Imgproc.cvtColor(image_bgr, image_hsv, Imgproc.COLOR_BGR2HSV);

double hue = ...
for (int row = 0; row < image_hsv.rows(); row++) {
    for (int col = 0; col < image_hsv.cols(); col++) {
        double[] pixel = image_hsv.get(row, col);
        pixel[0] += hue;
        image_hsv.put(row, col, pixel);
    }
}
Zac Todd
  • 113
  • 1
  • 11
  • Can you share some example code? Or is it possible to do it without using OpenCV? – ps_who Sep 27 '20 at 06:16
  • Can you show me full working demo code if possible. Because I'm totally beginner in OpenCV and have no idea that how to use it in Android Java. – ps_who Sep 27 '20 at 06:52
  • You will have to follow the instructions in the above link to install OpenCV for android then this should be able to add hue to the image. I don't understand what kind of demo you would want. I would recommend using OpenCV if you are doing a lot of image stuff. – Zac Todd Sep 27 '20 at 07:02