-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageThreshold.java
62 lines (56 loc) · 2.56 KB
/
ImageThreshold.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package exercises;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
*
* @Chatbot AI
* @author MOPHE
*/
public class ImageThreshold {
/* In this implementation, we first load the input image using the 'ImageIO.read()' method.
* We then apply thresholding to the image by looping through each pixel and calculating the grayscale value of that pixel.
* We define a threshold value (here, 127) to determine whether to set the pixel color to black or white.
* If the grayscale value is less than the threshold, we set the pixel color to black (0x000000),
* otherwise we set it to white (0xffffff).
*
* Finally, we write the binary image to file using the 'ImageIO.write()' method.
* This implementation uses the standard Java BufferedImage library and should work with most Java applications.
*/
public static void main(String[] args) throws IOException {
final String importPath = "C:\\Users\\MOPHE\\Documents\\NetBeansProjects\\Exercises\\src\\exercises\\Images\\lagertha.jpg";
final String exportPath = "C:\\Users\\MOPHE\\Documents\\NetBeansProjects\\Exercises\\src\\exercises\\Images\\black%whiteLagertha.jpg";
// Load the image
BufferedImage image = ImageIO.read(new File(importPath));
// Apply thresholding
int threshold = 127;
int width = image.getWidth();
int height = image.getHeight();
BufferedImage binaryImage = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_BINARY);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int rgb = image.getRGB(i, j);
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
// Zero constant can be removed
int b = (rgb) & 0xff;
// int b = (rgb >> 0) & 0xff;
int gray = (r + g + b) / 3;
if (gray < threshold) {
binaryImage.setRGB(i, j, 0x000000);
} else {
binaryImage.setRGB(i, j, 0xffffff);
}
}
}
// Write the binary image to file
File output = new File(exportPath);
ImageIO.write(binaryImage, "jpg", output);
}
}