Java实现图片去噪和灰度的类
来源:互联网 发布:jdk源码分析 pdf 编辑:程序博客网 时间:2024/06/14 22:29
用于实现对图片去噪和灰度化。
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");//灰度化}}
0 0
- Java实现图片去噪和灰度的类
- 灰度增强和去噪
- 灰度图像增强和图像去噪
- java实现图片转化为字符图片--图片的灰度化
- Android图片转换类 1. Bitmap去色,转换为黑白的灰度图, 2. Bitmap图片加圆角效果
- Android图片转换类 1. Bitmap去色,转换为黑白的灰度图, 2. Bitmap图片加圆角效果
- java 图片灰度化
- UGUI 图片去色,变灰度图
- 用Glide去实现图片的缓存和瀑布流
- Java实现24位真彩转换为8位灰度图片
- java图片灰度化原理与实现(未测试)
- 图片的灰度图
- 图像的灰度化原理和实现
- 图像的灰度化原理和实现
- 图像的灰度化原理和实现
- [J2ME]-图片灰度转换实现
- Atitit 图像处理 灰度图片 灰度化的原理与实现
- java 图片处理灰度化
- Matlab/VS调试
- Light oj 1179 - Josephus Problem 【思维】
- 查看SVN 历史版本时报错
- 进程的运行顺序
- C++内存管理
- Java实现图片去噪和灰度的类
- httpd系列-01-常用配置
- 插入排序及其优化
- 基本数据类型范围
- 窗口的查找与控制
- HTML/CSS: Make a Ring Menu 环形菜单
- SQL Server大容量导入和导出 XML 文档的示例
- Struts+mybatis+spring整合tomcat启动报错
- 【javamail】 通过javamail用各种邮箱发送邮件!!