用于实现对图片去噪和灰度化。

package org.eye;import java.awt.Color;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;  public class ClearImageHelper {/*** * @param sfile*            需要去噪的图像* @param destDir*            去噪后的图像保存地址* @throws IOException*/public static void cleanImage(File sfile, String destDir)throws IOException{File destF = new File(destDir);if (!destF.exists()){destF.mkdirs();}BufferedImage bufferedImage = ImageIO.read(sfile);int h = bufferedImage.getHeight();int w = bufferedImage.getWidth();// 灰度化int[][] gray = new int[w][h];for (int x = 0; x < w; x++){for (int y = 0; y < h; y++){int argb = bufferedImage.getRGB(x, y);// 图像加亮(调整亮度识别率非常高)int r = (int) (((argb >> 16) & 0xFF) * 1.1 + 30);int g = (int) (((argb >> 8) & 0xFF) * 1.1 + 30);int b = (int) (((argb >> 0) & 0xFF) * 1.1 + 30);if (r >= 255){r = 255;}if (g >= 255){g = 255;}if (b >= 255){b = 255;}gray[x][y] = (int) Math.pow((Math.pow(r, 2.2) * 0.2973 + Math.pow(g, 2.2)* 0.6274 + Math.pow(b, 2.2) * 0.0753), 1 / 2.2);}}// 二值化int threshold = ostu(gray, w, h);BufferedImage binaryBufferedImage = new BufferedImage(w, h,BufferedImage.TYPE_BYTE_BINARY);for (int x = 0; x < w; x++){for (int y = 0; y < h; y++){if (gray[x][y] > threshold){gray[x][y] |= 0x00FFFF;} else{gray[x][y] &= 0xFF0000;}binaryBufferedImage.setRGB(x, y, gray[x][y]);}}// 矩阵打印for (int y = 0; y < h; y++){for (int x = 0; x < w; x++){if (isBlack(binaryBufferedImage.getRGB(x, y))){System.out.print("*");} else{System.out.print(" ");}}System.out.println();}ImageIO.write(binaryBufferedImage, "jpg", new File(destDir, sfile.getName()));}public static boolean isBlack(int colorInt){Color color = new Color(colorInt);if (color.getRed() + color.getGreen() + color.getBlue() <= 300){return true;}return false;}public static boolean isWhite(int colorInt){Color color = new Color(colorInt);if (color.getRed() + color.getGreen() + color.getBlue() > 300){return true;}return false;}public static int isBlackOrWhite(int colorInt){if (getColorBright(colorInt) < 30 || getColorBright(colorInt) > 730){return 1;}return 0;}public static int getColorBright(int colorInt){Color color = new Color(colorInt);return color.getRed() + color.getGreen() + color.getBlue();}public static int ostu(int[][] gray, int w, int h){int[] histData = new int[w * h];// Calculate histogramfor (int x = 0; x < w; x++){for (int y = 0; y < h; y++){int red = 0xFF & gray[x][y];histData[red]++;}}// Total number of pixelsint total = w * h;float sum = 0;for (int t = 0; t < 256; t++)sum += t * histData[t];float sumB = 0;int wB = 0;int wF = 0;float varMax = 0;int threshold = 0;for (int t = 0; t < 256; t++){wB += histData[t]; // Weight Backgroundif (wB == 0)continue;wF = total - wB; // Weight Foregroundif (wF == 0)break;sumB += (float) (t * histData[t]);float mB = sumB / wB; // Mean Backgroundfloat mF = (sum - sumB) / wF; // Mean Foreground// Calculate Between Class Variancefloat varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);// Check if new maximum foundif (varBetween > varMax){varMax = varBetween;threshold = t;}}return threshold;}//图片灰度,黑白public static void gray(String srcImageFile, String destImageFile) {try {BufferedImage src = ImageIO.read(new File(srcImageFile));ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);ColorConvertOp op = new ColorConvertOp(cs, null);src = op.filter(src, null);ImageIO.write(src, "JPEG", new File(destImageFile));} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) throws IOException{/*File testDataDir = new File("testdata");final String destDir = testDataDir.getAbsolutePath()+"/tmp";for (File file : testDataDir.listFiles()){cleanImage(file, destDir);}*/File testDataDir = new File("D:\\tmp\\3VG5B.jpg");//去噪String destDir ="D:\\tmp\\tmp";cleanImage(testDataDir, destDir);//gray("D:\\tmp\\3VG5B.jpg","D:\\tmp\\3VG5B1.jpg");//灰度化}
}

Java实现图片去噪和灰度的类相关推荐

  1. java将图片变灰、去噪、反色

    反色 原图: 反色后: 反色实现代码: import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.i ...

  2. Java BufferImage图片处理(获取宽高、图片截取、转换灰度图)

    Java BufferImage图片处理(获取宽高.截取.转换灰度图) 1. 效果图 2. 源码 参考 这篇博客将介绍如何使用Java读取图片为byte[]数组,或者BufferedImage及互相转 ...

  3. java将图片灰度化

    全栈工程师开发手册 (作者:栾鹏) java教程全解 java将图片灰度化 测试代码 public static void main(String[] args) {//文件与BufferedImag ...

  4. java制作海报工具类,java操作图片贴图,java给图片添加文字,调整字体颜色大小间距

    工具类 java操作图片,给一个大图片贴小图片,给图片添加文字并调整文字颜色,大小,字体间距,把本地图片或者网络图片加载到缓冲区 主要方法: imageIoRead方法,把图片加载到缓冲区 merge ...

  5. Java实现图片渲染((拖动)马赛克、黑白照、油画风格等)

    Java实现图片渲染 方法改进 ImageUI ImageListener ImageUtils 演示动画 github源代码下载: 图片渲染源代码. 方法改进 紧接上一篇实现图片渲染链接: 图片渲染 ...

  6. Java 将图片转换为素描图

    Java 将图片转换为素描图 练习的时候突然想把自己几张照片处理为素描图,到网上查了一下没找到详细的Java实现代码.自己参考着http://sharehoo.cn/159058786244608.h ...

  7. 基于Java的图片字符画(含动图)

    一.介绍 字符画是一种纯由字符组成,在文本编辑器中行列排开的二维字符矩阵中,整体展示出可识别的图案.特点是无色,画面的最小单元是字符而非像素.可由基本的文本编辑器模拟图像. 字符画的展示: 蜡笔小心表 ...

  8. Android之给图片去色,返回灰度图片以及ColorMatrix中setSaturation方法的用法

    原图: 效果图: 实现以上效果其实很简单,直接上代码: public class MainActivity extends Activity {private Button btn_start;pri ...

  9. java截取图片-设置方位+设置大小

    java截取图片-设置方位+设置大小 package com.onegrid.darj.test; import java.awt.Rectangle;   import java.awt.image ...

最新文章

  1. Android OTA 升级之三:生成recovery.img
  2. 科大星云诗社动态20210413
  3. php旧版本windows_Windows的旧版本中如何进行多任务处理?
  4. 网络编程——常用协议解析
  5. Cloud Native Weekly|2019欧洲KubeCon成功闭幕
  6. PHP动态生成select标签
  7. PRINCE2产品认证报考常见一些问答
  8. SpringBoot Mybatis Starter 解析
  9. 2019届秋季校园招聘总结
  10. 阿里云服务器出现 Resource stopwords not found. Please use the NLTK Downloader to obtain the resource:
  11. sql 查询本月请假的人数_温故而知新---学习SQL练习作业
  12. 8255A红绿灯c语言程序,汇编语言的交通灯程序
  13. tiny4412 裸机程序 六、重定位代码到IRAM+0x8000【转】
  14. MySQL使用SELECT 语句不加ORDER BY默认是如何排序的?
  15. (操作系统) 常见面试题整理
  16. 物联网卡发展历程与发展趋势
  17. C1-见习工程师能力认证-02
  18. Linux上搭建高可用nacos集群详细步骤
  19. 解决invalid url domain
  20. 微信小程序--传送方块

热门文章

  1. LetCode 3 无重复字符的最大子串
  2. vue 回车查询 按钮_前后端分离商城,前端基于Vue后端nodejs包含小程序源码免费分享...
  3. php 单例模式 序列化,php设计模式(二)单例模式
  4. 微信小程序设置域名、不校验域名
  5. 【机器学习实战】第1章 机器学习基础
  6. Java Struts2 (一)
  7. Excel35招必学秘技
  8. ubuntu11.04解决root不能登录的问题
  9. mysql5.7 首次登陆_mysql5.7.20第一次登录失败的快速解决方法
  10. React 16 Jest单元测试 之 Jest工具