由于需求,在做图像处理这块,大概也学习了小半年,本文利用Java通过设置一个阈值来对一图像进行二值化处理。
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import javax.imageio.ImageIO;
public class BinaryTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedImage image = ImageIO.read(new File("/Volumes/Function/G/test/bgWhite.jpg"));
int w = image.getWidth();
int h = image.getHeight();
float[] rgb = new float[3];
double[][] zuobiao = new double[w][h];
int R = 0;
float red = 0;
float green = 0;
float blue = 0;
BufferedImage bi= new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_BINARY);;
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int pixel = image.getRGB(x, y);
sop(pixel);
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
red += rgb[0];
green += rgb[1];
blue += rgb[2];
R = (x+1) *(y+1);
float avg = (rgb[0]+rgb[1]+rgb[2])/3;
zuobiao[x][y] = avg;
}
}
double SW = 170;
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
if (zuobiao[x][y] <= SW) {
int max = new Color(0, 0, 0).getRGB();
bi.setRGB(x, y, max);
}else{
int min = new Color(255, 255, 255).getRGB();
bi.setRGB(x, y, min);
}
}
}
ImageIO.write(bi, "jpg", new File("/Volumes/Function/G/test2/binaryTest.jpg"));
}
public static void sop(Object obj) {
System.out.println(obj);
}
}
效果图如下: