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

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

    1.mvc5+ef6+Bootstrap 项目心得--创立之初 2.mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理 3.mvc5+ef6+Bootstrap 项目心得--WebG ...

  2. bootstrap - typeahead自动补全插件

    $('#Sale').typeahead({ ajax: { url: '@Url.Action("../Contract/GetSale")', //timeout: 300, ...

  3. 学习Python的三种境界

    前言 王国维在<人间词话>中将读书分为了三种境界:"古今之成大事业.大学问者,必经过三种之境界:'昨夜西风凋碧树,独上高楼,望尽天涯路'.此第一境也.'衣带渐宽终不悔,为伊消得人 ...

  4. [BZOJ2429][HAOI2006]聪明的猴子(MST)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2429 分析:要让最大的最小,所以就是最小生成树上的啦,于是问题就变成了有多少个猴子&g ...

  5. windows注册表学习笔记

    注册表,想起来了就学学,方便操作.无需把它当成重要学问,今日就学一波,作为了解. 一.注册表清理脚本 主要是删除临时文件,旧文件.并不能够删除无效的键 @echo off del/f/s/q %sys ...

  6. 【原创·总结】影响sql查询性能的因素

     1.表定义 (1)如果字符串字段是经常需要用到的,可以冗余,否则不要冗余 (2)经常需要作为where的查询条件的字段,可以建索引:但是过多的索引会影响写入时的性能 (3)合理定义字段的数据类型 ( ...

  7. LINUX下PHP开启短标签short_open_tag支持

    LINUX下PHP开启短标签short_open_tag支持 以CENTOS为例: 找到php.ini #find / -name php.ini #/etc/php.ini 编辑php.ini #v ...

  8. 【BZOJ 3053】The Closest M Points

    KDTree模板,在m维空间中找最近的k个点,用的是欧几里德距离. 理解了好久,昨晚始终不明白那些“估价函数”,后来才知道分情况讨论,≤k还是=k,在当前这一维度距离过线还是不过线,过线则要继续搜索另 ...

  9. 【BZOJ 2599】【IOI 2011】Race 点分治

    裸的点分治,然而我因为循环赋值$s$时把$i <= k$写成$i <= n$了,WA了好长时间 #include<cstdio> #include<cstring> ...

  10. Linux 查看文件内容

    cat   由第一行开始显示档案内容 格式: cat [选项] [文件]... -A, --show-all 等价于 -vET -b, -- 对非空输出行编号 -e 等价于 -vE -E, --在每行 ...