From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Image Moment - Optimizing Code for Speed

Solved!
Go to solution

Hello Smiley Happy

 

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:

Wikipedia - Image Moments

 

Image Moment.png

 

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?  Smiley Embarassed

 

 

 

 

Thank you and kudos will be given! Smiley Surprised

 

 

--------------------------------
The Enrichment Center is required to remind you that you will be baked, and then there will be cake.

0 Kudos
Message 1 of 27
(4,721 Views)

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

Antoine Chalons

Message 2 of 27
(4,713 Views)

Hello 

 

--------------------------------
The Enrichment Center is required to remind you that you will be baked, and then there will be cake.

0 Kudos
Message 3 of 27
(4,707 Views)

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!

 

Smiley Tongue

--------------------------------
The Enrichment Center is required to remind you that you will be baked, and then there will be cake.

0 Kudos
Message 4 of 27
(4,668 Views)

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).

Message 5 of 27
(4,660 Views)
Solution
Accepted by topic author SectorEffector

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.

 

Image Moment.png

Message 6 of 27
(4,658 Views)

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.

 

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 7 of 27
(4,655 Views)

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.

--------------------------------
The Enrichment Center is required to remind you that you will be baked, and then there will be cake.

0 Kudos
Message 8 of 27
(4,624 Views)

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?

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 9 of 27
(4,583 Views)

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.

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 10 of 27
(4,580 Views)