Android实现图像灰度化、线性灰度变化和二值化处理方法
时间:2020-11-24 16:39:46|栏目: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; }
上一篇:Android使用CountDownTimer类实现倒计时闹钟
栏 目:Android代码
下一篇:Android开发模仿qq视频通话悬浮按钮(实例代码)
本文标题:Android实现图像灰度化、线性灰度变化和二值化处理方法
本文地址:http://www.codeinn.net/misctech/25803.html