public static BufferedImage binary(BufferedImage sourceImg) throws IOException {
int width = sourceImg.getWidth();
int height = sourceImg.getHeight();
// 灰度
int[][] grayBits = new int[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
grayBits[i][j] = calcGray(sourceImg.getRGB(i, j));
}
}
// 閾值
int threshold = thresholdOTSU(grayBits, width, height);
int white = new Color(255, 255, 255).getRGB();
int black = new Color(0, 0, 0).getRGB();
BufferedImage bf = new BufferedImage(width, height, TYPE_BYTE_BINARY);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (grayBits[i][j] > threshold) {
bf.setRGB(i, j, white);
} else {
bf.setRGB(i, j, black);
}
}
}
return bf;
}
原圖

二值后:

放大后部磁區域后,有灰色顏色的部分還存在:

在bf.setRGB()的時候也把顏色值輸出看了,全部都是 (0,0,0)或者(255,255,255),怎么還會有其他顏色存在,求大佬們指點指點,不勝感激 !!!
uj5u.com熱心網友回復:
坑B
ImageIO.write(binary(image), "jpg", new File("1.jpg"));
用ImageIO.write 輸出 jpg格式時,圖片質量會損失 ,輸出png就可以了
ImageIO.write(binary(image), "png", new File("1.png"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/188374.html
標籤:Java SE
上一篇:為什么這個位域輸出不對
