




免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
java 图像灰度化与二值化 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 intwh; 19: for (int x = 0; x w; x+) 20: for (int y = 0; y 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: grayxy = 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 threshold) 36: grayxy |= 0x00FFFF; 37: else 38: grayxy &= 0xFF0000; 39: 40: binaryBufferedImage.setRGB(x, y, grayxy); 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) 70: return true; 71: 72: return false; 73: 74: 75: public static int isBlackOrWhite(int colorInt) 76: if (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 intw * h; 89: / Calculate histogram 90: for (int x = 0; x w; x+) 91: for (int y = 0; y h; y+) 92: int red = 0xFF & grayxy; 93: histDatared+; 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 * histDatat; 103: 104: float sumB = 0; 105: int wB = 0; 106: int wF = 0; 107: 108: float varMax = 0; 109: int threshold = 0; 110: 111: f
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年致远学院第一批次人才引进考前自测高频考点模拟试题及1套参考答案详解
- 2025合同样本:购房定金合同示范文本
- 2025江苏宿迁市泗洪县卫健系统引进高层次人才33名模拟试卷及答案详解(全优)
- 2025广东广州市百万英才汇南粤广州中医药大学第三附属医院招聘14人模拟试卷带答案详解
- 广西柳州市2026届高三上学期9月月考试题 物理 含答案
- 2025湖北恩施州宣恩县园投人力资源服务有限公司招聘多家企业人员人员考前自测高频考点模拟试题及答案详解(全优)
- 火力电厂考试题库及答案
- 电商直播考试题库及答案
- 海油电焊考试题库及答案
- 咖啡大师考试题库及答案
- GB/T 18103-2022实木复合地板
- GB/T 29084-2012航天器接地要求
- GB 31644-2018食品安全国家标准复合调味料
- 隧道开挖施工讲解课件
- 第三单元名著导读《朝花夕拾之二十四孝图》-部编版语文七年级上册
- 最新人教版四年级英语上册课件(完美版)Review of Unit 5
- 掌骨骨折查房课件
- 大学食堂装饰装修方案
- 工资结清证明(模板)
- 矿山档案(台帐) 表格参照模板参考范本
- 航运管理实务整套课件汇总完整版电子教案(全)
评论
0/150
提交评论