如何同时计算三幅图像的像素数

时间:2014-03-08 09:23:58

标签: java

如何同时计算三幅图像的总像素? 我尝试使用java Swing Worker和Thread,我得到了答案,但需要1.30分钟才能返回指定范围的总像素数(每张图像30秒,图像大小480 * 303)。但我需要在30秒内同时获得三张图像的答案。

public class ImageProcessor1 implements Runnable{
static int blackPix=0;
BufferedImage tempImg;
public static int blackPixel=0;
public ImageProcessor1(String path) throws Exception{
tempImg = ImageIO.read(new File(path));
}

private static int[] getPixelData(BufferedImage img, int x, int y) {
    int argb = img.getRGB(x, y);

    int rgb[] = new int[]{
        (argb >> 16) & 0xff, //red
        (argb >> 8) & 0xff, //green
        (argb) & 0xff //blue
    };
    System.out.println("Process1 :rgb: " + rgb[0] + " " + rgb[1] + " " + rgb[2]);
    return rgb;
}

@Override
public void run() {
     int[][] pixelData = new int[tempImg.getHeight() * tempImg.getWidth()][3];
        int[] rgb;
        int height=tempImg.getHeight();
        int width=tempImg.getWidth();
        int counter = 0;
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                rgb = getPixelData(tempImg, i, j);
                if(rgb[0]<125 && rgb[0]>105 && rgb[1]<125 && rgb[1]>105 && rgb[2]<125 && rgb[2]>105)
                {
                    blackPixel+=1;
                }
            }
        }
}

}

1 个答案:

答案 0 :(得分:1)

非常奇怪的是,迭代这么小的图片需要30秒!

在进行一些分析之后,似乎在热循环中使用println语句会大大减慢你的速度。

在对代码进行少量修改后,我的机器上的10500x5788图像需要大约3秒。

修改后的版本:

package application;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

public class ImageProcessor1 implements Runnable {

  BufferedImage tempImg;
  public static int blackPixel = 0;

  public ImageProcessor1(final String path) throws Exception {
    final long start = System.nanoTime();

    tempImg = ImageIO.read(new File(path));
    // Use tracing, profiling and sampling to proof performance issues and fixes
    System.out.println("ImageIO took " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)
        + " ms.");
  }

  @Override
  public void run() {
    long start = System.nanoTime();
    final int height = tempImg.getHeight();
    System.out.println("Getting height '" + height + "' took "
        + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start) + " ms.");


    start = System.nanoTime();
    final int width = tempImg.getWidth();
    System.out.println("Getting width '" + width + "' took "
        + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start) + " ms.");

    start = System.nanoTime();

    // reuse variables
    int argb;
    int red;
    int green;
    int blue;

    for (int i = 0; i < width; i++) {
      for (int j = 0; j < height; j++) {
        // HOT LOOP. Do as little as possible. No println calls!
        argb = tempImg.getRGB(i, j);

        // inline all method calls
        red = argb >> 16 & 0xff; // red
        green = argb >> 8 & 0xff; // green
        blue = argb & 0xff; // blue

        if (red < 125 && red > 105 && green < 125 && green > 105 && blue < 125 && blue > 105) {
          blackPixel += 1;
        }
      }
    }
    System.out.println("Iterating pixels took "
        + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start) + " ms.");
  }

  public static void main(final String[] args) throws Exception {
    new ImageProcessor1("big.jpg").run();
    System.out.println("Number of blackpixels = " + blackPixel);
  }

}

更一般地说,您必须小心您的方法,因为您将所有图像读入RAM,然后进行处理。如果您同时使用3个或更多大图像,则可能会出现OutOfMemoryError。如果这成为问题,您可以将图像作为输入流读取,并且一次只处理图像的小缓冲区。

要了解如何做到这一点,请参阅http://imagej.nih.gov/ij/source/ij/io/ImageReader.java

要了解如何累积多个线程的输出,请参阅How write mulithreaded code and accumulate the output from all the threads in a single file