安卓:转换为灰度图像二值 [英] Android: Convert Grayscale to Binary Image

查看:192
本文介绍了安卓:转换为灰度图像二值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我HAVA与得到灰度值完成,但我不知道如何使用功能的灰度转换为二进制图像。请帮我,我在这里的功能code:

 公共位图toBinary(位图bmpOriginal){
    INT宽度,高度,阈值;
    高度= bmpOriginal.getHeight();
    宽度= bmpOriginal.getWidth();
    阈值= 127;
    最后的位图bmpBinary = NULL;    为(中间体X = 0; X&下;宽度++ x)的{
        对于(INT Y = 0; Y<高度; ++ Y){
            //获取一个像素的颜色
            INT像素= bmpOriginal.getPixel(X,Y);            //获取灰度值
            INT灰色=(INT)(像素和放大器; 0xFF的);            //获取二进制值
            如果(灰色<阈值){
                bmpBinary.setPixel(X,Y,0);
            }其他{
                bmpBinary.setPixel(X,Y,255);
            }        }
    }
    返回bmpBinary;
}

在这里,我完全code:

 公共类MainActivity延伸活动{    ImageView的IMG;
    按钮BTN;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        //转换ImageView的位图
        IMG =(ImageView的)findViewById(R.id.imageView1);
        BitmapDrawable绘制=(BitmapDrawable)img.getDrawable();
        最终位图imgbitmap = drawable.getBitmap();
        btn.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                //位图转换为灰度
                位图imgnew;
                imgnew = toGrayscale(imgbitmap);
                //转换为二进制
imgnew = toBinary(imgnew);                //位图转换为ImageView的
                ImageView的imgbit;
                imgbit =(ImageView的)findViewById(R.id.imageView2);
                imgbit.setImageBitmap(imgnew);
            }
        });    }    公共位图toGrayscale(位图bmpOriginal){
        INT宽度,高度;
        高度= bmpOriginal.getHeight();
        宽度= bmpOriginal.getWidth();        位图bmpGrayscale = Bitmap.createBitmap(宽度,高度,Bitmap.Config.RGB_565);
        帆布C =新的Canvas(bmpGrayscale);
        涂料粉刷=新的油漆();
        嘉洛斯厘米=新嘉洛斯();
        cm.setSaturation(0);
        ColorMatrixColorFilter F =新ColorMatrixColorFilter(厘米);
        paint.setColorFilter(F);
        c.drawBitmap(bmpOriginal,0,0,油漆);
        返回bmpGrayscale;
    }
公共位图toBinary(位图bmpOriginal){
    INT宽度,高度,阈值;
    高度= bmpOriginal.getHeight();
    宽度= bmpOriginal.getWidth();
    阈值= 127;
    最后的位图bmpBinary = NULL;    为(中间体X = 0; X&下;宽度++ x)的{
        对于(INT Y = 0; Y<高度; ++ Y){
            //获取一个像素的颜色
            INT像素= bmpOriginal.getPixel(X,Y);            //获取灰度值
            INT灰色=(INT)(像素和放大器; 0xFF的);            //获取二进制值
            如果(灰色<阈值){
                bmpBinary.setPixel(X,Y,0);
            }其他{
                bmpBinary.setPixel(X,Y,255);
            }        }
    }
    返回bmpBinary;
}}


解决方案

首先,你会得到一个NullReferenceException因为bmpBinary为NULL。结果
其次,让你可以使用INT红色= Color.red(像素)的一种颜色香奈儿;结果
第三,要设置一个像素的白色使用bmpBinary.setPixel(X,Y,0xFFFFFFFF的);搜索结果

我修改您的code了一下:

 公共位图toBinary(位图bmpOriginal){
    INT宽度,高度,阈值;
    高度= bmpOriginal.getHeight();
    宽度= bmpOriginal.getWidth();
    阈值= 127;
    位图bmpBinary = Bitmap.createBitmap(bmpOriginal);    为(中间体X = 0; X&下;宽度++ x)的{
        对于(INT Y = 0; Y<高度; ++ Y){
            //获取一个像素的颜色
            INT像素= bmpOriginal.getPixel(X,Y);
            INT红色= Color.red(像素);            //获取二进制值
            如果(红色<阈值){
                bmpBinary.setPixel(X,Y,0xFF000000);
            }其他{
                bmpBinary.setPixel(X,Y,为0xFFFFFFFF);
            }        }
    }
    返回bmpBinary;
}

一个更好的方法是不使用一种颜色香奈儿只是价值,但加权平均的红色绿色和蓝色,例如:搜索

  INT灰色=(INT)(红* 0.3 +绿* 0.59 +蓝色* 0.11);

i hava done with get grayscale value, but i don't know how to use function to convert the grayscale to be binary image. Please help me, here my function code:

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

here my full code:

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

}

解决方案

First, you get a NullReferenceException because bmpBinary is NULL.
Second, to get one Color chanel you can use int red = Color.red(pixel);
Third, to set a pixel white use bmpBinary.setPixel(x, y, 0xFFFFFFFF);

I modified your code a bit:

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

An even better way is not to use just the value of one color chanel but a weighted average of red green and blue for example:

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

这篇关于安卓:转换为灰度图像二值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆