- Home /
How do I use web camera values of certain pixels as input?
I am working with C# and MonoDevelop, and wish to use certain hue values from the web camera feed as user input. To be exact; I have an 16x16 grid, and want to check for the colors red, green, blue and yellow in each of the squares of the grid, this corresponding to such colored blocks that the user will place in front of the camera in these positions. So far I have been successful in adding the camera-feed to a WebCamTexture, but I need a way of accessing the hue values in certain coordinates of it. Any ideas, and the more specific the better, are welcome.
Update: I have tried using the WebCamTexture.GetPixel to access the pixel values, using the code below:
using UnityEngine;
using System.Collections;
public class CamTexture : MonoBehaviour {
// Use this for initialization
void Start () {
WebCamTexture webcam = new WebCamTexture();
renderer.material.mainTexture = webcam;
webcam.Play();
Debug.Log (webcam.GetPixel(100,100));
}
// Update is called once per frame
void Update () {
}
void OnGUI () {
}
}
But it seems like it is impossible to activate the WebCamTexture anywhere outside of the Start() function, while the GetPixel() function cannot access the webcam texture from outside of the same function it is created in; meaning that I cannot access the pixel values at regular intervals, such as events from the Update() or OnGUI(). As always, any help appreciated.
Answer by Xalemich · Oct 15, 2014 at 03:07 AM
After a good nights sleep, which I apparently needed very much, I realized the painfully simple solution; initialize the WebCamTexture outside of the Start() function, and presto, everything worked as I expected. I'll leave the code here if any rookie might find it useful:
using UnityEngine;
using System.Collections;
public class CamTexture : MonoBehaviour {
WebCamTexture webcam = new WebCamTexture();
// Use this for initialization
void Start () {
renderer.material.mainTexture = webcam;
webcam.Play();
}
// Update is called once per frame
void Update () {
}
void OnGUI () {
Debug.Log (webcam.GetPixel(100,100));
}
}
Your answer
Follow this Question
Related Questions
Best way to Camera Pick multiple objects and move them together 2 Answers
Multiple Cars not working 1 Answer
Smooth transition from Camera1 to Camera2? 1 Answer
Distribute terrain in zones 3 Answers
Unassigned Reference Exception 4 Answers