Prompt: Pastebin Picture Sources: Link2
Background: This is a coding project for an intermediate computer science class in Java. The prompt in summary asks us to take the median of each pixels from several different source pictures. I've looked through several questions over SOF, but I still don't quite understand how to take individual pixels from several different source pictures.
I'm struggling to write a function that read pixels from a jpg picture using functions.
What I've done so far: I've managed to load pictures into 256bits source in arrays.
My attempt: I've tried using for loops to loop through all the images, but I don't know how to pass them into a proper function and let each source "img" BufferImage communicate with each other.
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
int pixel = img.getRGB(i, j);
}
}
My code so far:
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.plaf.basic.BasicComboBoxUI.FocusHandler;
import java.io.*;
import java.awt.event.*;
public class MedianFilter {
BufferedImage img[];
private File[] files;
private BufferedImage filteredImage;
public MedianFilter(String array[]) {
files = new File[array.length];
img = new BufferedImage[array.length];
for (int i = 0; i < array.length; i++) {
img[i] = readImage(new File(array[i]));
}
filteredImage = readImage(new File(array[0]));
}
private BufferedImage readImage(File imageFile) {// opens and reads in an image file
BufferedImage img = null;
try {
if (!imageFile.exists())
throw new IOException("Can't locate picture");
else
img = ImageIO.read(imageFile);
} catch (IOException ie) {
System.out.println(ie.getMessage());
}
return img;
}
public BufferedImage removeNoise() {
for (int i = 0; i < filteredImage.getWidth(); i++) {
for (int j = 0; j < filteredImage.getHeight(); j++) {
filteredImage = getMedianValue();
}
}
int p = img.getRGB(x, y);
int trans = (p >> 24) & 0xff;
int red = (p >> 16) & 0xff;
int green = (p >> 8) & 0xff;
int blue = p & 0xff;
return filteredImage;
} // gets the median value for all pixels and returns the filtered noiseless image
public int getMedianValue(ArrayList pixels) {
} // returns the median value of the pixel(x,y) for all images
public int writeImage(String outputFilename) {
File output;
File f2 = new File(outputFilename);
ImageIO.write(img, "jpg", f2); // write to file
} // writes filteredImage to the outputFilename jpg file. Returns 0 if successful,
// or -1 if an exception was thrown.
public int getHeight(BufferedImage bimg) {
int width = bimg.getWidth();
int height = bimg.getHeight();
} // returns height (y-dimension) of filteredImage
public int getWidth(BufferedImage bimg) {
} // returns width (x-dimension) of filteredImage
public static void main(String[] args) {
String[] vegs = { "vegs1.jpg", "vegs2.jpg", "vegs3.jpg", "vegs4.jpg", "vegs5.jpg", "vegs6.jpg", "vegs7.jpg",
"vegs8.jpg", };
MedianFilter process = new MedianFilter(vegs);
process.removeNoise();
process.writeImage("output.jpg");
}
}