问题描述
背景:这是一个 Java 中级计算机科学课程的编码项目。总结提示要求我们从几个不同的源图片中获取每个像素的中值。我已经通过 SOF 浏览了几个 questions,但我仍然不太明白如何从几个不同的源图片中获取单个像素。
我正在努力编写一个使用函数从 jpg 图片中读取像素的函数。
到目前为止我所做的:我设法将图片加载到数组中的 256 位源中。
我的尝试:我尝试使用 for 循环遍历所有图像,但我不知道如何将它们传递给适当的函数并让每个源“img”BufferImage 相互通信。
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
int pixel = img.getRGB(i,j);
}
}
到目前为止我的代码:
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");
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)