如何在安卓中更改图像的色调、饱和度和亮度。

huangapple go评论81阅读模式
英文:

How to change hue, saturation, and luminance of an image in android

问题

我想要在图像上更改“特定颜色”的“色调(hue)”。我已经搜索了很多次,但什么都没找到。目前我正在使用从SO获取的以下代码片段来更改图像的色调。

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));
    }
}

但是这段代码在拖动滑块时会改变整个图像的色调。请问有人可以告诉我如何仅针对特定颜色(例如仅针对红色)实现这一点吗?谢谢。

英文:

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.

答案1

得分: 1

你可以使用OpenCV for Android。然后你可以读取图像并转换为HSV,然后直接改变色调。

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);
    }
}
英文:

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(&quot;image_path&quot;);
Mat image_hsv = image_bgr.clone();
Imgproc.cvtColor(image_bgr, image_hsv, Imgproc.COLOR_BGR2HSV);
double hue = ...
for (int row = 0; row &lt; image_hsv.rows(); row++) {
for (int col = 0; col &lt; image_hsv.cols(); col++) {
double[] pixel = image_hsv.get(row, col);
pixel[0] += hue;
image_hsv.put(row, col, pixel);
}
}

huangapple
  • 本文由 发表于 2020年9月27日 12:58:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64084953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定