01-31-2013 07:12 AM
Hello
I'm want to find the moment of inertia of an 2d-array. The array is converted from an image using "IMAQ ImageToArray".
The algorithm I'm using is discribed here:
I need to calculate this formula with different values for i and j:
i.j = 0.0 - 0.1 - 1.0 - 1.1 - 0.2 - 2.0
I programmed the code shown above/attached VI, but I need the optimize it for speed.
The 2d array can be any size with a maximum of 2048 x 2048 with values varying between 0 and 4095.
My question:
How can I make this code faster?
Thank you and kudos will be given!
Solved! Go to Solution.
01-31-2013 07:30 AM
I think if you want good performance you should try to avoid the "Image to Array" and see if your processing doesn't exist in the IMAQ functions, open the Vission concepts manual in C:\Program Files\National Instruments\Vision\Documentation and search for moment the fews hits might be of interest to you.
Hope this helps
We have two ears and one mouth so that we can listen twice as much as we speak.
Epictetus
01-31-2013 07:43 AM
Hello Titou,
Thank you for your reply.
The Vision palette only has mathematical functions for binary images (0 to 1 instead of 0 to 4095).
The "IMAQ Particle Analysis" vi is promising and has functions for finding the orientation and moment of inertia,
but only for binary particles.
The only Vision VI that makes use of the weight of the pixels is the "IMAQ Centroid" VI.
01-31-2013 02:45 PM
Is there someone who can help me optimize this code?
It is to slow for our application at the moment and I would be vey thankful for every optimize!
01-31-2013 03:07 PM
Looking at the code I would try the following:
combine all of the operations that involve the transposed arrays into a single for loop. same for the original arrays. (ie. combine M10 and M20 loops as well as M01 and M02 loops)
parallelize the for loops (only the outer one for M11).
01-31-2013 03:09 PM
You can speed it up about 60% just by combining some of your loops -- the calculation of M01 and M02, and M10 and M20, both share some computation.
01-31-2013 03:12 PM
If you are looking at moments, the IMAQ particle analysis will do moments on particles all in imaq to avoide the slower image to array.
As for optomizing your code a few observations
1. why are you using doubles- your image is 12 (actually 16bit in imaq)
2. do nor calculate intedex on each itteration pre calculate these and cache.
3. when possible do calculation on arrays at a time, ie multiple 2 arrays instead of doing it in a loop.
02-01-2013 01:21 AM
Hello falkpl,
" If you are looking at moments, the IMAQ particle analysis will do moments on particles all in imaq to avoide the slower image to array.
As for optomizing your code a few observations
1. why are you using doubles- your image is 12 (actually 16bit in imaq)
2. do nor calculate intedex on each itteration pre calculate these and cache.
3. when possible do calculation on arrays at a time, ie multiple 2 arrays instead of doing it in a loop. "
Thank you for your reply, sir. As stated before, the "IMAQ Particle Analysis" only calculates moments on non-weighted (e.g. binary) particles.
The formula above includes the weight of each pixel.
1. The doubles are because the image is first filtered. This filter needs to convert the image to the DBL Type.
2. Could you please elaborate this, sir? I do not understand what you mean.
3. Effectively done in the solution. Thank you.
02-01-2013 03:54 PM
You state your images are 0-4096- this is a 12 bit number, you are converting this to a double (64bit numbers), I assumed your IMAQ image was either a I 16 or U16, is this correct?
02-01-2013 04:10 PM
2. You could make a constant array of indicies 0....n, then you can multiply the original array by a second array instead of doing each element inside of a loop. also, parallel for loops can sometimes make large differences in execution time.