android调整图片的色相,饱和度,灰度

android调整图片的色相,饱和度,灰度
/**
     * 调整图片的色相,饱和度,灰度
     *
     * @param srcBitmap
     * @param rotate
     * @param saturation
     * @param scale
     * @return
     */
    public static Bitmap changeImageTheme(Bitmap srcBitmap, float rotate, float saturation, float scale) {

        //调整色相
        ColorMatrix rotateMatrix = new ColorMatrix();
        rotateMatrix.setRotate(0, rotate);
        rotateMatrix.setRotate(1, rotate);
        rotateMatrix.setRotate(2, rotate);

        //调整色彩饱和度
        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);

        //调整灰度
        ColorMatrix scaleMatrix = new ColorMatrix();
        scaleMatrix.setScale(scale, scale, scale, 1);

        //叠加效果
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.postConcat(rotateMatrix);
        colorMatrix.postConcat(saturationMatrix);
        colorMatrix.postConcat(scaleMatrix);

        //创建一个大小相同的空白Bitmap
        Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        //载入Canvas,Paint
        Canvas canvas = new Canvas(dstBitmap);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        //绘图
        canvas.drawBitmap(srcBitmap, 0, 0, paint);

        return dstBitmap;
    }


    /**
     * 通过更改图片像素点的RGBA值,生成底片效果
     *
     * @param scrBitmap
     * @return
     */
    public static Bitmap beautyImage(Bitmap scrBitmap) {

        int width = scrBitmap.getWidth();
        int height = scrBitmap.getHeight();
        int count = width * height;

        int[] oldPixels = new int[count];
        int[] newPixels = new int[count];

        scrBitmap.getPixels(oldPixels, 0, width, 0, 0, width, height);

        for (int i = 0; i < oldPixels.length; i++) {
            int pixel = oldPixels[i];
            int r = Color.red(pixel);
            int g = Color.green(pixel);
            int b = Color.blue(pixel);

            r = 255 - r;
            g = 255 - g;
            b = 255 - b;

            if (r > 255) {
                r = 255;
            }
            if (g > 255) {
                g = 255;
            }
            if (b > 255) {
                b = 255;
            }

            if (r < 0) {
                r = 0;
            }
            if (g < 0) {
                g = 0;
            }
            if (b < 0) {
                b = 0;
            }

            newPixels[i] = Color.rgb(r, g, b);

        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(newPixels, 0, width, 0, 0, width, height);

        return bitmap;
    }


    // 黑白
    public static final float colormatrix_heibai[] = {0.8f, 1.6f, 0.2f, 0,
            -163.9f, 0.8f, 1.6f, 0.2f, 0, -163.9f, 0.8f, 1.6f, 0.2f, 0,
            -163.9f, 0, 0, 0, 1.0f, 0};
    // 怀旧
    public static final float colormatrix_huaijiu[] = {0.2f, 0.5f, 0.1f, 0,
            40.8f, 0.2f, 0.5f, 0.1f, 0, 40.8f, 0.2f, 0.5f, 0.1f, 0, 40.8f, 0,
            0, 0, 1, 0};
    // 哥特
    public static final float colormatrix_gete[] = {1.9f, -0.3f, -0.2f, 0,
            -87.0f, -0.2f, 1.7f, -0.1f, 0, -87.0f, -0.1f, -0.6f, 2.0f, 0,
            -87.0f, 0, 0, 0, 1.0f, 0};
    // 淡雅
    public static final float colormatrix_danya[] = {0.6f, 0.3f, 0.1f, 0,
            73.3f, 0.2f, 0.7f, 0.1f, 0, 73.3f, 0.2f, 0.3f, 0.4f, 0, 73.3f, 0,
            0, 0, 1.0f, 0};
    // 蓝调
    public static final float colormatrix_landiao[] = {2.1f, -1.4f, 0.6f,
            0.0f, -71.0f, -0.3f, 2.0f, -0.3f, 0.0f, -71.0f, -1.1f, -0.2f, 2.6f,
            0.0f, -71.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
    // 光晕
    public static final float colormatrix_guangyun[] = {0.9f, 0, 0, 0, 64.9f,
            0, 0.9f, 0, 0, 64.9f, 0, 0, 0.9f, 0, 64.9f, 0, 0, 0, 1.0f, 0};

    // 梦幻
    public static final float colormatrix_menghuan[] = {0.8f, 0.3f, 0.1f,
            0.0f, 46.5f, 0.1f, 0.9f, 0.0f, 0.0f, 46.5f, 0.1f, 0.3f, 0.7f, 0.0f,
            46.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
    // 酒红
    public static final float colormatrix_jiuhong[] = {1.2f, 0.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 0.9f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.8f, 0.0f, 0.0f,
            0, 0, 0, 1.0f, 0};
    // 胶片
    public static final float colormatrix_fanse[] = {-1.0f, 0.0f, 0.0f, 0.0f,
            255.0f, 0.0f, -1.0f, 0.0f, 0.0f, 255.0f, 0.0f, 0.0f, -1.0f, 0.0f,
            255.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
    // 湖光掠影
    public static final float colormatrix_huguang[] = {0.8f, 0.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.9f, 0.0f, 0.0f,
            0, 0, 0, 1.0f, 0};
    // 褐片
    public static final float colormatrix_hepian[] = {1.0f, 0.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 0.8f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.8f, 0.0f, 0.0f,
            0, 0, 0, 1.0f, 0};
    // 复古
    public static final float colormatrix_fugu[] = {0.9f, 0.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 0.8f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f,
            0, 0, 0, 1.0f, 0};
    // 泛黄
    public static final float colormatrix_huan_huang[] = {1.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f,
            0.0f, 0, 0, 0, 1.0f, 0};
    // 传统
    public static final float colormatrix_chuan_tong[] = {1.0f, 0.0f, 0.0f, 0,
            -10f, 0.0f, 1.0f, 0.0f, 0, -10f, 0.0f, 0.0f, 1.0f, 0, -10f, 0, 0,
            0, 1, 0};
    // 胶片2
    public static final float colormatrix_jiao_pian[] = {0.71f, 0.2f, 0.0f,
            0.0f, 60.0f, 0.0f, 0.94f, 0.0f, 0.0f, 60.0f, 0.0f, 0.0f, 0.62f,
            0.0f, 60.0f, 0, 0, 0, 1.0f, 0};

    // 锐色
    public static final float colormatrix_ruise[] = {4.8f, -1.0f, -0.1f, 0,
            -388.4f, -0.5f, 4.4f, -0.1f, 0, -388.4f, -0.5f, -1.0f, 5.2f, 0,
            -388.4f, 0, 0, 0, 1.0f, 0};
    // 清宁
    public static final float colormatrix_qingning[] = {0.9f, 0, 0, 0, 0, 0,
            1.1f, 0, 0, 0, 0, 0, 0.9f, 0, 0, 0, 0, 0, 1.0f, 0};
    // 浪漫
    public static final float colormatrix_langman[] = {0.9f, 0, 0, 0, 63.0f,
            0, 0.9f, 0, 0, 63.0f, 0, 0, 0.9f, 0, 63.0f, 0, 0, 0, 1.0f, 0};
    // 夜色
    public static final float colormatrix_yese[] = {1.0f, 0.0f, 0.0f, 0.0f,
            -66.6f, 0.0f, 1.1f, 0.0f, 0.0f, -66.6f, 0.0f, 0.0f, 1.0f, 0.0f,
            -66.6f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: opencv是一个开源的计算机视觉库,可以在Python中使用。通过使用opencv的色相饱和度转换功能,可以实现类似Photoshop中对图像进行色相饱和度调整。 首先,我们需要导入opencv库,并读取一张图片作为输入图像。可以使用cv2.imread()函数来读取图像。 接下来,我们可以通过调用cv2.cvtColor()函数来将图像转换为HSV色彩空间。HSV颜色模型由色调(Hue),饱和度(Saturation)和亮度(Value)组成。我们只需要调整色调和饱和度,所以我们将图像转换为HSV色彩空间。 然后,我们可以通过使用cv2.convertScaleAbs()函数来调整色相饱和度的值。该函数有三个参数,分别是输入图像、输出图像和缩放因子。我们可以将缩放因子设置为一个小数,以调整图像的色相饱和度。 最后,我们可以使用cv2.cvtColor()函数将图像转换回BGR颜色空间,然后可以使用cv2.imshow()函数显示调整后的图像。 下面是一个简单的示例代码: ```python import cv2 # 读取输入图像 image = cv2.imread('image.jpg') # 将图片转换为HSV颜色空间 hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # 调整色相饱和度的值 hue_scale = 0.5 # 色相缩放因子 saturation_scale = 1.5 # 饱和度缩放因子 # 调整色相饱和度的值 hsv_image[:,:,0] = cv2.convertScaleAbs(hsv_image[:,:,0], alpha=hue_scale) hsv_image[:,:,1] = cv2.convertScaleAbs(hsv_image[:,:,1], alpha=saturation_scale) # 将图片转换回BGR颜色空间 new_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR) # 显示调整后的图像 cv2.imshow('Adjusted Image', new_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这个示例代码中,我们将色相缩放因子设为0.5,饱和度缩放因子设为1.5。你可以根据自己的需要调整这两个值来得到不同的效果。调整后的图像会在一个新的窗口中显示出来。 这样,我们就可以通过opencv和Python来实现简单的色相饱和度调整,类似于Photoshop中的功能。 ### 回答2: 要使用OpenCV和Python实现Photoshop中的色相饱和度功能,可以按照以下步骤进行: 1. 导入必要的库:使用`import cv2`导入OpenCV库。 2. 加载图像:使用`cv2.imread()`函数加载要处理的图像。 3. 转换颜色空间:将加载的图像转换为HSV颜色空间,以便可以对色相饱和度进行调整。使用`cv2.cvtColor()`函数将图像从BGR颜色空间转换为HSV颜色空间。 4. 调整色相饱和度:使用`cv2.cvtColor()`函数的第三个参数来调整色相饱和度。该参数的取值范围为[-180, 180],其中负值表示减少色相饱和度,正值表示增加色相饱和度。 5. 转换颜色空间:将调整后的图像转换回BGR颜色空间,以便显示或保存。使用`cv2.cvtColor()`函数将图像从HSV颜色空间转换为BGR颜色空间。 6. 显示或保存结果:使用`cv2.imshow()`函数显示调整后的图像,并使用`cv2.waitKey()`函数等待用户按下键盘上的任意键。或使用`cv2.imwrite()`函数将调整后的图像保存到文件中。 7. 释放资源:使用`cv2.destroyAllWindows()`函数释放窗口和图像资源。 以下是一个示例代码来实现上述步骤: ```python import cv2 # 加载图像 image = cv2.imread('input.jpg') # 转换颜色空间为HSV hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # 调整色相饱和度 hsv_image[..., 0] += 30 # 增加色相 hsv_image[..., 1] *= 1.5 # 增加饱和度 # 转换颜色空间为BGR adjusted_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR) # 显示调整后的图像 cv2.imshow('Adjusted Image', adjusted_image) cv2.waitKey(0) # 保存调整后的图像 cv2.imwrite('output.jpg', adjusted_image) # 释放资源 cv2.destroyAllWindows() ``` 这段示例代码实现了将输入图像的色相增加30度,饱和度增加1.5倍的效果。您可以根据实际需求调整这两个参数来实现不同的色相饱和度调整效果。 ### 回答3: 要使用OpenCV在Python中实现Photoshop的色相饱和度功能,你需要按照以下步骤进行操作: 1. 导入所需的库和模块: ```python import cv2 import numpy as np ``` 2. 读取图像: ```python image = cv2.imread("your_image.jpg") ``` 3. 将图像转换为HSV颜色空间: ```python hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) ``` 4. 调整色相饱和度参数: ```python hue_shift = 30 # 色相偏移量(取值范围:-180到180) saturation_factor = 1.5 # 饱和度增强因子(取值范围:0到正无穷) hsv_image[:, :, 0] = (hsv_image[:, :, 0] + hue_shift) % 180 hsv_image[:, :, 1] = hsv_image[:, :, 1] * saturation_factor ``` 5. 将修改后的图像转换回BGR颜色空间: ```python result_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR) ``` 6. 显示和保存结果图像: ```python cv2.imshow("Result Image", result_image) cv2.imwrite("result_image.jpg", result_image) cv2.waitKey(0) ``` 通过以上步骤,你可以使用OpenCV和Python实现类似Photoshop的色相饱和度功能。根据需要,你可以调整色相饱和度的参数来得到不同的效果。记得根据自己的实际情况修改读取和保存图像的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值