反色

原图:

反色后:

反色实现代码:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;public class ImageColor {/*** @Description: 反色* @param  imgPath 图片路径* @param  fileUrl 输出图片路径* @throws*/public static void inverse(String imgPath, String fileUrl){try {FileInputStream fileInputStream = new FileInputStream(imgPath);BufferedImage image = ImageIO.read(fileInputStream);//生成字符图片int w = image.getWidth();int h = image.getHeight();BufferedImage imageBuffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);;// 绘制字符for (int y = 0; y < h; y++) {for (int x = 0; x< w; x++) {int rgb = image.getRGB(x, y);int R = (rgb & 0xff0000) >> 16;int G = (rgb & 0x00ff00) >> 8;int B = rgb & 0x0000ff;int newPixel=colorToRGB(255-R,255-G,255-B);imageBuffer.setRGB(x,y,newPixel);}}ImageIO.write(imageBuffer, "png", new File(fileUrl)); //输出图片} catch (Exception e) {e.printStackTrace();}}/*** @Description: 颜色转rgb值* @throws*/public static int colorToRGB(int red,int green,int blue){int newPixel=0;newPixel=newPixel << 8;newPixel+=red;newPixel=newPixel << 8;newPixel+=green;newPixel=newPixel << 8;newPixel+=blue;return  newPixel;}public static void main(String[] args) throws IOException {inverse("D:\\121212.png","D:\\333.png");}
}

置灰、去噪

图片置灰方式1

实现代码:

图片置灰方式2

图片去噪

import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;import javax.imageio.ImageIO;public class DecimalFormatDemo {public static void main(String[] args) throws Exception {//图片置灰方式1//图片源文件String imagePath = "D:\\qqqq.jpeg";BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imagePath));BufferedImage grayImage = getGrayImage(bufferedImage);//输出目录+输出文件File out = new File("D:\\heads.png");ImageIO.write(grayImage, "png", out);//图片置灰方式2makeImageColorToBlackWhite("D:\\qqqq.jpeg","D:\\head1.png");//图片去噪File testDataDir = new File("D:\\qqqq.jpeg");String destDir ="D:\\test";cleanImage(testDataDir, destDir);}/*** 将图片置灰 ---  使用现成的类** @param originalImage* @return*/public static BufferedImage getGrayImage(BufferedImage originalImage) {BufferedImage grayImage;int imageWidth = originalImage.getWidth();int imageHeight = originalImage.getHeight();//TYPE_3BYTE_BGR -->  表示一个具有 8 位 RGB 颜色分量的图像,对应于 Windows 风格的 BGR 颜色模型,具有用 3 字节存储的 Blue、Green 和 Red 三种颜色。grayImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_3BYTE_BGR);ColorConvertOp cco = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);cco.filter(originalImage, grayImage);return grayImage;}/*** 将彩色图片变为灰色的图片** @param imagePath*/public static void makeImageColorToBlackWhite(String imagePath,String outputPath) {int[][] result = getImageGRB(imagePath);int[] rgb = new int[3];BufferedImage bi = new BufferedImage(result.length, result[0].length, BufferedImage.TYPE_INT_RGB);for (int i = 0; i < result.length; i++) {for (int j = 0; j < result[i].length; j++) {rgb[0] = (result[i][j] & 0xff0000) >> 16;rgb[1] = (result[i][j] & 0xff00) >> 8;rgb[2] = (result[i][j] & 0xff);int color = (int) (rgb[0] * 0.3 + rgb[1] * 0.59 + rgb[2] * 0.11);
//color = color > 128 ? 255 : 0;bi.setRGB(i, j, (color << 16) | (color << 8) | color);}}try {ImageIO.write(bi, "png", new File(outputPath));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 获取图片的像素点** @param filePath* @return*/public static int[][] getImageGRB(String filePath) {File file = new File(filePath);int[][] result = null;if (!file.exists()) {System.out.println("图片不存在");return result;}try {BufferedImage bufImg = ImageIO.read(file);int height = bufImg.getHeight();int width = bufImg.getWidth();result = new int[width][height];for (int i = 0; i < width; i++) {for (int j = 0; j < height; j++) {result[i][j] = bufImg.getRGB(i, j) & 0xFFFFFF;}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}/**** @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();}}
}

java将图片变灰、去噪、反色相关推荐

  1. NumPy处理图像:色彩取反、图片变灰、图像手绘

    教程:Python数据分析与展示_北京理工大学 目录 图像的数组表示 图像的变换 "图像的手绘效果"实例分析 图像的数组表示 图像一般使用RGB色彩模式,即每个像素点的颜色由红( ...

  2. html 图片变灰,科技常识:css使图片变灰的实现方法

    今天小编跟大家讲解下有关css使图片变灰的实现方法 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了有关css使图片变灰的实现方法 的相关资料,希望小伙伴们看了有所帮助. 如果您是想将页面 网页 ...

  3. ui unity 图片高亮_Unity5 UI图片变灰处理(UGUI)(二)

    图片"变灰"处理是再寻常不过的要求了,特别按钮,头像等UI图片的"变灰"处理非常常见.比如: 网上已经有很多的实现方法,但是Unity5.3.8以后,对于使用s ...

  4. html鼠标悬停多个图片变灰,怎样用CSS让鼠标悬停在图片上图片变灰

    我们常常在网页中看到这样一种效果,当你的鼠标移动到图片上的时候,图片变灰,或者变暗,这实际是图片被CSS设置为半透明样式.下面就给大家做一个实例看一下这种效果怎么实现a:hover img{filte ...

  5. CSS鼠标悬停图片上图片变灰 变色 半透明

    CSS鼠标悬停图片上图片变灰 变色 半透明 DIV CSS鼠标移动悬停在图片上图片变色或半透明变化效果实现,CSS鼠标悬停图片上图片变灰.图片变色.图片半透明 一.DIVCSS5介绍与说明:   - ...

  6. java 裁剪图片变大_java上传图片放大(小图等比放大,大图等比裁剪)

    java上传图片放大(小图等比放大,大图等比裁剪): package oms.util; import java.awt.Graphics; import java.awt.Image; import ...

  7. WinForm 图片变灰方法

    最近公司项目用到文件树,对于工程文件中不存在的文件,要给予灰色显示,如何让图片成灰色,在网上找了个效率较高的方法,很方便调用,故记录. 1 public Bitmap ExColorDepth(Ima ...

  8. 【小松教你手游开发】【unity实用技能】Unity图片变灰的方式

    http://www.tuicool.com/articles/Vruuqme NGUI中的Button几乎是最常用到的控件之一,并且可以组合各种组件(比如UIButtonColor,UIButton ...

  9. c# winform 把彩色图片转换为灰色的图片,变灰,灰度图片,速度很快,safe,unsafe

    把彩色图片转换为灰色的图片,直接用.net接口遍历每个像素点转换的效率非常低,800K的图片65万像素我的电脑要用5分钟,而用了unsafe,速度提高了几千倍,同样的图片只用了0.几秒 附一个常用的遍 ...

最新文章

  1. sublime text3 怎么配置、运行python_【IT专家】Sublime Text3配置在可交互环境下运行python快捷键...
  2. java 单例 缓存hashmap_java 、HashMap 和单例
  3. Topsky酒店管理系统v1.4.2.3
  4. asp/php招聘,招聘ASP与PHP相关岗位的笔经
  5. UpdatePanelAnimation
  6. TeamWork#3,Week5,The First Meeting of Our Team
  7. java删除指定文件后重新建立文件系统_java file 操作之创建、删除文件及文件夹...
  8. 小米note2鸿蒙ROM,小米Note2官方原版系统rom线刷刷机包_小米Note2线刷官方包
  9. AI模型的大一统!浅析微软的BEIT3:多模态领域乱杀的十二边形战士
  10. mysql数据库查询优化技术 视频_那海蓝蓝 MySQL数据库查询优化技术视频教程
  11. Progressive GAN
  12. 【阿里在线技术峰会】李金波:企业大数据平台仓库架构建设思路
  13. 华为正式发布“鸿蒙”,率先进行应用开发。
  14. #中秋节#迅镭激光第四届中秋游园会活动精彩回顾
  15. 永中word页码怎么从第二页开始_快捷的word文档转pdf好方法推荐
  16. 目标检测YOLO实战应用案例100讲-面向目标检测的语义分割技术研究与应用
  17. Part GeoAI----当ArcGIS遇上人工智能
  18. Tier和Layer
  19. 教你如何编写shell脚本
  20. ninja介绍及使用

热门文章

  1. 超级浏览器的技术原理,超级浏览器的浏览器指纹是什么?
  2. 关于mobiscroll日期插件无法正确的选中默认日期
  3. docker之部署mall开源项目
  4. GPS从入门到放弃(十二) --- 多普勒定速
  5. 针对电子企业的仓储需求,提出WMS仓储管理系统解决方案
  6. 微信小程序-底部导航栏
  7. 项目进度管理的算法总结
  8. 计算机专业大学新学年计划,大学的新学期的学习计划(精选5篇)
  9. 小区物业管理系统-总结-数据库设计
  10. 基于python的npcap库与dpkt库实现抓包及存储