欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)圖像灰度化、線性灰度變化和二值化處理方法

 更新時(shí)間:2018年10月30日 11:45:28   作者:Sakura_echo  
這篇文章主要介紹了Android實(shí)現(xiàn)圖像灰度化、線性灰度變化和二值化處理方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

1、圖像灰度化:

public Bitmap bitmap2Gray(Bitmap bmSrc) { 
  // 得到圖片的長和寬 
  int width = bmSrc.getWidth(); 
  int height = bmSrc.getHeight(); 
  // 創(chuàng)建目標(biāo)灰度圖像 
  Bitmap bmpGray = null; 
  bmpGray = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 
  // 創(chuàng)建畫布 
  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、對圖像進(jìn)行線性灰度變化

public Bitmap lineGrey(Bitmap image) {
  //得到圖像的寬度和長度 
  int width = image.getWidth(); 
  int height = image.getHeight(); 
  //創(chuàng)建線性拉升灰度圖像 
  Bitmap linegray = null; 
  linegray = image.copy(Config.ARGB_8888, true); 
  //依次循環(huán)對圖像的像素進(jìn)行處理 
  for (int i = 0; i < width; i++) { 
    for (int j = 0; j < height; j++) { 
      //得到每點(diǎn)的像素值 
      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); 
      //對圖像像素越界進(jìn)行處理 
      if (red >= 255)  
      { 
        red = 255; 
      } 

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

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

3、對圖像進(jìn)行二值化

public Bitmap gray2Binary(Bitmap graymap) { 
  //得到圖形的寬度和長度 
  int width = graymap.getWidth(); 
  int height = graymap.getHeight(); 
  //創(chuàng)建二值化圖像 
  Bitmap binarymap = null; 
  binarymap = graymap.copy(Config.ARGB_8888, true); 
  //依次循環(huán),對圖像的像素進(jìn)行處理 
  for (int i = 0; i < width; i++) { 
    for (int j = 0; j < height; j++) { 
      //得到當(dāng)前像素的值 
      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計(jì)算出X代替原來的RGB 
      int gray = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11); 
      //對圖像進(jìn)行二值化處理 
      if (gray <= 95) { 
        gray = 0; 
      } else { 
        gray = 255; 
      } 
      // 新的ARGB 
      int newColor = alpha | (gray << 16) | (gray << 8) | gray; 
      //設(shè)置新圖像的當(dāng)前像素值 
      binarymap.setPixel(i, j, newColor); 
    } 
  } 
  return binarymap; 
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論