java - 在具有多个线程的图像上应用中值
问题描述
对于学校作业,我们必须对嘈杂的 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));
}
在我们的 main 中,我们有一个整数,它定义了我们拥有的线程数量。我们的想法和任务是我们将整个图像的高度分成几部分,让多个线程计算我们的中值。
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
}
希望这可以帮助 !
推荐阅读
- javascript - 反应变量不更新
- next.js - 如何在 nextjs 中获取上一个 url
- c# - 我如何检查 DateTime 是否为空
- javascript - 如何在运行一组 mocha 测试之前读取文件
- mysql - mysql:通过在季度报告中使用 date_created 和 DATE_FORMAT 或 Quarter() 函数进行分组
- javascript - 如何使用 node.js 读取名词文本文件并将其存储在变量中
- python - 将matplotlib映射保存到python中的shapefile?
- hive - Hive 中 Parquet 和 Avro 文件格式的模式演变的含义是什么
- amazon-web-services - How to view the ec2 instance limits on aws
- python-3.x - 如何对 csv 文件中的一行或一列的值求和?