国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

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

2019-10-21 21:36:36
字體:
供稿:網(wǎng)友

1、圖像灰度化:

public Bitmap bitmap2Gray(Bitmap bmSrc) {   // 得到圖片的長(zhǎng)和寬   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、對(duì)圖像進(jìn)行線性灰度變化

public Bitmap lineGrey(Bitmap image) {  //得到圖像的寬度和長(zhǎng)度   int width = image.getWidth();   int height = image.getHeight();   //創(chuàng)建線性拉升灰度圖像   Bitmap linegray = null;   linegray = image.copy(Config.ARGB_8888, true);   //依次循環(huán)對(duì)圖像的像素進(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);       //對(duì)圖像像素越界進(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、對(duì)圖像進(jìn)行二值化

public Bitmap gray2Binary(Bitmap graymap) {   //得到圖形的寬度和長(zhǎng)度   int width = graymap.getWidth();   int height = graymap.getHeight();   //創(chuàng)建二值化圖像   Bitmap binarymap = null;   binarymap = graymap.copy(Config.ARGB_8888, true);   //依次循環(huán),對(duì)圖像的像素進(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);       //對(duì)圖像進(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; }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长宁县| 陇南市| 东乌珠穆沁旗| 丹东市| 江口县| 墨竹工卡县| 娄烦县| 昔阳县| 林州市| 天全县| 赤峰市| 浦东新区| 易门县| 辰溪县| 永寿县| 海原县| 迭部县| 巴中市| 甘南县| 合作市| 蒙城县| 博客| 金乡县| 博爱县| 体育| 浠水县| 玉田县| 高青县| 大英县| 六盘水市| 安国市| 泸西县| 南昌市| 固镇县| 进贤县| 甘孜县| 普陀区| 济阳县| 珠海市| 凤庆县| 凤庆县|