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

时间:2018-05-25 10:00:22

标签: java arrays multithreading image-processing bufferedimage

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

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));
}

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

int heightPerThread = imageHeight / threadAmount;

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

    <ion-col class = "mystyle"   (click) ="wajid()" >
 <ion-label id = "aa"   > اَبَدًا </ion-label>   

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

希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:0)

我认为这需要您添加一个可以从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
}

希望这有帮助!