Login  Register

Re: Help with computing correlations along z in stacks.

Posted by Michael Schmid on Feb 27, 2015; 11:08am
URL: http://imagej.273.s1.nabble.com/Re-IMAGEJ-Digest-25-Feb-2015-to-26-Feb-2015-2015-67-tp5011805p5011804.html

Hi Thomas,

if you don't use a macro to loop over all pixels of an image or the like, there won't be a significant difference in performance. Just don't do any time-consuming operations in a macro.
E.g., an FFT is not any slower when called from a macro than when called from Java code.

To avoid the overhead of displaying temporary images in a macro, use BatchMode.

Michael
________________________________________________________________
On Feb 27, 2015, at 12:03, Thomas Julou wrote:

> Hi Michael,
>
> Thank you very much for taking time to look at my question. What you propose is very close to what I’d be able to come up with by myself. I posted on this mailing list specifically because my impression was that this macro-like approach would yield very low performance. And so far my experience was that it’s difficult to incrementally improve code written in this way…
>
> So let’s rephrase my question: should I expect a large jump in performance between a macro-like plugin and calling appropriate APIs directly? If yes, which ones? (cf my previous post…)
>
> Best,
> Thomas
>
>
> Date:    Thu, 26 Feb 2015 17:37:19 +0100
> From:    Michael Schmid <[hidden email]>
> Subject: Re: Help with computing correlations along z in stacks.
>
>> Hi Thomas,
>>
>> unfortunately I don't know matlab, but it seems to my that you can it in ImageJ without very much programming, i.e., in a macro:
>> - Image>Stacks>Reslice to have the z direction run in x or y.
>> - Convert to float and subtract the background value, so that the background gets a value of 0
>> - Pad the canvas size to a square that is a power of 2.
>> - Create a 32-bit image of the same size with you function centered in the middle, running in the same x or y direction as the original z direction, only in the middle line.
>> - For all stack slices, run Process>Math>FD Math and correlate (with inverse transform) the images of the resliced stack with the image having the function.
>> - Combine the output images to a stack, and reslice to the original orientation if desired.
>>
>> If you want to do programming in Java, you could also use the built-in 1D FHT (fast Hartley transform) of ImageJ.
>>  https://github.com/imagej/imagej1/blob/master/ij/process/FHT.java
>> Correlation is simply multiplying the FHT arrays index by index, then transform back (inverseTransform1D).
>>
>>
>> Michael
>> ________________________________________________________________
>>
>>
>> On Feb 26, 2015, at 12:14, Thomas Julou wrote:
>>
>>> Hello,
>>>
>>> I hope this list is the right place to ask my question.
>>> In order to achieve robust segmentation of bacteria from white light pictures, we use a "correlation" image. The principle is to acquire a z-stack of bright field pictures, and to correlate the intensity along z with an « expected" intensity profile. So far, we wrote the code in Matlab only.
>>>
>>> However for several purpose, I’d be interested in having the code written for imageJ (among other, doing the acquisition with micro-manager and processing images on the fly; though I know I could call MM from Matlab). Since I'm new to plug-in programming (but anticipate that speed will matter in this case), I'd really appreciate getting pointers to appropriate libraries / examples.
>>>
>>> Here are a few specific questions:
>>> - is it better to run a FFT per (x, y) intensity profile or is there any kind of "vectorized syntax"?
>>> - which fft library do you recommend? fftw? imglib2?
>>> - is it realistic to write this for cuda / opencl already? (to me it looks like a good task for a gpu)?
>>>
>>> Below is the code I wrote for matlab since it might help to answer.
>>> Thank you in advance for your help. Best,
>>>
>>> Thomas
>>>
>>> --
>>> Thomas Julou  |  Computational & Systems Biology  |  Biozentrum – University of Basel  |  Klingelbergstrasse 50/70 CH-4056 Basel  |  +41 (0)61 267 16 21
>>>
>>>
>>>> function IM_CORREL = correlImg(IM, sigma)
>>>> [ny, nx, nz] = size(IM);
>>>> z = (1:nz)';
>>>> center = round(nz/2);
>>>>
>>>> zProfile = - (z-center) .* exp(- (z-center).^2 / (2*sigma^2));
>>>> zProfileFFT = fft(zProfile);
>>>> zProfileFFT(1) = 0;
>>>>
>>>> zProfileFFTc = conj(zProfileFFT);
>>>> Z_PROFILE_FFT_C(1,1,:) = zProfileFFTc;
>>>> Z_PROFILE_FFT_C = repmat(Z_PROFILE_FFT_C, [ny nx]);
>>>>
>>>> IM_Z_FFT = fft(IM, [], 3);
>>>> IM_Z_FFT(:,:,1) = 0;
>>>> CORREL = real(ifft(IM_Z_FFT.*Z_PROFILE_FFT_C, [], 3));
>>>>
>>>> idx = [center+1:nz 1:center];
>>>> IM_CORREL = CORREL(:,:,idx);
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html