【问题标题】:Bitmap threshold quicker位图阈值更快
【发布时间】:2013-01-30 17:22:35
【问题描述】:

我正在尝试编写一个方法,该方法将采用 Bitmap 并将其强制为严格的黑白图像(没有灰色阴影)。

我首先将位图传递给使用colormatrix 使其灰度化的方法:

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

效果又好又快..

然后我将它传递给另一种方法,将灰度图像强制为 2 色图像(黑白),这种方法有效,但显然它会遍历每个像素并且需要很长时间:

public Bitmap toStrictBlackWhite(Bitmap bmp){
        Bitmap imageOut = bmp;
        int tempColorRed;
        for(int y=0; y<bmp.getHeight(); y++){
            for(int x=0; x<bmp.getWidth(); x++){
                tempColorRed = Color.red(imageOut.getPixel(x,y));
                Log.v(TAG, "COLOR: "+tempColorRed);

                if(imageOut.getPixel(x,y) < 127){
                    imageOut.setPixel(x, y, 0xffffff);
                }
                else{
                    imageOut.setPixel(x, y, 0x000000);
                }               
            }
        } 
        return imageOut;
    }

有人知道更快更有效的方法吗?

【问题讨论】:

  • 除了下面 kcoppock 的回答(这会快得多),如果您使用图像中值,而不是常数 &lt; 127,通常会获得更好的质量阈值。这是一种流行的方法,因为它几乎适用于任何图像,无论明暗比如何,并且几乎在任何情况下都能提供非常清晰的图像。有一个链接here 显示了一种方法,以及一个额外的阈值噪声步骤,可以进一步清理图像。整个过程对我来说效果很好,而且速度也很快。

标签: android


【解决方案1】:

您是否尝试过将其转换为字节数组(参见答案here)?

而且,当我对此进行调查时,the Android reference for developers about Bitmap processing 也可能对您有所帮助。

【讨论】:

    【解决方案2】:

    不要使用getPixel()setPixel()

    使用getPixels() 将返回所有像素的多维数组。在此阵列上进行本地操作,然后使用setPixels() 设置修改后的阵列。这将明显更快。

    【讨论】:

      猜你喜欢
      • 2013-03-11
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      相关资源
      最近更新 更多