Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by eco_bach, Nov 21, 2015.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    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#.
     
  2. btft

    btft

    Joined:
    Aug 11, 2015
    Posts:
    14
    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:
    Code (CSharp):
    1. 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: http://docs.unity3d.com/ScriptReference/WebCamTexture.GetPixels.html

    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).
     
    Last edited: Nov 22, 2015
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    eco_bach likes this.
  4. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601