【问题标题】:Android : Converting color image to grayscale [duplicate]Android:将彩色图像转换为灰度[重复]
【发布时间】:2011-12-05 05:54:42
【问题描述】:

我正在尝试使用红色、绿色、蓝色的平均值将彩色图像转换为灰度。但它会出现错误。

这是我的代码

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

但是当我在模拟器上运行我的应用程序时,它会强制关闭。有什么想法吗?

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

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

【问题讨论】:

  • 发布错误日志的错误是什么。
  • 您是否在日志中收到任何确切的错误?比如stackoverflow?

标签: android grayscale


【解决方案1】:

你也可以这样做:

    ColorMatrix matrix = new ColorMatrix();
    matrix.setSaturation(0); 
    imageview.setColorFilter(new ColorMatrixColorFilter(matrix));

【讨论】:

  • 是否可以让它只有全黑(0xff000000)和全白(0xffffffff)?
  • @androiddeveloper 您可以使用 ImageView 的内置 setColorFilter(color, mode) 方法或常规 PorterDuffColorFilter 来实现。 developer.android.com/reference/android/graphics/…
  • @Pkmmte 常规的 PorterDuffColorFilter 是什么?
  • 这对我不起作用。我正在使用 imageView.setImageBitmap() 在 imageview 中设置图像。
  • 这种方法比把图片转Bitmap更可靠。
【解决方案2】:

试试解决方案from this previous answer by 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;
    }

【讨论】:

  • 如何使图像变亮?图像现在是灰色的,但由于原始图像中的颜色,它也很暗。
  • 所以在将其转换为黑白后图像尺寸会减小吗? (大小以 kb 为单位)
  • Lalit 你能告诉我如何从灰度图像中提取字符吗?
  • 我想要 dat codez,请你回答我的问题!
  • 说真的,伙计们,通常最好提出一个新问题,而不是评论旧帖子。
【解决方案3】:

Lalit 给出了最实用的答案。 然而,你希望得到的灰色是红色、绿色和蓝色的平均值,应该像这样设置你的矩阵:

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

最后,正如我之前遇到过将图像转换为灰度的问题 - 在所有情况下,视觉上最令人愉悦的结果是通过不取平均值来实现的,而是通过根据感知的亮度为每种颜色赋予不同的权重,我倾向于使用这些值:

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

【讨论】:

  • @user1324936 需要详细说明吗?
猜你喜欢
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多