- Home /
Accessing values in Color32 or convert to Byte array
I am trying to access pixel R, G and B values in a rather large web camera feed, and was led towards the path of using the .GetPixels32, since this was supposed to be the fastest option, and indeed it seems so. Now I just need to access the individual values, in order to measure them against certain limits, E.G. is this red enough to be the previously introduced red object? But I can't seem to figure out how to access the individual values (a,b,g,r), (or as it says in the debug: RGBA).
What I know about the GetPixels32: http://docs.unity3d.com/ScriptReference/WebCamTexture.GetPixels32.html
What I know about the Color32: http://docs.unity3d.com/ScriptReference/Color32.html
Currently I am trying to feed the RGB values into an array, which is 16 columns wide, 16 rows high, and has 3 values (the colors):
using UnityEngine;
using System.Collections;
public class CamTexture : MonoBehaviour {
public byte[,,] colorArr = new byte[16,16,3];
public static WebCamTexture webcam;
private Color32[] ImgMatrix;
void Start (){
webcam = new WebCamTexture();
renderer.material.mainTexture = webcam;
webcam.Play();
ImgMatrix = new Color32[webcam.width * webcam.height];
webcam.GetPixels32(ImgMatrix);
//This is a desperate and vague attempt at feeding any value into colorArr...
colorArr[0,0,0] = ImgMatrix[0];
}
}
Any, also different ways of approaching the problem, ways of doing this would be amazing. The values are there; the Debug.Log shows the values just fine when I feed it the ImgMatrix[0], i just don't see how to get at them...
Answer by Xalemich · Oct 16, 2014 at 12:53 PM
Right, in my way too common "Why didn't I think of that before!?" manner, I realized what needed to be done: ImgMatrix[0].r, or col[0].r, returns the red value of the color32 struct. And so on and so forth with g, b and a. Hope this helps someone who's also stuck at this point get over it faster than I did.
Answer by KayelGee · Oct 16, 2014 at 06:28 AM
I don't quite understand what you're trying to do with that byte array.
If you're only trying to access the color values of pixels at position x,y then the solution is quite simple. Your ImgMatrix is an array of Color structs so it has all the information you need. To get the Color struct at position x,y:
Color32 col = ImgMatrix[y*webcam.width + x];
Why does this work? Let's say our camera has 3 lines and looks like this:
AAAAAAAAAA
BBBBBBBBBB
CCCCCCCCCC
Each character is one pixel on our cam. This two dimensonal image is actually stored as one continuous array which looks like this:
AAAAAAAAAABBBBBBBBBBCCCCCCCCCC
And that's your ImgMatrix array.
Indeed that is the way I understood the Img$$anonymous$$atrix (sorry for the misna$$anonymous$$g there, named that before I understood it was a single-dimensional structure), but I need to access the RGB values individually; to follow your example, each position in the Color32 col has an R, G, B and A (alpha, not your ABC version), value, but I don't know how to access these, only the positions. I want to be able access the R value of col[12], for instance.
Thanks for the reply $$anonymous$$ayelGee
I thought you knew that because you posted the documentation of Color32 which point's that out :D
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Change Color in C# with RGB Values? 4 Answers
Flip over an object (smooth transition) 3 Answers