0

Hello I need to access every pixels of an ImagePlus for image analysis.

Because of the huge amount of images to process, I was wondering if there are special effective ways/methods to access and/or modify each pixel from an imagePlus? The only idea I naturally come out with is double for-looping through the image matrix, which takes me several dozens of seconds to achieve on a 1000x1000 image. Here is my code:

ImagePlus Iorg = IJ.openImage("Demo1.png");
int[] pix = Iorg.getPixel(5, 5);
if(Iorg.getSlice() != 1) {
    System.exit(0);
}

for(int w=0; w< Iorg.getDimensions()[0]; w++) {
    for(int h=0; h<Iorg.getDimensions()[1]; h++) {
       System.out.println(w + " x " + h);
       // DO what needs to be done      
    }
}

Any idea?

kaligne
  • 3,098
  • 9
  • 34
  • 60
  • 1) `System.out.print` will take so much time. 2. Try to use library methods rather than for loops. 3) What are you trying to do inside `DO what needs to be done`? – smttsp Apr 17 '14 at 13:39
  • For now I would like to divide every pixel by the maximum intensity, which is 255. Indeed I removed System.out.print and it now takes 1 second to loop through the whole matrix :P – kaligne Apr 17 '14 at 14:08
  • For division you could use `Iorg.getProcessor().multiply()` to do the math without looping. I think you better have the image be 32bit float before doing this. – Kota Apr 17 '14 at 14:21

1 Answers1

0

Since images are uchar, what you want to do is the equivalent of

if(selected_pixel==255) 
     selected_pixel = 1; 
else 
     selected_pixel = 0 

You can create mask, that would be easier. I don't know in java ImagePlus but in matlab it is mask = image==255;.

Try to use those kind of matrix operations according to your need. I'm sure these methods should be somewhere inside the library(if ImagePlus is image processing library.)

smttsp
  • 4,011
  • 3
  • 33
  • 62