转载:http://www.chinasb.org/archives/2013/01/5053.shtml

   1:  package org.chinasb.client;
   2:   
   3:  import java.awt.Color;
   4:  import java.awt.image.BufferedImage;
   5:  import java.io.File;
   6:  import java.io.IOException;
   7:   
   8:  import javax.imageio.ImageIO;
   9:   
  10:  public class BinaryTest {
  11:   
  12:      public static void main(String[] args) throws IOException {
  13:          BufferedImage bufferedImage = ImageIO.read(new File("D:/passCodeAction.jpg"));
  14:          int h = bufferedImage.getHeight();
  15:          int w = bufferedImage.getWidth();
  16:   
  17:          // 灰度化
  18:          int[][] gray = new int[w][h];
  19:          for (int x = 0; x < w; x++) {
  20:              for (int y = 0; y < h; y++) {
  21:                  int argb = bufferedImage.getRGB(x, y);
  22:                  int r = (argb >> 16) & 0xFF;
  23:                  int g = (argb >> 8) & 0xFF;
  24:                  int b = (argb >> 0) & 0xFF;
  25:                  int grayPixel = (int) ((b * 29 + g * 150 + r * 77 + 128) >> 8);                
  26:                  gray[x][y] = grayPixel;
  27:              }
  28:          }
  29:   
  30:          // 二值化
  31:          int threshold = ostu(gray, w, h);
  32:          BufferedImage binaryBufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
  33:          for (int x = 0; x < w; x++) {
  34:              for (int y = 0; y < h; y++) {
  35:                  if (gray[x][y] > threshold) {
  36:                      gray[x][y] |= 0x00FFFF;
  37:                  } else {
  38:                      gray[x][y] &= 0xFF0000;
  39:                  }
  40:                  binaryBufferedImage.setRGB(x, y, gray[x][y]);
  41:              }
  42:          }
  43:   
  44:          // 矩阵打印
  45:          for (int y = 0; y < h; y++) {
  46:              for (int x = 0; x < w; x++) {
  47:                  if (isBlack(binaryBufferedImage.getRGB(x, y))) {
  48:                      System.out.print("*");
  49:                  } else {
  50:                      System.out.print(" ");
  51:                  }
  52:              }
  53:              System.out.println();
  54:          }
  55:   
  56:          ImageIO.write(binaryBufferedImage, "jpg", new File("D:/code.jpg"));
  57:      }
  58:   
  59:      public static boolean isBlack(int colorInt) {
  60:          Color color = new Color(colorInt);
  61:          if (color.getRed() + color.getGreen() + color.getBlue() <= 300) {
  62:              return true;
  63:          }
  64:          return false;
  65:      }
  66:   
  67:      public static boolean isWhite(int colorInt) {
  68:          Color color = new Color(colorInt);
  69:          if (color.getRed() + color.getGreen() + color.getBlue() > 300) {
  70:              return true;
  71:          }
  72:          return false;
  73:      }
  74:   
  75:      public static int isBlackOrWhite(int colorInt) {
  76:          if (getColorBright(colorInt) < 30 || getColorBright(colorInt) > 730) {
  77:              return 1;
  78:          }
  79:          return 0;
  80:      }
  81:   
  82:      public static int getColorBright(int colorInt) {
  83:          Color color = new Color(colorInt);
  84:          return color.getRed() + color.getGreen() + color.getBlue();
  85:      }
  86:   
  87:      public static int ostu(int[][] gray, int w, int h) {
  88:          int[] histData = new int[w * h];
  89:          // Calculate histogram
  90:          for (int x = 0; x < w; x++) {
  91:              for (int y = 0; y < h; y++) {
  92:                  int red = 0xFF & gray[x][y];
  93:                  histData[red]++;
  94:              }
  95:          }
  96:   
  97:          // Total number of pixels
  98:          int total = w * h;
  99:   
 100:          float sum = 0;
 101:          for (int t = 0; t < 256; t++)
 102:              sum += t * histData[t];
 103:   
 104:          float sumB = 0;
 105:          int wB = 0;
 106:          int wF = 0;
 107:   
 108:          float varMax = 0;
 109:          int threshold = 0;
 110:   
 111:          for (int t = 0; t < 256; t++) {
 112:              wB += histData[t]; // Weight Background
 113:              if (wB == 0)
 114:                  continue;
 115:   
 116:              wF = total - wB; // Weight Foreground
 117:              if (wF == 0)
 118:                  break;
 119:   
 120:              sumB += (float) (t * histData[t]);
 121:   
 122:              float mB = sumB / wB; // Mean Background
 123:              float mF = (sum - sumB) / wF; // Mean Foreground
 124:   
 125:              // Calculate Between Class Variance
 126:              float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);
 127:   
 128:              // Check if new maximum found
 129:              if (varBetween > varMax) {
 130:                  varMax = varBetween;
 131:                  threshold = t;
 132:              }
 133:          }
 134:   
 135:          return threshold;
 136:      }
 137:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

效果

java 图像灰度化与二值化的更多相关文章

  1. Java基于opencv实现图像数字识别(三)—灰度化和二值化

    Java基于opencv实现图像数字识别(三)-灰度化和二值化 一.灰度化 灰度化:在RGB模型中,如果R=G=B时,则彩色表示灰度颜色,其中R=G=B的值叫灰度值:因此,灰度图像每个像素点只需一个字 ...

  2. Opencv实现图像的灰度处理,二值化,阀值选择

    前几天接触了图像的处理,发现用OPencv处理确实比較方便.毕竟是非常多东西都封装好的.可是要研究里面的东西,还是比較麻烦的,首先,你得知道图片处理的一些知识,比方腐蚀,膨胀,仿射,透射等,还有非常多 ...

  3. c#图像灰度化、灰度反转、二值化

    图像灰度化:将彩色图像转化成为灰度图像的过程成为图像的灰度化处理.彩色图像中的每个像素的颜色有R.G.B三个分量决定,而每个分量有255中值可取,这样一个像素点可以有1600多万(255*255*25 ...

  4. OpenCV图像的全局阈值二值化函数(OTSU)

    cv::threshold(GrayImg, Bw, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);//灰度图像二值化 CV_THRESH_OTSU是提取图像最 ...

  5. 实现图像的二值化(java+opencv)

    书里的解释: 其他的没找到什么资料,直接参考百度百科 https://baike.baidu.com/item/%E5%9B%BE%E5%83%8F%E4%BA%8C%E5%80%BC%E5%8C%9 ...

  6. atitit.验证码识别step4--------图形二值化 灰度化

    atitit.验证码识别step4--------图形二值化 灰度化 1. 常见二值化的方法原理总结 1 1.1. 方法一:该方法非常简单,对RGB彩色图像灰度化以后,扫描图像的每个像素值,值小于12 ...

  7. [iOS OpenCV的使用,灰度和二值化]

    看网上方法很多,但版本都不够新,我看了网上一些知识,总结了下,来个最新版Xcode6.1的. 最近主要想做iOS端的车牌识别,所以开始了解OpenCV.有兴趣的可以跟我交流下哈. 一.Opencv的使 ...

  8. [转载+原创]Emgu CV on C# (四) —— Emgu CV on 全局固定阈值二值化

    重点介绍了全局二值化原理及数学实现,并利用emgucv方法编程实现. 一.理论概述(转载,如果懂图像处理,可以略过,仅用作科普,或者写文章凑字数)  1.概述 图像二值化是图像处理中的一项基本技术,也 ...

  9. 机器学习进阶-项目实战-信用卡数字识别 1.cv2.findContour(找出轮廓) 2.cv2.boudingRect(轮廓外接矩阵位置) 3.cv2.threshold(图片二值化操作) 4.cv2.MORPH_TOPHAT(礼帽运算突出线条) 5.cv2.MORPH_CLOSE(闭运算图片内部膨胀) 6. cv2.resize(改变图像大小) 7.cv2.putText(在图片上放上文本)

    7. cv2.putText(img, text, loc, text_font, font_scale, color, linestick) # 参数说明:img表示输入图片,text表示需要填写的 ...

随机推荐

  1. Tensorflow学习笔记3:TensorBoard可视化学习

    TensorBoard简介 Tensorflow发布包中提供了TensorBoard,用于展示Tensorflow任务在计算过程中的Graph.定量指标图以及附加数据.大致的效果如下所示, Tenso ...

  2. 隐马尔可夫模型(Hidden Markov Model,HMM)

    介绍 崔晓源 翻译 我们通常都习惯寻找一个事物在一段时间里的变化规律.在很多领域我们都希望找到这个规律,比如计算机中的指令顺序,句子中的词顺序和语音中的词顺序等等.一个最适用的例子就是天气的预测. 首 ...

  3. 最短判断IE的办法

    if(!!-[1,]){ return }; 无意中看到这样一行代码,经查是用来判断IE的代码,非常精简,原理如下: [1,],这是一个数组,IE和标准浏览器对这样一个数组的解析是不一样的 alert ...

  4. 35-less 简明笔记

    分屏显示文本文件 less [options] [file-list] less与more类似,但比more更加完善 例如:在显示一屏文本之后,less将显示提示副等待下一条命令的输入;可以向前或向后 ...

  5. yii2搭建完美后台并实现rbac权限控制实例教程

    1.安装yii2 未安装的请参考yii2史上最简单式安装教程,没有之一 或者参考yii2实战教程之详细安装步骤 已安装的请继续看下一步操作 2.配置数据库 2.1 配置数据库 修改common/con ...

  6. Beta版本冲刺———第三天

    会议照片: 项目燃尽图: 1.项目进展: 今天解决的进度:对游戏结束的检测进行了完善,使分数标签和最高分标签的变化更加合理. 仍在进行对排行榜分数变更的实现 2.每个人每天做的事情 郭怡锋:汇总工作进 ...

  7. 使用Servlet实现下载文件的功能

    在前台有一个下载链接,比如 <a href="DownLoadServlet">下载</a> <br/> 使用Servlet实现下载: impo ...

  8. javascript中数组去重的4种方法

    面试前端必须准备的一道问题:怎样去掉Javascript的Array的重复项.在最近面试中,百度.腾讯.盛大等都在面试里出过这个题目.这个问题看起来简单,但其实暗藏杀机. 考的不仅仅是实现这个功能,更 ...

  9. 【codevs1012】最大公约数和最小公倍数

    题目描述 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件:  1.P,Q是正整 ...

  10. bzoj2946: [Poi2000]公共串

    SAM处女题qwq #include <iostream> #include <cstdio> #include <cstring> #include <cm ...