-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageProcessing.java
More file actions
281 lines (271 loc) · 10.6 KB
/
ImageProcessing.java
File metadata and controls
281 lines (271 loc) · 10.6 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.Random;
import javax.imageio.ImageIO;
public class ImageProcessing {
public static void main(String[] args) {
// The provided images are apple.jpg, flower.jpg, and kitten.jpg
int[][] imageData = imgToTwoD("./apple.jpg");
// Or load your own image using a URL!
//int[][] imageData = imgToTwoD("https://content.codecademy.com/projects/project_thumbnails/phaser/bug-dodger.png");
//viewImageData(imageData);
// trimming image
int[][] trimmed = trimBorders(imageData, 60);
twoDToImage(trimmed, "./trimmed_apple.jpg");
// calling inverted method
int[][] inverted = negativeColor(imgToTwoD("./kitten.jpg"));
twoDToImage(inverted, "./inverted_kitten.jpg");
// calling stretching horizontally method
int[][] stretchedHoriz = stretchHorizontally(imgToTwoD("./flower.jpg"));
twoDToImage(stretchedHoriz, "./stretched_horiz_flower.jpg");
// calling shrinking vertically method
int[][] shrinkVert = shrinkVertically(imgToTwoD("./apple.jpg"));
twoDToImage(shrinkVert, "./shrink_apple.jpg");
// calling inverted image method
int[][] upsideDown = invertImage(imgToTwoD("./kitten.jpg"));
twoDToImage(upsideDown, "./upside_down_kitten.jpg");
// calling filter method
int[][] filtered = colorFilter(imgToTwoD("./flower.jpg"), -75, 80, -45);
twoDToImage(filtered, "./filtered_flower.jpg");
// calling painting a random image method
int[][] blankImg = new int[500][500];
int[][] randColors = paintRandomImage(blankImg);
twoDToImage(randColors, "./random_image.jpg");
// calling painting a rectangle method
int[] rgba = {255, 255, 0, 255};
int[][] img = imgToTwoD("./apple.jpg");
int[][] paintRect = paintRectangle(img, 200, 200, 100, 100, getColorIntValFromRGBA(rgba));
twoDToImage(paintRect, "./rect_image.jpg");
// calling generate # of rectangles method
int [][] generateRect = generateRectangles(imgToTwoD("./kitten.jpg"), 1000);
twoDToImage(generateRect, "./rectangle_kitten.jpg");
// int[][] allFilters = stretchHorizontally(shrinkVertically(colorFilter(negativeColor(trimBorders(invertImage(imageData), 50)), 200, 20, 40)));
// Painting with pixels
}
// Image Processing Methods
public static int[][] trimBorders(int[][] imageTwoD, int pixelCount) {
// Example Method
if (imageTwoD.length > pixelCount * 2 && imageTwoD[0].length > pixelCount * 2) {
int[][] trimmedImg = new int[imageTwoD.length - pixelCount * 2][imageTwoD[0].length - pixelCount * 2];
for (int i = 0; i < trimmedImg.length; i++) {
for (int j = 0; j < trimmedImg[i].length; j++) {
trimmedImg[i][j] = imageTwoD[i + pixelCount][j + pixelCount];
}
}
return trimmedImg;
} else {
System.out.println("Cannot trim that many pixels from the given image.");
return imageTwoD;
}
}
public static int[][] negativeColor(int[][] imageTwoD) {
int[][] new2DImage = new int[imageTwoD.length][imageTwoD[0].length];
for (int i = 0; i < imageTwoD.length; i++) {
for (int j = 0; j < imageTwoD[i].length; j++) {
// get RGBA value
int[] rgba = getRGBAFromPixel(imageTwoD[i][j]);
// invert colours
rgba[0] = 255 - rgba[0];
rgba[1] = 255 - rgba[1];
rgba[2] = 255 - rgba[2];
// set new 2DImage to that inverted colour
new2DImage[i][j] = getColorIntValFromRGBA(rgba);
}
}
// return inverted image
return new2DImage;
}
public static int[][] stretchHorizontally(int[][] imageTwoD) {
int[][] stretchedImage = new int[imageTwoD.length][imageTwoD[0].length*2];
// for width
int doubleCol = 0;
for (int i = 0; i < imageTwoD.length; i++) {
for (int j = 0; j < imageTwoD[i].length; j++) {
doubleCol = j * 2;
// copy current pixel over
stretchedImage[i][doubleCol] = imageTwoD[i][j];
// copy current pixel over again
stretchedImage[i][doubleCol+1] = imageTwoD[i][j];
}
}
return stretchedImage;
}
public static int[][] shrinkVertically(int[][] imageTwoD) {
int[][] shrinkedImage = new int[imageTwoD.length/2][imageTwoD[0].length];
for (int i = 0; i < imageTwoD[0].length; i++) {
for (int j = 0; j < imageTwoD.length-1; j+=2) {
// shrinks the y-axis or columns
shrinkedImage[j/2][i] = imageTwoD[j][i];
}
}
return shrinkedImage;
}
public static int[][] invertImage(int[][] imageTwoD) {
int[][] invertedImage = new int[imageTwoD.length][imageTwoD[0].length];
for (int i = 0; i < imageTwoD.length; i++) {
for (int j = 0; j < imageTwoD[i].length; j++) {
// changes the pixels at i and j to be the opposite (symmetrical on the diagonal)
invertedImage[i][j] = imageTwoD[(imageTwoD.length-1)-i][(imageTwoD[i].length-1)-j];
}
}
return invertedImage;
}
public static int[][] colorFilter(int[][] imageTwoD, int redChangeValue, int greenChangeValue, int blueChangeValue) {
int[][] filteredImage = new int[imageTwoD.length][imageTwoD[0].length];
for (int i = 0; i < imageTwoD.length; i++) {
for (int j = 0; j < imageTwoD[i].length; j++) {
// get pixel data in int array form
int[] rgba = getRGBAFromPixel(imageTwoD[i][j]);
// stored changes in array for convenience
int[] change = {redChangeValue, greenChangeValue, blueChangeValue};
// change each color in rgba and make sure they're in range
for (int k = 0; k < 3; k++) {
rgba[k] += change[k];
if (rgba[k] > 255) rgba[k] = 255;
else if (rgba[k] < 0) rgba[k] = 0;
}
// storing new pixel data in new array
filteredImage[i][j] = getColorIntValFromRGBA(rgba);
}
}
return filteredImage;
}
// Painting Methods
public static int[][] paintRandomImage(int[][] canvas) {
// Random object class
Random rand = new Random();
for (int i = 0; i < canvas.length; i++) {
for (int j = 0; j < canvas[i].length; j++) {
// create 3 random numbers
int rand1 = rand.nextInt(256);
int rand2 = rand.nextInt(256);
int rand3 = rand.nextInt(256);
// put these new rgb values in an int array
int[] rgbaValues = {rand1, rand2, rand3, 255};
// store it in the original image
canvas[i][j] = getColorIntValFromRGBA(rgbaValues);
}
}
return canvas;
}
public static int[][] paintRectangle(int[][] canvas, int width, int height, int rowPosition, int colPosition, int color) {
for (int i = 0; i < canvas.length; i++) {
for (int j = 0; j < canvas[i].length; j++) {
// check if the pixel is painted in this area with boundaries of width
if (i >= rowPosition && i <= rowPosition + width) {
// check if the pixel is painted in this area with boundaries of height
if (j >= colPosition && j <= colPosition + height) {
canvas[i][j] = color;
}
}
}
}
return canvas;
}
public static int[][] generateRectangles(int[][] canvas, int numRectangles) {
Random rand = new Random();
for (int i = 0; i < numRectangles; i++) {
// randomize width and height
int width = rand.nextInt(canvas[0].length);
int height = rand.nextInt(canvas.length);
// randomize position
int rowPos = rand.nextInt(canvas.length - height);
int colPos = rand.nextInt(canvas[0].length - width);
// create a random color
int rand1 = rand.nextInt(256);
int rand2 = rand.nextInt(256);
int rand3 = rand.nextInt(256);
// put these new rgb values in an int array
int[] rgbaValues = {rand1, rand2, rand3, 255};
// gets color in int form
int randColor = getColorIntValFromRGBA(rgbaValues);
// overwrite the input image
canvas = paintRectangle(canvas, width, height, rowPos, colPos, randColor);
}
return canvas;
}
// Utility Methods
public static int[][] imgToTwoD(String inputFileOrLink) {
try {
BufferedImage image = null;
if (inputFileOrLink.substring(0, 4).toLowerCase().equals("http")) {
URL imageUrl = new URL(inputFileOrLink);
image = ImageIO.read(imageUrl);
if (image == null) {
System.out.println("Failed to get image from provided URL.");
}
} else {
image = ImageIO.read(new File(inputFileOrLink));
}
int imgRows = image.getHeight();
int imgCols = image.getWidth();
int[][] pixelData = new int[imgRows][imgCols];
for (int i = 0; i < imgRows; i++) {
for (int j = 0; j < imgCols; j++) {
pixelData[i][j] = image.getRGB(j, i);
}
}
return pixelData;
} catch (Exception e) {
System.out.println("Failed to load image: " + e.getLocalizedMessage());
return null;
}
}
public static void twoDToImage(int[][] imgData, String fileName) {
try {
int imgRows = imgData.length;
int imgCols = imgData[0].length;
BufferedImage result = new BufferedImage(imgCols, imgRows, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < imgRows; i++) {
for (int j = 0; j < imgCols; j++) {
result.setRGB(j, i, imgData[i][j]);
}
}
File output = new File(fileName);
ImageIO.write(result, "jpg", output);
} catch (Exception e) {
System.out.println("Failed to save image: " + e.getLocalizedMessage());
}
}
public static int[] getRGBAFromPixel(int pixelColorValue) {
Color pixelColor = new Color(pixelColorValue);
return new int[] { pixelColor.getRed(), pixelColor.getGreen(), pixelColor.getBlue(), pixelColor.getAlpha() };
}
public static int getColorIntValFromRGBA(int[] colorData) {
if (colorData.length == 4) {
Color color = new Color(colorData[0], colorData[1], colorData[2], colorData[3]);
return color.getRGB();
} else {
System.out.println("Incorrect number of elements in RGBA array.");
return -1;
}
}
public static void viewImageData(int[][] imageTwoD) {
if (imageTwoD.length > 3 && imageTwoD[0].length > 3) {
int[][] rawPixels = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
rawPixels[i][j] = imageTwoD[i][j];
}
}
System.out.println("Raw pixel data from the top left corner.");
System.out.print(Arrays.deepToString(rawPixels).replace("],", "],\n") + "\n");
int[][][] rgbPixels = new int[3][3][4];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
rgbPixels[i][j] = getRGBAFromPixel(imageTwoD[i][j]);
}
}
System.out.println();
System.out.println("Extracted RGBA pixel data from top the left corner.");
for (int[][] row : rgbPixels) {
System.out.print(Arrays.deepToString(row) + System.lineSeparator());
}
} else {
System.out.println("The image is not large enough to extract 9 pixels from the top left corner");
}
}
}