0

I am writing a program which which will divide an image into domains and ranges. In this domain will be 16X16 matrix. I am using PixelGrabber to acquire the pixels. I have used for loop for the iteration but it is taking so much time to process.In java any alternate is there to reduce this time. I need a better performance. Whether we can use any collections for that?

My code is:

int[] pix = new int[16 * 16];
long start = System.nanoTime();
for (int i = 0; i < 512; i += 16) 
{
 for (int j = 0; j < 512; j += 16) 
 {
   PixelGrabber pg = new PixelGrabber(Im, i, j, 16, 16, pix, 0, 16);
   pg.grabPixels();
   makeStack(pix, i, j);
 }
}
long end = System.nanoTime();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • If I thought this through correctly both for loops together run a shocking 262144 times. How about trying out to let those loops run parallel (multiple threads)? Don't forget to use a data structure that supports parallel writing though. In the best case your working time could be reduced close to a minimum of SINGLETHREADINGTIME/AMOUNTOFTHREADS! – ASA Jan 09 '14 at 10:42
  • 1
    What is `Im`? What is `PixelGrabber`? – Aaron Digulla Jan 09 '14 at 10:48
  • Why not `BufferedImage#getRGB(int, int)`? –  Jan 09 '14 at 10:51
  • @kadaj: Getting single pixel values is a pretty slow operation. – Aaron Digulla Jan 09 '14 at 10:53
  • 1
    Use byte array instead. `byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();` http://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image –  Jan 09 '14 at 10:53

1 Answers1

1

Your problem right now is that you don't know why it's slow. Yes, it's somewhere in the code above but you can't tell us where exactly.

You should really run the code using a profiler to see where it spends most of the time.

That said, for image processing, it's usually most useful to convert the whole image into a byte[] or int[] array and then process that directly.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820