Break WebcamTexture into grid, then determine average brightness of each grid 'square'

I have an idea for a installation using a webcam that would require me to first
1-break a live webcam image into a grid
2- determine the average brightness value of each one of these grid squares.

This is obviously a coding problem, and need some tips, suggestions and even pseudo code as to how I might solve steps 1 & 2 above.

To break a webcamTexture into a grid, would I first need to convert it into a 2D texture?

And , once I have this grid, how would I determine the average brightness of each grid square?
The end result, as I see it, would be say for a coarse 10x10 grid, a vector containing 100 brightness values that would be updated in real time. I am using C#.

Haven’t worked with webcams in Unity3D before but from what I can see, you don’t need to use 2D Texture as you can just call:

Color[] pixels = WebCamTexture.GetPixels();

and work on result.
You can also use an overload of GetPixels() where you can choose which part of full texture you choose. That should help you out with creating a grid, since it allows you to get chunks of texture if you provide start point and block size.
Link to API documentation: Unity - Scripting API: WebCamTexture.GetPixels

Also, take in mind that you should take only few pixel rows from each cell so that it doesn’t kill performance.
For example if one grid cell is 300x300 pixel size, I’d take only one row per 100 pixels as it would probably be still representative for whole cell. For each row just take an average brightness, which in RGB (UnityEngine.Color uses RGB color model) would be the highest value of red, green or blue (either one having value of 255 would mean it has 100% brighness (value) in HSV model).

1 Like

A shader running on the graphics card may be able to do this better then your cpu. Maybe, I have no idea when it comes to shaders.

1 Like

yes. this increasingly is what makes the most sense.
Came across this shader code in another thread
http://forum.unity3d.com/threads/making-a-local-pixelation-image-effect-shader.183210/