新浪博客

Android下常用的图像处理程序(灰度化、线性灰度变化、二值化)

2016-08-17 16:47阅读:
1、图像灰度化:
[java] view plain copy
  1. public Bitmap bitmap2Gray(Bitmap bmSrc) {
  2. // 得到图片的长和宽
  3. int width = bmSrc.getWidth();
  4. int height = bmSrc.getHeight();
  5. // 创建目标灰度图像
  • 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、对图像进行线性灰度变化
    [java] view plain copy
  1. public Bitmap lineGrey(Bitmap image)
  2. {
  3. //得到图像的宽度和长度
  4. int width = image.getWidth();
  5. int height = image.getHeight();
  6. //创建线性拉升灰度图像
  7. Bitmap linegray = null;
  8. linegray = image.copy(Config.ARGB_8888, true);
  9. //依次循环对图像的像素进行处理
  10. for (int i = 0; i < width; i++) {
  11. for (int j = 0; j < height; j++) {
  12. //得到每点的像素值
  13. int col = image.getPixel(i, j);
  14. int alpha = col & 0xFF000000;
  15. int red = (col & 0x00FF0000) >> 16;
  16. int green = (col & 0x0000FF00) >> 8;
  17. int blue = (col & 0x000000FF);
  18. // 增加了图像的亮度
  19. red = (int) (1.1 * red + 30);
  20. green = (int) (1.1 * green + 30);
  21. blue = (int) (1.1 * blue + 30);
  22. //对图像像素越界进行处理
  23. if (red >= 255)
  24. {
  25. red = 255;
  26. }
  27. if (green >= 255) {
  28. green = 255;
  29. }
  30. if (blue >= 255) {
  31. blue = 255;
  32. }
  33. // 新的ARGB
  34. int newColor = alpha | (red << 16) | (green << 8) | blue;
  35. //设置新图像的RGB值
  36. linegray.setPixel(i, j, newColor);
  37. }
  38. }
  39. return linegray;
  40. }

3、对图像进行二值化
[java] view plain

我的更多文章

下载客户端阅读体验更佳

APP专享