1 Reply Latest reply on Aug 30, 2011 2:06 PM by unique_screenname_here

    How to calculate average of all pixels?

    wilecoyote42 Level 1

      Hello:

       

      I just started playing around with Pixel Bender a few weeks ago, and  I had an idea for a filter: take a given image, calculate an  average of all its pixels, and replace the image by a solid color  representing that average. The process, as I imagine it, would be  (pseudocode):

       

       

      var total=0;
      foreach pixel {
         calculate-average-of-RGB-channels;
         total += average;
        }
      totalAverage = total / numPixels;

       

       

      The problem is that I can't figure out how to do this. The  evaluatePixel() function isn't the place, since it processes all the  pixels in the image in parallel, and what I need is to calculate one by  one the values of all pixels and accumulate them in a variable, before  outputting the final image. The only way I've thought of is to do the  following in a different function before, and pass the average to  evaluatePixel():

       

       

      Find-out-x-and-y-size-of-image
      for (x=0; x < sizeX; x++) {
          for (y=0; y < sizeY; y++) {
            Calculate average
            }
         }

       

      The question is... do I actually have to run this loop by myself, "by  hand"? Or does Pixel Bender have some other facility to iterate through  all the pixels automatically? (Like evaluatePixel(), but allowing you to  accumulate data in an external variable that doesn't get reset to 0  every time the function is called).

       

       

      Thanks in advance.

        • 1. Re: How to calculate average of all pixels?
          unique_screenname_here Level 3

          This will be hard to do in Pixel Bender as Pixel Bender filters are intended to be run for every pixel in the output as you pointed out. Consequently, you would compute the average of all pixels for every pixel which would not be performance-friendly. We provide evaluateDependents as a means to do computations that will occur once per frame. Unfortunately, you cannot sample in evaluateDependents. Getting the exact image boundaries is also somewhat problematic. Your filters is a good idea but it will be difficult to implement in Pixel Bender at this point in time.

          1 person found this helpful