Android影像灰度化、線性灰度變化、二值化處理方法

sakura_echo發表於2018-11-08

android

1、影像灰度化:

public Bitmap bitmap2Gray(Bitmap bmSrc) {  
    // 得到圖片的長和寬  
    int width = bmSrc.getWidth();  
    int height = bmSrc.getHeight();  
    // 建立目標灰度影像  
    Bitmap bmpGray = null;  
    bmpGray = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
    // 建立畫布  
    Canvas c = new Canvas(bmpGray);  
    Paint paint = new Paint();  
    ColorMatrix cm = new ColorMatrix();  
    cm.setSaturation(0);  
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);  
    paint.setColorFilter(f);  
    c.drawBitmap(bmSrc, 0, 0, paint);  
    return bmpGray;  
}

2、對影像進行線性灰度變化

public Bitmap lineGrey(Bitmap image) {
    //得到影像的寬度和長度  
    int width = image.getWidth();  
    int height = image.getHeight();  
    //建立線性拉昇灰度影像  
    Bitmap linegray = null;  
    linegray = image.copy(Config.ARGB_8888, true);  
    //依次迴圈對影像的畫素進行處理  
    for (int i = 0; i < width; i++) {  
        for (int j = 0; j < height; j++) {  
            //得到每點的畫素值  
            int col = image.getPixel(i, j);  
            int alpha = col & 0xFF000000;  
            int red = (col & 0x00FF0000) >> 16;  
            int green = (col & 0x0000FF00) >> 8;  
            int blue = (col & 0x000000FF);  
            // 增加了影像的亮度  
            red = (int) (1.1 * red + 30);  
            green = (int) (1.1 * green + 30);  
            blue = (int) (1.1 * blue + 30);  
            //對影像畫素越界進行處理  
            if (red >= 255)   
            {  
                red = 255;  
            }  

            if (green >= 255) {  
                green = 255;  
            }  

            if (blue >= 255) {  
                blue = 255;  
            }  
            // 新的ARGB  
            int newColor = alpha | (red << 16) | (green << 8) | blue;  
            //設定新影像的RGB值  
            linegray.setPixel(i, j, newColor);  
        }  
    }  
    return linegray;  
}  

3、對影像進行二值化

public Bitmap gray2Binary(Bitmap graymap) {  
    //得到圖形的寬度和長度  
    int width = graymap.getWidth();  
    int height = graymap.getHeight();  
    //建立二值化影像  
    Bitmap binarymap = null;  
    binarymap = graymap.copy(Config.ARGB_8888, true);  
    //依次迴圈,對影像的畫素進行處理  
    for (int i = 0; i < width; i++) {  
        for (int j = 0; j < height; j++) {  
            //得到當前畫素的值  
            int col = binarymap.getPixel(i, j);  
            //得到alpha通道的值  
            int alpha = col & 0xFF000000;  
            //得到影像的畫素RGB的值  
            int red = (col & 0x00FF0000) >> 16;  
            int green = (col & 0x0000FF00) >> 8;  
            int blue = (col & 0x000000FF);  
            // 用公式X = 0.3×R+0.59×G+0.11×B計算出X代替原來的RGB  
            int gray = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);  
            //對影像進行二值化處理  
            if (gray <= 95) {  
                gray = 0;  
            } else {  
                gray = 255;  
            }  
            // 新的ARGB  
            int newColor = alpha | (gray << 16) | (gray << 8) | gray;  
            //設定新影像的當前畫素值  
            binarymap.setPixel(i, j, newColor);  
        }  
    }  
    return binarymap;  
}  


相關文章