# Machine Vision

cancel
Showing results for
Did you mean:

## How to calculate the sumof image pixels?

Hi

I would like to calculate the sum of pixels of an image. Here is my code:

I would like to ask if it exists another faster algorithm for sum calculation in labview. It seems that using C++ or Matlab this algorithm executes faster. In fact I would like to compare images according to their sum of pixels.

Message 1 of 7
(4,351 Views)

## Re: How to calculate the sumof image pixels?

The method you have demonstrated is probably the most inefficient.

In labview, you can just add the images together, or convert the images to arrays, and add the arrays together.  There is no need to go into for loops.

Machine Vision, Robotics, Embedded Systems, Surveillance

www.movimed.com - Custom Imaging Solutions
Message 2 of 7
(4,344 Views)

## Re: How to calculate the sumof image pixels?

The simplest method is to use IMAQ Quantify.  It will give you the average intensity.  If you really want the sum, just multiply the average by the number of pixels.

Bruce

Bruce Ammons
Ammons Engineering
Message 3 of 7
(4,341 Views)

## Re: How to calculate the sumof image pixels?

Hi,

Attached is a VI that shows different ways of doing computing the sum of the pixels of an image, with their respective benchmarks.

John is right. The fastest way is to use IMAQ Image to Array, cast the 2D array to double, then call the LabVIEW primitive Add Array Elements.

I's also like to point out a relatively new feature that we released about a year ago, which allows to access the image data in place, using the LabVIEW concept of Data Value Reference. There are VIs is Vision Development module that you can use to get a Data Value Reference of an image. When connected to an inplace structure, this allows you to get access to the pixels of the image directly, without having to make a copy (like it's the case with IMAQ Image to Array).

See example attached.

Hope this helps.

Best regards,

Christophe

Message 4 of 7
(4,336 Views)

## Re: How to calculate the sumof image pixels?

Christophe's VI is useful, but one thing you shouldn't do is create an array and then convert the whole array to DBL, especially if your images can be large.  A better, and even faster, approach is to sum the U8 array into a DBL shift register like this:

Really, this idea should be implemented, and then it wouldn't even be an issue.

Message 5 of 7
(4,277 Views)

## Re: How to calculate the sumof image pixels?

Thanks, Greg.

You might get even better performances by enabling the outside loop iteration parallelism. (Right click on the For loop, then Enable Loop iteration parallelism).

Message 6 of 7
(4,272 Views)

## Re: How to calculate the sumof image pixels?

Great solutions!

The only thing to note is that you cannot use parallel iterations because of the shift registers (using shift registers make one iteration depend on the previous one).

Andrew Valko
National Instruments Hungary
Message 7 of 7
(4,254 Views)