在具有多个线程的图像上应用中位数



对于学校作业,我们必须对嘈杂的PNG图像应用中值过滤器。 我有以下几点:

public static void main(String[] a) throws Throwable {
final String extension = "png";
final String input = "input1.png";
final String output = "output1.png";
final int threadsAmount = 4;
long startTime = System.currentTimeMillis();
final Image image = new Image(input, output, extension);
image.applyMedian(threadsAmount);
long duration = System.currentTimeMillis() - startTime;
System.out.println("duration: " + duration);
}

一种将中值过滤器应用于图像的方法,创建缓冲图像,读取图像,计算平均值并将它们放入导出到新图像的新缓冲图像中。

public void applyMedian(int threadAmount) throws IOException, Exception {
File imageFile = new File(this.input);
BufferedImage bufferedImage = ImageIO.read(imageFile);
int imageHeight = bufferedImage.getHeight();
int imageWidth = bufferedImage.getWidth();
BufferedImage finalImage = new BufferedImage(imageWidth, imageHeight, bufferedImage.getType());
int heightPerThread = imageHeight / threadAmount;
Color[] surroundedPixel = new Color[9];
int[] R = new int[9];
int[] B = new int[9];
int[] G = new int[9];
for (int j = 1; j < bufferedImage.getHeight() - 1; j++) {
for (int i = 1; i < bufferedImage.getWidth() - 1; i++) {
surroundedPixel[0] = new Color(bufferedImage.getRGB(i - 1, j - 1));
surroundedPixel[1] = new Color(bufferedImage.getRGB(i - 1, j));
surroundedPixel[2] = new Color(bufferedImage.getRGB(i - 1, j + 1));
surroundedPixel[3] = new Color(bufferedImage.getRGB(i, j + 1));
surroundedPixel[4] = new Color(bufferedImage.getRGB(i + 1, j + 1));
surroundedPixel[5] = new Color(bufferedImage.getRGB(i + 1, j));
surroundedPixel[6] = new Color(bufferedImage.getRGB(i + 1, j - 1));
surroundedPixel[7] = new Color(bufferedImage.getRGB(i, j - 1));
surroundedPixel[8] = new Color(bufferedImage.getRGB(i, j));
for (int k = 0; k < 9; k++) {
R[k] = surroundedPixel[k].getRed();
B[k] = surroundedPixel[k].getBlue();
G[k] = surroundedPixel[k].getGreen();
}
Arrays.sort(R);
Arrays.sort(G);
Arrays.sort(B);
finalImage.setRGB(i, j, new Color(R[4], B[4], G[4]).getRGB());
}
}
ImageIO.write(finalImage, this.getExtension(), new File(this.output));
}

在我们的主中,我们有一个整数来定义我们拥有的线程数量。我们的想法和任务是,我们将整个图像的长度分成几部分,让多个线程计算我们的中位数。

int heightPerThread = imageHeight / threadAmount;

但是,我们已经学会了如何创建线程,但我们根本不知道如何在上面的方法上应用所有这些线程,并让所有线程处理它们自己的图像部分。

希望有人能帮忙。

我认为这将需要您添加一个可以从applyMedian调用的附加函数。附加功能可以获取左上角像素位置(i, j)以及螺纹应处理的高度和宽度。你可以让它看起来像这样:

public static void main(String[] a) throws Throwable {
final String extension = "png";
final String input = "input1.png";
final String output = "output1.png";
final int threadsAmount = 4;
long startTime = System.currentTimeMillis();
final Image image = new Image(input, output, extension);
image.applyMedian(threadsAmount);
long duration = System.currentTimeMillis() - startTime;
System.out.println("duration: " + duration);
}

并且您的applyMedian应修改为:

public void applyMedian(int threadAmount) throws IOException, Exception {
File imageFile = new File(this.input);
BufferedImage bufferedImage = ImageIO.read(imageFile);
int imageHeight = bufferedImage.getHeight();
int imageWidth = bufferedImage.getWidth();
int heightPerThread = imageHeight / threadAmount;
int widthPerThread = imageWidth / threadAmount
BufferedImage finalImage = new BufferedImage(imageWidth, imageHeight, bufferedImage.getType());
// create threads here
// make all threads invoke the new function here
}

你的新函数应该将finalImage对象,左上角位置,heightPerThread和widthPerThread作为参数,并执行你现在在applyMedian 本身中所做的处理。

public newFunction(Image finalImage, int i, int j, int heightPerThread, int widthPerThread) {
// do all processing here
// call finalImage.setRGB
}

希望这有帮助!

最新更新