【问题标题】:Converting color bitmap to grayscale image using only the blue channel using a colormatrix使用颜色矩阵仅使用蓝色通道将彩色位图转换为灰度图像
【发布时间】:2014-11-06 09:01:47
【问题描述】:

我需要在 Android 中仅使用蓝色通道对位图进行灰度化。 我设法通过使用颜色矩阵摆脱了绿色和红色通道,但是当我将该矩阵的饱和度设置为 0 时,它会忽略之前对红色和绿色通道所做的更改。

有没有办法完成我的任务?遍历整个像素数组不是一种选择,因为它太慢了。

我正在使用这段代码:

public Bitmap ConvertToGrayscale(Bitmap bitmap) {
    int height = super.getHeight();
    int width = super.getWidth();

    float[] arrayForColorMatrix = new float[] {0, 0, 0, 0, 0,
                                               0, 0, 0, 0, 0,
                                               0, 0, 1, 0, 0,
                                               0, 0, 0, 1, 0};

    Bitmap.Config config = bitmap.getConfig();
    Bitmap grayScaleBitmap = Bitmap.createBitmap(width, height, config);

    Canvas c = new Canvas(grayScaleBitmap);
    Paint paint = new Paint();

    ColorMatrix matrix = new ColorMatrix(arrayForColorMatrix);
    matrix.setSaturation(0);

    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
    paint.setColorFilter(filter);

    c.drawBitmap(bitmap, 0, 0, paint);

    return grayScaleBitmap;
}

【问题讨论】:

    标签: android bitmap grayscale colormatrix


    【解决方案1】:

    从蓝色通道获取灰度图像的正确矩阵如下:

    float[] arrayForColorMatrix = new float[] {0, 0, 1, 0, 0,
                                               0, 0, 1, 0, 0,
                                               0, 0, 1, 0, 0,
                                               0, 0, 0, 1, 0};
    

    From the Android documentation:

    当应用于颜色 [r, g, b, a] 时,结果颜色计算为(钳位后):

    R' = a*R + b*G + c*B + d*A + e;
    G' = f*R + g*G + h*B + i*A + j;
    B' = k*R + l*G + m*B + n*A + o;
    A' = p*R + q*G + r*B + s*A + t;
    

    使用我的arrayForColorMatrix,我得到了 RGB 颜色分量的每个值的蓝色值,这会产生基于蓝色通道的灰度位图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-24
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      相关资源
      最近更新 更多