【问题标题】:Android: Convert Grayscale to Binary ImageAndroid:将灰度转换为二进制图像
【发布时间】:2013-12-16 10:44:14
【问题描述】:

我已经完成了获取灰度值,但我不知道如何使用函数将灰度转换为二进制图像。请帮帮我,这是我的功能代码:

public Bitmap toBinary(Bitmap bmpOriginal) {
    int width, height, threshold;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();
    threshold = 127;
    final Bitmap bmpBinary = null;

    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get one pixel color
            int pixel = bmpOriginal.getPixel(x, y);

            //get grayscale value
            int gray = (int)(pixel & 0xFF);

            //get binary value
            if(gray < threshold){
                bmpBinary.setPixel(x, y, 0);
            } else{
                bmpBinary.setPixel(x, y, 255);
            }

        }
    }
    return bmpBinary;
}

这里是我的完整代码:

public class MainActivity extends Activity {

    ImageView img;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //convert imageview to bitmap
        img =(ImageView) findViewById(R.id.imageView1);
        BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
        final Bitmap imgbitmap = drawable.getBitmap();


        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //convert bitmap to grayscale 
                Bitmap imgnew;
                imgnew = toGrayscale(imgbitmap);    
                //convert to binary
imgnew = toBinary(imgnew);

                //convert bitmap to imageview 
                ImageView imgbit;
                imgbit = (ImageView) findViewById(R.id.imageView2);
                imgbit.setImageBitmap(imgnew);
            }
        });

    }

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


public Bitmap toBinary(Bitmap bmpOriginal) {
    int width, height, threshold;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();
    threshold = 127;
    final Bitmap bmpBinary = null;

    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get one pixel color
            int pixel = bmpOriginal.getPixel(x, y);

            //get grayscale value
            int gray = (int)(pixel & 0xFF);

            //get binary value
            if(gray < threshold){
                bmpBinary.setPixel(x, y, 0);
            } else{
                bmpBinary.setPixel(x, y, 255);
            }

        }
    }
    return bmpBinary;
}

}

【问题讨论】:

  • 请告诉我二进制图像是什么意思...
  • 调用你的函数的结果是什么?你怎么了? @GopalRao,我认为他在谈论黑白图像,但只有黑白,没有灰色的细微差别。
  • @GopalRao 我的意思是黑白图像,只有 0 和 1 的值。
  • @gahfy 我想要结果是二进制图像的位图值,请帮助我,我的项目是制作一个用于叶子识别的 android 应用程序,
  • 现在你的函数的结果是什么?

标签: android bitmap binary grayscale


【解决方案1】:

首先,您会收到 NullReferenceException,因为 bmpBinary 为 NULL。
其次,要获得一个 Color chanel,您可以使用 int red = Color.red(pixel);
三、设置像素白色使用 bmpBinary.setPixel(x, y, 0xFFFFFFFF);

我稍微修改了你的代码:

public Bitmap toBinary(Bitmap bmpOriginal) {
    int width, height, threshold;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();
    threshold = 127;
    Bitmap bmpBinary = Bitmap.createBitmap(bmpOriginal);

    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get one pixel color
            int pixel = bmpOriginal.getPixel(x, y);
            int red = Color.red(pixel);

            //get binary value
            if(red < threshold){
                bmpBinary.setPixel(x, y, 0xFF000000);
            } else{
                bmpBinary.setPixel(x, y, 0xFFFFFFFF);
            }

        }
    }
    return bmpBinary;
}

更好的方法不是只使用一种颜色通道的值,而是使用红绿蓝的加权平均值,例如:

int gray = (int)(red * 0.3 + green * 0.59 + blue * 0.11);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 2011-02-28
    • 2014-01-01
    • 2015-12-07
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多