Sunday, 27 January 2013

Sexy Unity Web Camera manipulator tutorial

Sexy Unity Web Camera Manipulator Tutorial ( Kinect OpenCV like )


As all of us I’am very lazy guy. So I have created as simple way to use my manipulator as it possible. If you get my sources. You can get it by taking part in some simple contest.

Anyway the package consist of 2 scripts:
KDManipulato.cs – do all dirty jobs for us
ManipulatorManager.cs – retrieve and setup manipulator

So we need to look deeper on ManipulatorManager:
using UnityEngine;
using System.Collections;

public class ManipulatorManager : MonoBehaviour {
                private WebCamTexture webcamTexture;
                private KDManipulator theManipulator;
                private float prevX;
                private float prevY;
                private float prevZ;
                // Use this for initialization
                void Start () {
                                webcamTexture = new WebCamTexture();       
                                webcamTexture.requestedFPS = 30;
                                webcamTexture.requestedWidth = 320;
                                webcamTexture.requestedHeight = 240;
                                theManipulator = new KDManipulator(webcamTexture.width, webcamTexture.height );                            
                // Update is called once per frame
                void FixedUpdate () {
                                theManipulator.ProcessImage( ref webcamTexture );
                                // Now get coordinates
                                float x = ( theManipulator.GetMinX(theManipulator.BiggestAreaID) + theManipulator.GetMaxX(theManipulator.BiggestAreaID)) * 0.0015625f; //Normalized coeff
        float y = 1f - (( theManipulator.GetMinY(theManipulator.BiggestAreaID) + theManipulator.GetMaxY(theManipulator.BiggestAreaID)) * 0.00208333333333333333333333333333f); //Normalized coeff
        float z = theManipulator.GetArea(theManipulator.BiggestAreaID) * 1.3020833333333333333333333333333e-5f; // Normalize on 320x240 area
                                x = ( x + prevX ) / 2f;
                                y = ( y + prevY ) / 2f;
                                z = ( z + prevZ ) / 2f;
                                transform.position = new Vector3(x,y,z);                            
                                prevX = x;
                                prevY = y;
                                prevZ = z;

theManipulator = new KDManipulator(webcamTexture.width, webcamTexture.height ); - Yes it is a dimension of a texture that manipulator will process. I have tooked 320x240, cuz larger textures produces lags and blabla. I also have overrode a constructor with minimum color filter and maximum color filter. By default it works with red markers.

So whats going on in FixedUpdate(). We process every fixed time a frame and retrieve coordinates. I just want to say that normalize coefficients will produce values [0..1] instead of 320x240 etc. Same on Z value. And previous coordinates is working like a some error correction of marker position.

You need to attach this script to an object. Or you can use it directly with small modificaiton. It works fine on Android, so should work on iPad/iPhone. Please don't ask me how it works on it, cuz i simply have no money for iPad, iPhone or Android.

If you have any questions how to use it. Please ask me.

And don't forget to achieve best result you should look like this:

konstantin dvornik