-
Notifications
You must be signed in to change notification settings - Fork 0
/
PixImage.java
453 lines (413 loc) · 19.9 KB
/
PixImage.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import cs61b.Pixal;
/* PixImage.java */
/**
* The PixImage class represents an image, which is a rectangular grid of
* color pixels. Each pixel has red, green, and blue intensities in the range
* 0...255. Descriptions of the methods you must implement appear below.
* They include a constructor of the form
*
* public PixImage(int width, int height);
*
* that creates a black (zero intensity) image of the specified width and
* height. Pixels are numbered in the range (0...width - 1, 0...height - 1).
*
* All methods in this class must be implemented to complete Part I.
* See the README file accompanying this project for additional details.
*/
public class PixImage {
/**
* Define any variables associated with a PixImage object here. These
* variables MUST be private.
*/
private int width;
private int height;
public Pixal[][] coordinate;
/**
* PixImage() constructs an empty PixImage with a specified width and height.
* Every pixel has red, green, and blue intensities of zero (solid black).
*
* @param width the width of the image.
* @param height the height of the image.
*/
public PixImage(int width, int height) {
// Your solution here.
this.width=width;
this.height=height;
this.coordinate = new Pixal[width][height];
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
coordinate[i][j] = new Pixal();
}
}
}
/**
* getWidth() returns the width of the image.
*
* @return the width of the image.
*/
public int getWidth() {
// Replace the following line with your solution.
return width;
}
/**
* getHeight() returns the height of the image.
*
* @return the height of the image.
*/
public int getHeight() {
// Replace the following line with your solution.
return height;
}
/**
* getRed() returns the red intensity of the pixel at coordinate (x, y).
*
* @param x the x-coordinate of the pixel.
* @param y the y-coordinate of the pixel.
* @return the red intensity of the pixel at coordinate (x, y).
*/
public short getRed(int x, int y) {
// Replace the following line with your solution.
return coordinate[x][y].getRed();
}
/**
* getGreen() returns the green intensity of the pixel at coordinate (x, y).
*
* @param x the x-coordinate of the pixel.
* @param y the y-coordinate of the pixel.
* @return the green intensity of the pixel at coordinate (x, y).
*/
public short getGreen(int x, int y) {
// Replace the following line with your solution.
return coordinate[x][y].getGreen();
}
/**
* getBlue() returns the blue intensity of the pixel at coordinate (x, y).
*
* @param x the x-coordinate of the pixel.
* @param y the y-coordinate of the pixel.
* @return the blue intensity of the pixel at coordinate (x, y).
*/
public short getBlue(int x, int y) {
// Replace the following line with your solution.
return coordinate[x][y].getBlue();
}
/**
* setPixel() sets the pixel at coordinate (x, y) to specified red, green,
* and blue intensities.
*
* If any of the three color intensities is NOT in the range 0...255, then
* this method does NOT change any of the pixel intensities.
*
* @param x the x-coordinate of the pixel.
* @param y the y-coordinate of the pixel.
* @param red the new red intensity for the pixel at coordinate (x, y).
* @param green the new green intensity for the pixel at coordinate (x, y).
* @param blue the new blue intensity for the pixel at coordinate (x, y).
*/
public void setPixel(int x, int y, short red, short green, short blue) {
coordinate[x][y].setPixal(red, green, blue);
}
/**
* toString() returns a String representation of this PixImage.
*
* This method isn't required, but it should be very useful to you when
* you're debugging your code. It's up to you how you represent a PixImage
* as a String.
*
* @return a String representation of this PixImage.
*/
public void tooString() {
// Replace the following line with your solution.
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
System.out.print("("+coordinate[j][i].getRed()+","+coordinate[j][i].getGreen()+","+coordinate[j][i].getBlue()+") ");
if(j==width-1){
System.out.println("");
}
}
}
}
public String toString() {
String s ="";
for (int i=0;i<height;i++){
for(int j=0;j<width;j++){
s = s.concat(" | "+coordinate[j][i].getRed()+" "+coordinate[j][i].getGreen()+" "+coordinate[j][i].getBlue()+" |");
}
s = s.concat("\n");
}
return s;
}
/**
* boxBlur() returns a blurred version of "this" PixImage.
*
* If numIterations == 1, each pixel in the output PixImage is assigned
* a value equal to the average of its neighboring pixels in "this" PixImage,
* INCLUDING the pixel itself.
*
* A pixel not on the image boundary has nine neighbors--the pixel itself and
* the eight pixels surrounding it. A pixel on the boundary has six
* neighbors if it is not a corner pixel; only four neighbors if it is
* a corner pixel. The average of the neighbors is the sum of all the
* neighbor pixel values (including the pixel itself) divided by the number
* of neighbors, with non-integer quotients rounded toward zero (as Java does
* naturally when you divide two integers).
*
* Each color (red, green, blue) is blurred separately. The red input should
* have NO effect on the green or blue outputs, etc.
*
* The parameter numIterations specifies a number of repeated iterations of
* box blurring to perform. If numIterations is zero or negative, "this"
* PixImage is returned (not a copy). If numIterations is positive, the
* return value is a newly constructed PixImage.
*
* IMPORTANT: DO NOT CHANGE "this" PixImage!!! All blurring/changes should
* appear in the new, output PixImage only.
*
* @param numIterations the number of iterations of box blurring.
* @return a blurred version of "this" PixImage.
*/
public PixImage boxBlur(int numIterations) {
// Replace the following line with your solution.
if(numIterations==0||numIterations<0){
return this;
}else{
PixImage temp = new PixImage(this.width,this.height);
while(numIterations>0){
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
if(i==0&&j==0){
temp.setPixel(i, j, (short)((this.getRed(i, j)+this.getRed(i, j+1)+this.getRed(i+1, j)+this.getRed(i+1, j+1))/4), (short)((this.getGreen(i, j)+this.getGreen(i, j+1)+this.getGreen(i+1, j)+this.getGreen(i+1, j+1))/4), (short)((this.getBlue(i, j)+this.getBlue(i, j+1)+this.getBlue(i+1, j)+this.getBlue(i+1, j+1))/4));
}else if(i==0&&j==height-1){
temp.setPixel(i, j, (short)((this.getRed(i, j)+this.getRed(i, j-1)+this.getRed(i+1, j)+this.getRed(i+1, j-1))/4), (short)((this.getGreen(i, j)+this.getGreen(i, j-1)+this.getGreen(i+1, j)+this.getGreen(i+1, j-1))/4), (short)((this.getBlue(i, j)+this.getBlue(i, j-1)+this.getBlue(i+1, j)+this.getBlue(i+1, j-1))/4));
}else if(i==width-1&&j==0){
temp.setPixel(i, j, (short)((this.getRed(i, j)+this.getRed(i, j+1)+this.getRed(i-1, j)+this.getRed(i-1, j+1))/4), (short)((this.getGreen(i, j)+this.getGreen(i, j+1)+this.getGreen(i-1, j)+this.getGreen(i-1, j+1))/4), (short)((this.getBlue(i, j)+this.getBlue(i, j+1)+this.getBlue(i-1, j)+this.getBlue(i-1, j+1))/4));
}else if(i==width-1&&j==height-1){
temp.setPixel(i, j, (short)((this.getRed(i, j)+this.getRed(i, j-1)+this.getRed(i-1, j)+this.getRed(i-1, j-1))/4), (short)((this.getGreen(i, j)+this.getGreen(i, j-1)+this.getGreen(i-1, j)+this.getGreen(i-1, j-1))/4), (short)((this.getBlue(i, j)+this.getBlue(i, j-1)+this.getBlue(i-1, j)+this.getBlue(i-1, j-1))/4));
}else if(i==0){
temp.setPixel(i, j, (short)((this.getRed(i, j)+this.getRed(i, j+1)+this.getRed(i+1, j)+this.getRed(i+1, j+1)+this.getRed(i, j-1)+this.getRed(i+1, j-1))/6), (short)((this.getGreen(i, j)+this.getGreen(i, j+1)+this.getGreen(i+1, j)+this.getGreen(i+1, j+1)+this.getGreen(i, j-1)+this.getGreen(i+1, j-1))/6), (short)((this.getBlue(i, j)+this.getBlue(i, j+1)+this.getBlue(i+1, j)+this.getBlue(i+1, j+1)+this.getBlue(i, j-1)+this.getBlue(i+1, j-1))/6));
}else if(j==0){
temp.setPixel(i, j, (short)((this.getRed(i, j)+this.getRed(i, j+1)+this.getRed(i+1, j)+this.getRed(i+1, j+1)+this.getRed(i-1, j)+this.getRed(i-1, j+1))/6), (short)((this.getGreen(i, j)+this.getGreen(i, j+1)+this.getGreen(i+1, j)+this.getGreen(i+1, j+1)+this.getGreen(i-1, j)+this.getGreen(i-1, j+1))/6), (short)((this.getBlue(i, j)+this.getBlue(i, j+1)+this.getBlue(i+1, j)+this.getBlue(i+1, j+1)+this.getBlue(i-1, j)+this.getBlue(i-1, j+1))/6));
}else if(i==width-1){
temp.setPixel(i, j, (short)((this.getRed(i, j)+this.getRed(i, j+1)+this.getRed(i-1, j)+this.getRed(i-1, j+1)+this.getRed(i, j-1)+this.getRed(i-1, j-1))/6), (short)((this.getGreen(i, j)+this.getGreen(i, j+1)+this.getGreen(i-1, j)+this.getGreen(i-1, j+1)+this.getGreen(i, j-1)+this.getGreen(i-1, j-1))/6), (short)((this.getBlue(i, j)+this.getBlue(i, j+1)+this.getBlue(i-1, j)+this.getBlue(i-1, j+1)+this.getBlue(i, j-1)+this.getBlue(i-1, j-1))/6));
}else if(j==height-1){
temp.setPixel(i, j, (short)((this.getRed(i, j)+this.getRed(i, j-1)+this.getRed(i-1, j)+this.getRed(i-1, j-1)+this.getRed(i+1, j)+this.getRed(i+1, j-1))/6), (short)((this.getGreen(i, j)+this.getGreen(i, j-1)+this.getGreen(i-1, j)+this.getGreen(i-1, j-1)+this.getGreen(i+1, j)+this.getGreen(i+1, j-1))/6), (short)((this.getBlue(i, j)+this.getBlue(i, j-1)+this.getBlue(i-1, j)+this.getBlue(i-1, j-1)+this.getBlue(i+1, j)+this.getBlue(i+1, j-1))/6));
}else{
temp.setPixel(i, j, (short)((this.getRed(i, j)+this.getRed(i, j+1)+this.getRed(i, j-1)+this.getRed(i-1, j)+this.getRed(i-1, j-1)+this.getRed(i-1, j+1)+this.getRed(i+1, j)+this.getRed(i+1, j-1)+this.getRed(i+1, j+1))/9), (short)((this.getGreen(i, j)+this.getGreen(i, j+1)+this.getGreen(i, j-1)+this.getGreen(i-1, j)+this.getGreen(i-1, j-1)+this.getGreen(i-1, j+1)+this.getGreen(i+1, j)+this.getGreen(i+1, j-1)+this.getGreen(i+1, j+1))/9), (short)((this.getBlue(i, j)+this.getBlue(i, j+1)+this.getBlue(i, j-1)+this.getBlue(i-1, j)+this.getBlue(i-1, j-1)+this.getBlue(i-1, j+1)+this.getBlue(i+1, j)+this.getBlue(i+1, j-1)+this.getBlue(i+1, j+1))/9));
}
}
}
numIterations--;
return temp.boxBlur(numIterations);
}
}
return null;
}
/**
* mag2gray() maps an energy (squared vector magnitude) in the range
* 0...24,969,600 to a grayscale intensity in the range 0...255. The map
* is logarithmic, but shifted so that values of 5,080 and below map to zero.
*
* DO NOT CHANGE THIS METHOD. If you do, you will not be able to get the
* correct images and pass the autograder.
*
* @param mag the energy (squared vector magnitude) of the pixel whose
* intensity we want to compute.
* @return the intensity of the output pixel.
*/
private static short mag2gray(long mag) {
short intensity = (short) (30.0 * Math.log(1.0 + (double) mag) - 256.0);
// Make sure the returned intensity is in the range 0...255, regardless of
// the input value.
if (intensity < 0) {
intensity = 0;
} else if (intensity > 255) {
intensity = 255;
}
return intensity;
}
/**
* sobelEdges() applies the Sobel operator, identifying edges in "this"
* image. The Sobel operator computes a magnitude that represents how
* strong the edge is. We compute separate gradients for the red, blue, and
* green components at each pixel, then sum the squares of the three
* gradients at each pixel. We convert the squared magnitude at each pixel
* into a grayscale pixel intensity in the range 0...255 with the logarithmic
* mapping encoded in mag2gray(). The output is a grayscale PixImage whose
* pixel intensities reflect the strength of the edges.
*
* See http://en.wikipedia.org/wiki/Sobel_operator#Formulation for details.
*
* @return a grayscale PixImage representing the edges of the input image.
* Whiter pixels represent stronger edges.
*/
public PixImage sobelEdges() {
// Replace the following line with your solution.
// Don't forget to use the method mag2gray() above to convert energies to
// pixel intensities.
PixImage reflectImg = this.reflection();
//reflectImg.tooString();
PixImage temp = new PixImage(this.width,this.height);
for(int i=1;i<this.width+1;i++){
for(int j=1;j<this.height+1;j++){
short intensity = mag2gray(reflectImg.energy(i, j));
temp.setPixel(i-1, j-1, intensity, intensity, intensity);
}
}
//temp.tooString();
return temp;
}
public PixImage reflection(){
PixImage newPic = new PixImage(this.getWidth()+2,this.getHeight()+2);
for(int i=1;i<this.width+1;i++){
for(int j=0;j<this.height+2;j++){
if(j==0){
newPic.setPixel(i, j, this.getRed(i-1, j), this.getGreen(i-1, j), this.getBlue(i-1, j));
}else if(j==height+1){
newPic.setPixel(i, j, this.getRed(i-1, j-2), this.getGreen(i-1,j-2), this.getBlue(i-1, j-2));
}else{
newPic.setPixel(i, j, this.getRed(i-1, j-1), this.getGreen(i-1, j-1), this.getBlue(i-1, j-1));
}
}
}
for(int j=1;j<this.height+1;j++){
newPic.setPixel(0, j, this.getRed(0, j-1), this.getGreen(0, j-1), this.getBlue(0, j-1));
newPic.setPixel(this.width+1, j, this.getRed(this.width-1, j-1), this.getGreen(this.width-1, j-1), this.getBlue(this.width-1, j-1));
}
newPic.setPixel(0, 0, this.getRed(0, 0), this.getGreen(0, 0), this.getBlue(0, 0));
newPic.setPixel(0, this.height+1, this.getRed(0, this.height-1), this.getGreen(0, this.height-1), this.getBlue(0, this.height-1));
newPic.setPixel(this.width+1, 0, this.getRed(this.width-1, 0), this.getGreen(this.width-1, 0), this.getBlue(this.width-1, 0));
newPic.setPixel(this.width+1, this.height+1, this.getRed(this.width-1, this.height-1), this.getGreen(this.width-1, this.height-1), this.getBlue(this.width-1, this.height-1));
return newPic;
}
public long energy(int x, int y){
int redX = this.getRed(x-1, y-1)-this.getRed(x+1, y-1)+2*this.getRed(x-1, y)-2*this.getRed(x+1, y)+this.getRed(x-1, y+1)-this.getRed(x+1, y+1);
//System.out.println("redX is "+redX);
int greenX = this.getGreen(x-1, y-1)-this.getGreen(x+1, y-1)+2*this.getGreen(x-1, y)-2*this.getGreen(x+1, y)+this.getGreen(x-1, y+1)-this.getGreen(x+1, y+1);
int blueX = this.getBlue(x-1, y-1)-this.getBlue(x+1, y-1)+2*this.getBlue(x-1, y)-2*this.getBlue(x+1, y)+this.getBlue(x-1, y+1)-this.getBlue(x+1, y+1);
int redY = this.getRed(x-1,y-1)+2*this.getRed(x, y-1)+this.getRed(x+1, y-1)-this.getRed(x-1, y+1)-2*this.getRed(x, y+1)-this.getRed(x+1, y+1);
//System.out.println("redY is "+redY);
int greenY = this.getGreen(x-1,y-1)+2*this.getGreen(x, y-1)+this.getGreen(x+1, y-1)-this.getGreen(x-1, y+1)-2*this.getGreen(x, y+1)-this.getGreen(x+1, y+1);
int blueY = this.getBlue(x-1,y-1)+2*this.getBlue(x, y-1)+this.getBlue(x+1, y-1)-this.getBlue(x-1, y+1)-2*this.getBlue(x, y+1)-this.getBlue(x+1, y+1);
long sum = redX*redX+redY*redY+greenX*greenX+greenY*greenY+blueX*blueX+blueY*blueY;
//System.out.println("energy of ("+x+", "+y+") is "+sum);
return sum;
}
/**
* TEST CODE: YOU DO NOT NEED TO FILL IN ANY METHODS BELOW THIS POINT.
* You are welcome to add tests, though. Methods below this point will not
* be tested. This is not the autograder, which will be provided separately.
*/
/**
* doTest() checks whether the condition is true and prints the given error
* message if it is not.
*
* @param b the condition to check.
* @param msg the error message to print if the condition is false.
*/
private static void doTest(boolean b, String msg) {
if (b) {
System.out.println("Good.");
} else {
System.err.println(msg);
}
}
/**
* array2PixImage() converts a 2D array of grayscale intensities to
* a grayscale PixImage.
*
* @param pixels a 2D array of grayscale intensities in the range 0...255.
* @return a new PixImage whose red, green, and blue values are equal to
* the input grayscale intensities.
*/
private static PixImage array2PixImage(int[][] pixels) {
int width = pixels.length;
int height = pixels[0].length;
PixImage image = new PixImage(width, height);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setPixel(x, y, (short) pixels[x][y], (short) pixels[x][y],
(short) pixels[x][y]);
}
}
return image;
}
/**
* equals() checks whether two images are the same, i.e. have the same
* dimensions and pixels.
*
* @param image a PixImage to compare with "this" PixImage.
* @return true if the specified PixImage is identical to "this" PixImage.
*/
public boolean equals(PixImage image) {
int width = getWidth();
int height = getHeight();
if (image == null ||
width != image.getWidth() || height != image.getHeight()) {
return false;
}
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (! (getRed(x, y) == image.getRed(x, y) &&
getGreen(x, y) == image.getGreen(x, y) &&
getBlue(x, y) == image.getBlue(x, y))) {
return false;
}
}
}
return true;
}
/**
* main() runs a series of tests to ensure that the convolutions (box blur
* and Sobel) are correct.
*/
public static void main(String[] args) {
// Be forwarned that when you write arrays directly in Java as below,
// each "row" of text is a column of your image--the numbers get
// transposed.
PixImage image1 = array2PixImage(new int[][] { { 0, 10, 240 },
{ 30, 120, 250 },
{ 80, 250, 255 } });
System.out.println("Testing getWidth/getHeight on a 3x3 image. " +
"Input image:");
System.out.print(image1);
doTest(image1.getWidth() == 3 && image1.getHeight() == 3,
"Incorrect image width and height.");
System.out.println("Testing blurring on a 3x3 image.");
doTest(image1.boxBlur(1).equals(
array2PixImage(new int[][] { { 40, 108, 155 },
{ 81, 137, 187 },
{ 120, 164, 218 } })),
"Incorrect box blur (1 rep):\n" + image1.boxBlur(1));
doTest(image1.boxBlur(2).equals(
array2PixImage(new int[][] { { 91, 118, 146 },
{ 108, 134, 161 },
{ 125, 151, 176 } })),
"Incorrect box blur (2 rep):\n" + image1.boxBlur(2));
doTest(image1.boxBlur(2).equals(image1.boxBlur(1).boxBlur(1)),
"Incorrect box blur (1 rep + 1 rep):\n" +
image1.boxBlur(2) + image1.boxBlur(1).boxBlur(1));
System.out.println("Testing edge detection on a 3x3 image.");
doTest(image1.sobelEdges().equals(
array2PixImage(new int[][] { { 104, 189, 180 },
{ 160, 193, 157 },
{ 166, 178, 96 } })),
"Incorrect Sobel:\n" + image1.sobelEdges());
PixImage image2 = array2PixImage(new int[][] { { 0, 100, 100 },
{ 0, 0, 100 } });
System.out.println("Testing getWidth/getHeight on a 2x3 image. " +
"Input image:");
System.out.print(image2);
doTest(image2.getWidth() == 2 && image2.getHeight() == 3,
"Incorrect image width and height.");
System.out.println("Testing blurring on a 2x3 image.");
doTest(image2.boxBlur(1).equals(
array2PixImage(new int[][] { { 25, 50, 75 },
{ 25, 50, 75 } })),
"Incorrect box blur (1 rep):\n" + image2.boxBlur(1));
System.out.println("Testing edge detection on a 2x3 image.");
doTest(image2.sobelEdges().equals(
array2PixImage(new int[][] { { 122, 143, 74 },
{ 74, 143, 122 } })),
"Incorrect Sobel:\n" + image2.sobelEdges());
}
}