反色
原图:
反色后:
反色实现代码:
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);
//图片置灰方式2
makeImageColorToBlackWhite("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 block
e.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 block
e.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 histogram
for (int x = 0; x < w; x++){
for (int y = 0; y < h; y++){
int red = 0xFF & gray[x][y];
histData[red]++;
}
}
// Total number of pixels
int 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 Background
if (wB == 0)
continue;
wF = total - wB; // Weight Foreground
if (wF == 0)
break;
sumB += (float) (t * histData[t]);
float mB = sumB / wB; // Mean Background
float mF = (sum - sumB) / wF; // Mean Foreground
// Calculate Between Class Variance
float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);
// Check if new maximum found
if (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();
}
}
}