Home > Back-end >  After image binarization, BufferedImage setRGB, written to a local, black pixels and white pixels of
After image binarization, BufferedImage setRGB, written to a local, black pixels and white pixels of

Time:11-06

 
Public static BufferedImage binary (BufferedImage sourceImg) throws IOException {
Int width=sourceImg. GetWidth ();
Int height=sourceImg. GetHeight ();

//gray
Int [] [] grayBits=new int [width] [height];
For (int I=0; I & lt; Width; I++) {
For (int j=0; J & lt; Height; J++) {
GrayBits [I] [j]=calcGray (sourceImg. GetRGB (I, j));
}
}

//threshold
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 & lt; Width; I++) {
For (int j=0; J & lt; Height; J++) {
If (grayBits [I] [j] & gt; Threshold) {
Bf. SetRGB (I, j, white);
} else {
Bf. SetRGB (I, j, black);
}
}
}
Return the bf;
}


The original



After the two values:



After enlarged area, with gray color part is:



In bf. SetRGB () when they read the output color values, all of them are (0, 0) and (255255255), how will there be any other color, bosses some directions, thank!!!!!!

CodePudding user response:

Pit B
 
ImageIO. Write (binary (image), "JPG", the new File (" 1. JPG "));

Use ImageIO. Write output JPG format, the image quality loss, output PNG is


 
ImageIO. Write (binary (image), "PNG", the new File (" 1. PNG "));
  • Related