Android:将彩色图像转换为灰度[重复]

时间:2022-01-14 09:00:11

This question already has an answer here:

这个问题在这里已有答案:

Hi guys I need your help, I'm trying to convert color image into grayscale using the average of red, green, blue. But it comes out with errors,

嗨伙计们,我需要你的帮助,我正在尝试使用红色,绿色,蓝色的平均值将彩色图像转换为灰度。但它出现了错误,

Here is my code

这是我的代码

imgWidth = myBitmap.getWidth();
imgHeight = myBitmap.getHeight();

for(int i =0;i<imgWidth;i++) {
    for(int j=0;j<imgHeight;j++) {
     int s = myBitmap.getPixel(i, j)/3;
     myBitmap.setPixel(i, j, s);
    }
}

ImageView img = (ImageView)findViewById(R.id.image1);
img.setImageBitmap(myBitmap);

But when I run my application on Emulator, it's force close. Any idea?

但是当我在模拟器上运行我的应用程序时,它会强行关闭。任何想法?

I have solved my problem use the following code:

我已经用以下代码解决了我的问题:

for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get one pixel color
                pixel = src.getPixel(x, y);
                // retrieve color of all channels
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // take conversion up to one single value
                R = G = B = (int)(0.299 * R + 0.587 * G + 0.114 * B);
                // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }

3 个解决方案

#1


68  

You can do this too :

你也可以这样做 :

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

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

#2


30  

Try the solution from this previous answer by leparlon:

尝试leparlon之前的回答中的解决方案:

public Bitmap toGrayscale(Bitmap bmpOriginal)
    {        
        int width, height;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();    

        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
    }

#3


12  

Lalit has the most practical answer. However, you wanted the resulting grey to be the average of the red, green and blue and should set up your matrix like so:

拉利特有最实际的答案。但是,您希望生成的灰色是红色,绿色和蓝色的平均值,并且应该像这样设置矩阵:

    float oneThird = 1/3f;
    float[] mat = new float[]{
            oneThird, oneThird, oneThird, 0, 0, 
            oneThird, oneThird, oneThird, 0, 0, 
            oneThird, oneThird, oneThird, 0, 0, 
            0, 0, 0, 1, 0,};
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(mat);
    paint.setColorFilter(filter);
    c.drawBitmap(original, 0, 0, paint);

And finally, as I have faced the problem of converting an image to grayscale before - the most visually pleasing result in all cases is achieved by not taking the average, but through giving each colour different weight depending on its percieved brightness, I tend to use these values:

最后,由于我面临着之前将图像转换为灰度的问题 - 所有情况下最令人满意的结果都是通过不采用平均值来实现的,但是通过根据每个颜色不同的亮度给出不同的重量,我倾向于使用这些价值观:

    float[] mat = new float[]{
            0.3f, 0.59f, 0.11f, 0, 0, 
            0.3f, 0.59f, 0.11f, 0, 0, 
            0.3f, 0.59f, 0.11f, 0, 0, 
            0, 0, 0, 1, 0,};

#1


68  

You can do this too :

你也可以这样做 :

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

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

#2


30  

Try the solution from this previous answer by leparlon:

尝试leparlon之前的回答中的解决方案:

public Bitmap toGrayscale(Bitmap bmpOriginal)
    {        
        int width, height;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();    

        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
    }

#3


12  

Lalit has the most practical answer. However, you wanted the resulting grey to be the average of the red, green and blue and should set up your matrix like so:

拉利特有最实际的答案。但是,您希望生成的灰色是红色,绿色和蓝色的平均值,并且应该像这样设置矩阵:

    float oneThird = 1/3f;
    float[] mat = new float[]{
            oneThird, oneThird, oneThird, 0, 0, 
            oneThird, oneThird, oneThird, 0, 0, 
            oneThird, oneThird, oneThird, 0, 0, 
            0, 0, 0, 1, 0,};
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(mat);
    paint.setColorFilter(filter);
    c.drawBitmap(original, 0, 0, paint);

And finally, as I have faced the problem of converting an image to grayscale before - the most visually pleasing result in all cases is achieved by not taking the average, but through giving each colour different weight depending on its percieved brightness, I tend to use these values:

最后,由于我面临着之前将图像转换为灰度的问题 - 所有情况下最令人满意的结果都是通过不采用平均值来实现的,但是通过根据每个颜色不同的亮度给出不同的重量,我倾向于使用这些价值观:

    float[] mat = new float[]{
            0.3f, 0.59f, 0.11f, 0, 0, 
            0.3f, 0.59f, 0.11f, 0, 0, 
            0.3f, 0.59f, 0.11f, 0, 0, 
            0, 0, 0, 1, 0,};