Reputation: 137
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.
Upvotes: 2
Views: 1127
Reputation: 123
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);
}
}
Upvotes: 1