Speed up a calculation on idividual pixel values

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Speed up a calculation on idividual pixel values

leon.gellinger
Hello,

I have a 2D+t Stack of 1024x512x100 images. I want to calculate the product
of two pixels at a distance of x,y from each other, then, take the average
product of all pixel pairs with that distance. The result should be
presented in a 1024x512 image. My problem is that the multiplication takes
forever. Is there any way to speed things up?

Code below:

setBatchMode(1);
w = getWidth();
h = getHeight();
corArray=newArray(w*h);


for (x=0; x<w; x++) {
        for (y=0; y<h; y++) {
                IJ.log("Processing pixel " + x+", "+y );
                corArray[x+(h+y)]=meancorrforgivenpos(x, y);
        }
}

newImage("Readout noise_mean_xcorr.tif", "32-bit", w, h, 1);
for (xy=0; x<w; x++) {
        for (y=0; y<h; y++) {
        setPixel(x,y,corArray[x+(h+y)]);
        }
}

setBatchMode(0);
updateDisplay();

function meancorrforgivenpos(x,y) {
        meancor=0;
        for (i=0; i<(getWidth-x); i++) {
                for (j=0; j<(getHeight-y); j++) {
                        for(z=0; z<nSlices; z++) {
          setZCoordinate(z);
          meancor = meancor+getPixel(i, j)*getPixel(i+x,j+y);
        }
                }
        }
        return meancor/(x*y);
}

Reply | Threaded
Open this post in threaded view
|

Re: Speed up a calculation on idividual pixel values

Michael Ellis-2
Some thoughts:

1. Scripting in the ImageJ macro language is incredibly powerful but it is better suited to performing high level operations where you call upon building in routines to do the bulk of the bit twiddling work. So investigate whether it is possible to average off of some of the builtin routines for doing the actually pixel arithmetic.

2. If you must do the pixel arithmetic yourself, could meancorrforgivenpos() be written more efficiently by getting all the pixel values into an array rather than having to make a scripting functional call for every pixel?

3. If this is the algorithm and it has to be done this way, look into rewriting it as a Java plugIn

— Michael Ellis

> On 14 Oct 2016, at 10:58, leon.gellinger <[hidden email]> wrote:
>
> Hello,
>
> I have a 2D+t Stack of 1024x512x100 images. I want to calculate the product
> of two pixels at a distance of x,y from each other, then, take the average
> product of all pixel pairs with that distance. The result should be
> presented in a 1024x512 image. My problem is that the multiplication takes
> forever. Is there any way to speed things up?
>
> Code below:
>
> setBatchMode(1);
> w = getWidth();
> h = getHeight();
> corArray=newArray(w*h);
>
>
> for (x=0; x<w; x++) {
> for (y=0; y<h; y++) {
> IJ.log("Processing pixel " + x+", "+y );
> corArray[x+(h+y)]=meancorrforgivenpos(x, y);
> }
> }
>
> newImage("Readout noise_mean_xcorr.tif", "32-bit", w, h, 1);
> for (xy=0; x<w; x++) {
> for (y=0; y<h; y++) {
> setPixel(x,y,corArray[x+(h+y)]);
> }
> }
>
> setBatchMode(0);
> updateDisplay();
>
> function meancorrforgivenpos(x,y) {
> meancor=0;
> for (i=0; i<(getWidth-x); i++) {
> for (j=0; j<(getHeight-y); j++) {
> for(z=0; z<nSlices; z++) {
>         setZCoordinate(z);
>         meancor = meancor+getPixel(i, j)*getPixel(i+x,j+y);
>         }
> }
> }
> return meancor/(x*y);
> }
>
>
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Speed-up-a-calculation-on-idividual-pixel-values-tp5017382.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Speed up a calculation on idividual pixel values

Herbie
In reply to this post by leon.gellinger
Good day Leon,

what about shifting the two images with respect to each other (x, y) and
multiplying them by use of the "Image Calculator...". Then you can take
the average of the resulting image.

If you need the complete cross-correlation function, i.e. all possible
x,y-shifts I recommend using the power-spectral approach per "FD Math.."

HTH

Herbie

::::::::::::::::::::::::::::::::::::::::::::
Am 14.10.16 um 11:58 schrieb leon.gellinger:

> Hello,
>
> I have a 2D+t Stack of 1024x512x100 images. I want to calculate the product
> of two pixels at a distance of x,y from each other, then, take the average
> product of all pixel pairs with that distance. The result should be
> presented in a 1024x512 image. My problem is that the multiplication takes
> forever. Is there any way to speed things up?
>
> Code below:
>
> setBatchMode(1);
> w = getWidth();
> h = getHeight();
> corArray=newArray(w*h);
>
>
> for (x=0; x<w; x++) {
> for (y=0; y<h; y++) {
> IJ.log("Processing pixel " + x+", "+y );
> corArray[x+(h+y)]=meancorrforgivenpos(x, y);
> }
> }
>
> newImage("Readout noise_mean_xcorr.tif", "32-bit", w, h, 1);
> for (xy=0; x<w; x++) {
> for (y=0; y<h; y++) {
> setPixel(x,y,corArray[x+(h+y)]);
> }
> }
>
> setBatchMode(0);
> updateDisplay();
>
> function meancorrforgivenpos(x,y) {
> meancor=0;
> for (i=0; i<(getWidth-x); i++) {
> for (j=0; j<(getHeight-y); j++) {
> for(z=0; z<nSlices; z++) {
>           setZCoordinate(z);
>           meancor = meancor+getPixel(i, j)*getPixel(i+x,j+y);
>         }
> }
> }
> return meancor/(x*y);
> }
>
>
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Speed-up-a-calculation-on-idividual-pixel-values-tp5017382.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Speed up a calculation on idividual pixel values

leon.gellinger
In reply to this post by Michael Ellis-2
1. Okay, I did not know that. I will use the built-in function as much as I can.
Reply | Threaded
Open this post in threaded view
|

Re: Speed up a calculation on idividual pixel values

leon.gellinger
In reply to this post by Herbie
Good Day Herbie,

Yes, translation and multiplication is a great speed-up. This will do the trick.