-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGrayScale.java
118 lines (101 loc) · 3.77 KB
/
GrayScale.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class GrayScale {
BufferedImage image;
BufferedImage gray_image;
int[] freq = new int[256];
int width;
int height;
/**
* It get the gray scale of the image
*/
public GrayScale() {
try {
String input_img_path = "digital_image_processing.jpg"; //Input image here
String gray_img_path = "grayscale.jpg";
File input = new File(input_img_path);
// Reading image
image = ImageIO.read(input);
width = image.getWidth();
height = image.getHeight();
// RGB to Gray scale conversion
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Color c = new Color(image.getRGB(j, i));
int red = (int) (c.getRed() * 0.299);
int green = (int) (c.getGreen() * 0.587);
int blue = (int) (c.getBlue() * 0.114);
Color newColor = new Color(red + green + blue,
red + green + blue, red + green + blue);
image.setRGB(j, i, newColor.getRGB());
}
}
// Writing converted image to file
File ouptut = new File(gray_img_path);
ImageIO.write(image, "jpg", ouptut);
// Original image
JFrame frame = new JFrame();
ImageIcon icon = new ImageIcon(input_img_path);
JLabel label = new JLabel(icon);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
// Converted image
JFrame frame1 = new JFrame();
ImageIcon icon1 = new ImageIcon(gray_img_path);
JLabel label1 = new JLabel(icon1);
frame1.add(label1);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);
// Grayscale image manipulation
gray_image = ImageIO.read(new File(gray_img_path));
width = gray_image.getWidth();
height = gray_image.getHeight();
Raster raster = gray_image.getData();
int[][] img_arr = new int[width][height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
img_arr[j][i] = raster.getSample(j, i, 0);
}
}
// Image details
System.out.println("Image Height :" + height);
System.out.println("Image Width :" + width);
// to calculate the frequency
freq(img_arr);
String path = "Constructed_from_matrix.jpg";
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Color newColor = new Color(img_arr[j][i], img_arr[j][i], img_arr[j][i]);
image.setRGB(j, i, newColor.getRGB());
}
}
// Writing converted image to file
File ouptut2 = new File(path);
ImageIO.write(image, "jpg", ouptut2);
} catch (Exception e) {
}
}
/**
* here frequency of the element of the array is calculated
*
* @param img_arr 2d array of the jpeg
*/
public int[] freq(int[][] img_arr) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
freq[img_arr[j][i]] += 1;
}
}
return freq;
}
}