/// <summary>
        /// Raises the destroy event.
        /// </summary>
        void OnDestroy()
        {
            if (reader != null)
            {
                reader.Dispose();
                reader = null;
            }

            if (sensor != null)
            {
                if (sensor.IsOpen)
                {
                    sensor.Close();
                }

                sensor = null;
            }

            if (texture != null)
            {
                Texture2D.Destroy(texture);
                texture = null;
            }
            if (rgbaMat != null)
            {
                rgbaMat.Dispose();
                rgbaMat = null;
            }
            if (comicFilter != null)
            {
                comicFilter.Dispose();
                comicFilter = null;
            }
        }
        void Start()
        {
            sensor = KinectSensor.GetDefault();

            if (sensor != null)
            {
                reader = sensor.ColorFrameSource.OpenReader();

                FrameDescription frameDesc = sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Rgba);


                texture = new Texture2D(frameDesc.Width, frameDesc.Height, TextureFormat.RGBA32, false);
                data    = new byte[frameDesc.BytesPerPixel * frameDesc.LengthInPixels];

                if (!sensor.IsOpen)
                {
                    sensor.Open();
                }


                rgbaMat = new Mat(texture.height, texture.width, CvType.CV_8UC4);

                gameObject.transform.localScale = new Vector3(texture.width, texture.height, 1);
                gameObject.GetComponent <Renderer>().material.mainTexture = texture;

                float width  = rgbaMat.width();
                float height = rgbaMat.height();

                float widthScale  = (float)Screen.width / width;
                float heightScale = (float)Screen.height / height;
                if (widthScale < heightScale)
                {
                    Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
                }
                else
                {
                    Camera.main.orthographicSize = height / 2;
                }



                // sepia
                sepiaKernel = new Mat(4, 4, CvType.CV_32F);
                sepiaKernel.put(0, 0, /* R */ 0.189f, 0.769f, 0.393f, 0f);
                sepiaKernel.put(1, 0, /* G */ 0.168f, 0.686f, 0.349f, 0f);
                sepiaKernel.put(2, 0, /* B */ 0.131f, 0.534f, 0.272f, 0f);
                sepiaKernel.put(3, 0, /* A */ 0.000f, 0.000f, 0.000f, 1f);


                // pixelize
                pixelizeIntermediateMat = new Mat();
                pixelizeSize0           = new Size();


                //comic
                comicFilter = new ComicFilter();
            }
            else
            {
                UnityEngine.Debug.LogError("No ready Kinect found!");
            }

            // Update GUI state
            filterTypeDropdown.value = (int)filterType;
        }
Example #3
0
        void Start()
        {
            sensor = KinectSensor.GetDefault();

            if (sensor != null)
            {
                coordinateMapper = sensor.CoordinateMapper;

                reader = sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.BodyIndex);

                FrameDescription colorFrameDesc = sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Rgba);
                texture          = new Texture2D(colorFrameDesc.Width, colorFrameDesc.Height, TextureFormat.RGBA32, false);
                colorData        = new byte[colorFrameDesc.BytesPerPixel * colorFrameDesc.LengthInPixels];
                colorFrameWidth  = colorFrameDesc.Width;
                colorFrameHeight = colorFrameDesc.Height;

                FrameDescription depthFrameDesc = sensor.DepthFrameSource.FrameDescription;
                depthData        = new ushort[depthFrameDesc.LengthInPixels];
                depthSpacePoints = new DepthSpacePoint[colorFrameDesc.LengthInPixels];
                depthFrameWidth  = depthFrameDesc.Width;
                depthFrameHeight = depthFrameDesc.Height;

                FrameDescription bodyIndexFrameDesc = sensor.BodyIndexFrameSource.FrameDescription;
                bodyIndexData = new byte[bodyIndexFrameDesc.BytesPerPixel * bodyIndexFrameDesc.LengthInPixels];


                if (!sensor.IsOpen)
                {
                    sensor.Open();
                }

                rgbaMat = new Mat(colorFrameDesc.Height, colorFrameDesc.Width, CvType.CV_8UC4);

                maskMat   = new Mat(rgbaMat.rows(), rgbaMat.cols(), CvType.CV_8UC1);
                outputMat = new Mat(rgbaMat.rows(), rgbaMat.cols(), CvType.CV_8UC4);

                maskData = new byte[rgbaMat.rows() * rgbaMat.cols()];

                gameObject.transform.localScale = new Vector3(texture.width, texture.height, 1);
                gameObject.GetComponent <Renderer>().material.mainTexture = texture;

                float width  = rgbaMat.width();
                float height = rgbaMat.height();

                float widthScale  = (float)Screen.width / width;
                float heightScale = (float)Screen.height / height;
                if (widthScale < heightScale)
                {
                    Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
                }
                else
                {
                    Camera.main.orthographicSize = height / 2;
                }



                // sepia
                sepiaKernel = new Mat(4, 4, CvType.CV_32F);
                sepiaKernel.put(0, 0, /* R */ 0.189f, 0.769f, 0.393f, 0f);
                sepiaKernel.put(1, 0, /* G */ 0.168f, 0.686f, 0.349f, 0f);
                sepiaKernel.put(2, 0, /* B */ 0.131f, 0.534f, 0.272f, 0f);
                sepiaKernel.put(3, 0, /* A */ 0.000f, 0.000f, 0.000f, 1f);


                // pixelize
                pixelizeIntermediateMat = new Mat();
                pixelizeSize0           = new Size();


                //comic
                comicFilter = new ComicFilter();
            }
            else
            {
                UnityEngine.Debug.LogError("No ready Kinect found!");
            }

            // Update GUI state
            filterTypeDropdown.value = (int)filterType;
        }