My assignment is to write a monochrome (black and white) fog filter to change an image so it looks like it was taken on a foggy day. To do this, change every value in the image to the average of the value itself, the values next to it (4 or all 8) and a random value from 0 to 255 ((short)(Math.random() * 256) will get you the random number). My teacher doesn’t respond on weekends for help.
So here’s where I’m at:
I’ve improved my code because it makes the image black and white. But I thought that it would standout more then black and white. I just want to make sure that I am atleast doing what the assignment asked.
The teacher has designed a few of his own classes for this assignment and an Interface
ImageProvider class that provides the image (a .jpg picture)
java.lang.Object
extended byjava.awt.Component
extended byimagelab.ImgProvider
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable
ImageLab class
java.lang.Object
extended byimagelab.ImageLab
ImageLab is a platform for image filter development. ImageLab begins by building a menu of all available filters (those .class files that implement the ImageFilter interface @see ImageFilter).
Interface ImageFilter
public interface ImageFilter
The ImageFilter interface is implemented by the respective image filters.
Implementation Detail The format of the program
When writing a filter you should indicate that the filter is a member of the filter package by placing the line:
package filters; at the top of the .java file.
So your filter can find the ImageFilter interface, you should put the line:
import imagelab.*; as the second line in the file.
To compile your filter (assume its called MyFilter.java), go to the parent directory (the directory that has imagelab and filters as subdirectories) and type:
javac filters/MyFilter.java
To run imageLab, you should be in the same directory and type:
java imagelab.ImageLab
package filters;
import imagelab.*;
public class Monochrome implements ImageFilter {
ImgProvider filteredImage;
public void filter (ImgProvider ip) {
short[][] mono = ip.getBWImage();//Convert the picture to B&W
short[][] mono2 = new short[mono.length][mono[0].length];
/**Go through both arrays and change every value in the image to the average of the value itself, the values next to it (4 or all 8) and a random value from 0 to 255
*/
for (int r = 1; r<mono.length-1; r++) {
for (int c = 1; c < mono[0].length-1; c++) {
int val = mono[r][c];
val += ((short)Math.random()*256); //random number
val += mono[r-1][c];
val += mono[r+1][c];
val += mono[r][c-1];
val += mono[r][c+1];
val /= 5;
val = (val < 0) ? -val : val;
if (val > 255) val = 255;
mono2[r][c] = (short)((val < 0) ? -val : val);
}//for c
}//for r;
filteredImage = new ImgProvider(); //create the new .jpg image
filteredImage.setBWImage(mono2); //set it to B&W using the monochrome settings
filteredImage.showPix("Monochrome of original image"); //label the image
}//filter
public ImgProvider getImgProvider() {
return filteredImage;
}//getImgProvider
public String getMenuLabel() {
return "Monochrome";
} //getMenuLabel
}
You state you need to
"change every value in the image to the average of the value itself, the values next to it (4 or all 8) and a random value from 0 to 255"You’re currently not doing anything like that; you’re assigning a randomly chosen location to your current location as you go through the arrays.
Your 2 loops to go through the entire 2D array are fine, but you need to get the values of those surrounding the one you’re currently at.
Example: If you’re at
mono[0][0]then you need to getmono[0][1],mono[1][0], andmono[1][1]. Add those together along with your current location’s value and the random number then divide by5to get the average.You’ll note I picked a corner for the example. Depending on where you are in the 2D array you could have 3 (you’re in a corner), 5 (you’re on the border but not in a corner) , or 8 (you’re in the interior) surrounding values; I don’t know where
4came from in your description.