Beispiel #1
0
        /// <summary>
        /// 距離データをDepthImagePixel列に変換する
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        public static DepthImagePixel[] ToDepthImagePixel(this DepthImageFrame depthFrame)
        {
            var pixels = new DepthImagePixel[depthFrame.PixelDataLength];

            depthFrame.CopyDepthImagePixelDataTo(pixels);
            return(pixels);
        }
Beispiel #2
0
        /// <summary>
        /// Event handler for Kinect sensor's DepthFrameReady event
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    // Copy the pixel data from the image to a temporary array
                    depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);

                    // Get the min and max reliable depth for the current frame
                    int minDepth = depthFrame.MinDepth;
                    int maxDepth = depthFrame.MaxDepth;

                    // Convert the depth to RGB
                    int colorPixelIndex = 0;
                    for (int i = 0; i < this.depthPixels.Length; ++i)
                    {
                        // Get the depth for this pixel
                        short depth = depthPixels[i].Depth;

                        // To convert to a byte, we're discarding the most-significant
                        // rather than least-significant bits.
                        // We're preserving detail, although the intensity will "wrap."
                        // Values outside the reliable depth range are mapped to 0 (black).

                        // Note: Using conditionals in this loop could degrade performance.
                        // Consider using a lookup table instead when writing production code.
                        // See the KinectDepthViewer class used by the KinectExplorer sample
                        // for a lookup table example.

                        if (depth > 0)
                        {
                            int x = 1;
                        }

                        byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);

                        // Write out blue byte
                        this.colorPixels[colorPixelIndex++] = intensity;

                        // Write out green byte
                        this.colorPixels[colorPixelIndex++] = intensity;

                        // Write out red byte
                        this.colorPixels[colorPixelIndex++] = intensity;

                        // We're outputting BGR, the last byte in the 32 bits is unused so skip it
                        // If we were outputting BGRA, we would write alpha here.
                        ++colorPixelIndex;
                    }

                    // Write the pixel data into our bitmap
                    this.colorBitmap.WritePixels(
                        new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                        this.colorPixels,
                        this.colorBitmap.PixelWidth * sizeof(int),
                        0);
                }
            }
        }
        void mykinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            #region 基本彩色影像與深度影像處理
            depthframe = e.OpenDepthImageFrame();
            colorframe = e.OpenColorImageFrame();

            if (depthframe == null || colorframe == null)
            {
                return;
            }

            depthpixelData = new short[depthframe.PixelDataLength];
            depthframe.CopyPixelDataTo(depthpixelData);
            _DepthImageBitmap.WritePixels(_DepthImageBitmapRect, depthpixelData, _DepthImageStride, 0);
            depthPixel = new DepthImagePixel[depthframe.PixelDataLength];
            depthframe.CopyDepthImagePixelDataTo(depthPixel);

            colorpixelData = new byte[colorframe.PixelDataLength];
            colorframe.CopyPixelDataTo(colorpixelData);
            #endregion

            if (depthpixelData != null)
            {
                PlayerFilter();
                Alarm();
            }

            _ColorImageBitmap.WritePixels(_ColorImageBitmapRect, colorpixelData, _ColorImageStride, 0);
            depthframe.Dispose();
            colorframe.Dispose();
        }
        /**
         * Metoda handler pentru evenimentele create
         * de captarea imaginilor prin senzorii de adancime.
         **/
        void DepthImageReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            bool receiveData = false;

            using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame())
            {
                if (depthImageFrame != null)
                {
                    if (pixelDataDepth == null)
                    {
                        pixelDataDepth = new byte[depthImageFrame.PixelDataLength];
                    }
                    depthImageFrame.CopyDepthImagePixelDataTo(this.depthPixels);
                    receiveData = true;
                }
                else
                {
                    //nu s-au primit date
                }
                if (receiveData)
                {
                    image2.Source = depthImageFrame.ToBitmapSource();
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Event handler for Kinect sensor's DepthFrameReady event
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void SensorAllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            // in the middle of shutting down, so nothing to do
            if (null == this.sensor)
            {
                return;
            }

            bool depthReceived = false;
            bool colorReceived = false;

            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (null != depthFrame)
                {
                    // Copy the pixel data from the image to a temporary array
                    depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);
                    depthReceived = true;
                }
            }

            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (null != colorFrame)
                {
                    // Copy the pixel data from the image to a temporary array
                    colorFrame.CopyPixelDataTo(this.colorPixels);
                    colorReceived = true;
                }
            }

            // do our processing outside of the using block
            // so that we return resources to the kinect as soon as possible
            if ((true == depthReceived) && (true == colorReceived))
            {
                this.sensor.CoordinateMapper.MapDepthFrameToColorFrame(
                    DepthFormat,
                    this.depthPixels,
                    ColorFormat,
                    this.colorCoordinates);

                //draw the WritableBitmap
                // colorBitmap.WritePixels(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight),
                // bitMapBits,
                // colorBitmap.PixelWidth * sizeof(int), 0);
                //                    this.mappedImage.Source = bitMap;}}
//                this. = colorBitmap;
                // do our processing outside of the using block
                // so that we return resources to the kinect as soon as possible
                if (true == colorReceived)
                {
                    // Write the pixel data into our bitmap
                    this.colorBitmap.WritePixels(
                        new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                        this.colorPixels,
                        this.colorBitmap.PixelWidth * sizeof(int),
                        0);
                }
            }
        }
Beispiel #6
0
        private Bitmap CreateColorBitmapFromDepth(DepthImageFrame frame)
        {
            //WriteableBitmap colorBitmap;
            DepthImagePixel[] depthPixels;
            byte[]            colorPixels;

            depthPixels = new DepthImagePixel[kSensor.DepthStream.FramePixelDataLength];
            colorPixels = new byte[kSensor.DepthStream.FramePixelDataLength * sizeof(int)];

            frame.CopyDepthImagePixelDataTo(depthPixels);

            //get min and max reliable depth
            int minDepth = frame.MinDepth;
            int maxDepth = frame.MaxDepth;

            //convert depth to RGB
            int colorPixelIndex = 0;

            for (int i = 0; i < depthPixels.Length; ++i)
            {
                //get depth for this pixel
                short depth = depthPixels[i].Depth;

                // To convert to a byte, we're discarding the most-significant
                // rather than least-significant bits.
                // We're preserving detail, although the intensity will "wrap."
                // Values outside the reliable depth range are mapped to 0 (black).

                // Note: Using conditionals in this loop could degrade performance.
                // Consider using a lookup table instead when writing production code.
                // See the KinectDepthViewer class used by the KinectExplorer sample
                // for a lookup table example.

                byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);

                //write out blue byte
                colorPixels[colorPixelIndex++] = intensity;

                //write out green byte
                colorPixels[colorPixelIndex++] = intensity;

                //write out red byte
                colorPixels[colorPixelIndex++] = intensity;

                // We're outputting BGR, the last byte in the 32 bits is unused so skip it
                // If we were outputting BGRA, we would write alpha here.
                ++colorPixelIndex;
            }

            var stride = frame.Width * frame.BytesPerPixel;

            var bmpFrame = new Bitmap(frame.Width, frame.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            var bmpData  = bmpFrame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bmpFrame.PixelFormat);

            System.Runtime.InteropServices.Marshal.Copy(colorPixels, 0, bmpData.Scan0, colorPixels.Length);

            bmpFrame.UnlockBits(bmpData);

            return(bmpFrame);
        }
        void mykinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            depthframe = e.OpenDepthImageFrame();
            colorframe = e.OpenColorImageFrame();

            if (depthframe == null || colorframe == null)
            {
                return;
            }

            depthpixelData = new short[depthframe.PixelDataLength];
            depthframe.CopyPixelDataTo(depthpixelData);
            _DepthImageBitmap.WritePixels(_DepthImageBitmapRect, depthpixelData, _DepthImageStride, 0);
            depthPixel = new DepthImagePixel[depthframe.PixelDataLength];
            depthframe.CopyDepthImagePixelDataTo(depthPixel);

            colorpixelData = new byte[colorframe.PixelDataLength];
            colorframe.CopyPixelDataTo(colorpixelData);

            if (depthpixelData != null)
            {
                UserBorderCaculation();
            }

            _ColorImageBitmap.WritePixels(_ColorImageBitmapRect, colorpixelData, _ColorImageStride, 0);

            depthframe.Dispose();
            colorframe.Dispose();
        }
Beispiel #8
0
        /// <summary>
        /// Event handler for Kinect sensor's DepthFrameReady event
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    // Copy the pixel data from the image to a temporary array
                    depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);
                    // Get the min and max reliable depth for the current frame

                    short[]    depth      = this.depthPixels.Select(pixel => pixel.Depth).ToArray();
                    short[]    depthFixed = DepthFixer.Fix(depth);
                    KinectData kd         = new KinectData(DepthWidth, DepthHeight);
                    kd.SetDepthData(depth, depthFixed, MinDepthRange, MaxDepthRange);
                    //sender matrix

                    if (Frame != null)
                    {
                        fpsController = 0; //reset counter
                        Frame(kd, e);
                    }

                    fpsController++;
                }
            }
        }
Beispiel #9
0
        private void DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            DepthImageFrame frame = e.OpenDepthImageFrame();

            if (frame != null)
            {
                if (this.first || frame.Format != this.format || this.width != frame.Width || this.height != frame.Height)
                {
                    this.InitBuffers(frame);
                    this.DisposeTextures();
                    this.first = false;
                }

                this.FInvalidate = true;
                this.frameindex  = frame.FrameNumber;
                lock (m_lock)
                {
                    frame.CopyDepthImagePixelDataTo(this.depthpixels);
                    for (int i16 = 0; i16 < this.width * this.height; i16++)
                    {
                        this.rawdepth[i16] = this.depthpixels[i16].Depth;
                    }
                }

                frame.Dispose();
            }
        }
Beispiel #10
0
        /// <summary>
        /// Event handler for Kinect sensor's DepthFrameReady event
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            bool frameReady = false;

            lock (processingLock)
            {
                frameReady = !frameProcessing;
            }
            if (frameReady)
            {
                using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
                {
                    if (depthFrame != null)
                    {
                        if (diffArray == null)
                        {
                            diffArray = new short[depthFrame.Width, depthFrame.Height];
                        }
                        depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);
                        lock (processingLock)
                        {
                            frameProcessing = true;
                        }
                        this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                                                    (Action)(() => this.ProcessDepthData(depthFrame)));
                    }
                }
            }
        }
        private void ReconhecerDistancia(DepthImageFrame quadro, byte[] bytesImagem, int distanciaMaxima)
        {
            if (quadro == null || bytesImagem == null)
            {
                return;
            }

            using (quadro)
            {
                DepthImagePixel[] imagemProfundidade = new DepthImagePixel[quadro.PixelDataLength];
                quadro.CopyDepthImagePixelDataTo(imagemProfundidade);

                DepthImagePoint[] pontosImagemProfundidade = new DepthImagePoint[640 * 480];
                kinect.CoordinateMapper.MapColorFrameToDepthFrame(kinect.ColorStream.Format, kinect.DepthStream.Format, imagemProfundidade, pontosImagemProfundidade);

                for (int i = 0; i < pontosImagemProfundidade.Length; i++)
                {
                    var point = pontosImagemProfundidade[i];
                    if (point.Depth < distanciaMaxima && KinectSensor.IsKnownPoint(point))
                    {
                        var pixelDataIndex = i * 4;

                        byte maiorValorCor = Math.Max(bytesImagem[pixelDataIndex], Math.Max(bytesImagem[pixelDataIndex + 1], bytesImagem[pixelDataIndex + 2]));

                        bytesImagem[pixelDataIndex]     = maiorValorCor;
                        bytesImagem[pixelDataIndex + 1] = maiorValorCor;
                        bytesImagem[pixelDataIndex + 2] = maiorValorCor;
                    }
                }
            }
        }
Beispiel #12
0
        private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);

                    int minDepth = depthFrame.MinDepth;
                    int maxDepth = depthFrame.MaxDepth;

                    // Convert depth to RGB.
                    int colorPixelIndex = 0;
                    for (int i = 0; i < this.depthPixels.Length; ++i)
                    {
                        short depth     = depthPixels[i].Depth;
                        byte  intensity = (byte)(depth >= minDepth && depth <= maxDepth ?
                                                 depth : 0);
                        this.colorPixels[colorPixelIndex++] = intensity;
                        this.colorPixels[colorPixelIndex++] = intensity;
                        this.colorPixels[colorPixelIndex++] = intensity;
                        ++colorPixelIndex; // No alpha channel RGB.
                    }

                    // Copy pixels in RGB in the bitmap.
                    this.depthBitmap.WritePixels(
                        new Int32Rect(0, 0, this.depthBitmap.PixelWidth,
                                      this.depthBitmap.PixelHeight),
                        this.colorPixels,
                        this.depthBitmap.PixelWidth * sizeof(int),
                        0);
                }
            }
        }
Beispiel #13
0
        void sensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    depthFrame.CopyDepthImagePixelDataTo(this.depth_pixels);
                    int minDepth = depthFrame.MinDepth;
                    int maxDepth = depthFrame.MaxDepth;

                    int colorPixelIndex = 0;

                    for (int i = 0; i < this.depth_pixels.Length; ++i)
                    {
                        short depth     = depth_pixels[i].Depth;
                        byte  intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
                        this.depth_color[colorPixelIndex++] = intensity;
                        this.depth_color[colorPixelIndex++] = intensity;
                        this.depth_color[colorPixelIndex++] = intensity;
                        ++colorPixelIndex;
                    }
                    this.kinect_depth.WritePixels(
                        new Int32Rect(0, 0, this.kinect_depth.PixelWidth, this.kinect_depth.PixelHeight),
                        this.depth_color, this.kinect_depth.PixelWidth * sizeof(int), 0);
                    BitmapSource source = this.kinect_depth_screen.Source as BitmapSource;
                    wcon.updateDepthState(ref source, source.PixelHeight, source.PixelWidth);
                }
            }
        }
Beispiel #14
0
        private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    depthFrame.CopyDepthImagePixelDataTo(depthPixels);

                    int minDepth = depthFrame.MinDepth;
                    int maxDepth = depthFrame.MaxDepth;

                    int colorPixelIndex = 0;
                    for (int i = 0; i < depthPixels.Length; i++)
                    {
                        short depth     = depthPixels[i].Depth;
                        byte  intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
                        colorPixels[colorPixelIndex++] = intensity;
                        colorPixels[colorPixelIndex++] = intensity;
                        colorPixels[colorPixelIndex++] = intensity;
                        ++colorPixelIndex;
                    }

                    colorBitmap.WritePixels(new Int32Rect(0, 0,
                                                          colorBitmap.PixelWidth, colorBitmap.PixelHeight),
                                            colorPixels, colorBitmap.PixelWidth * sizeof(int), 0);

                    image.Source = colorBitmap;
                }
            }
        }
        private void ReconhecerDistancia(DepthImageFrame quadro, byte[] bytesImagem, int distanciaMaxima)
        {
            if (quadro == null || bytesImagem == null)
            {
                return;
            }

            using (quadro)
            {
                DepthImagePixel[] imagemProfundidade = new DepthImagePixel[quadro.PixelDataLength];
                quadro.CopyDepthImagePixelDataTo(imagemProfundidade);

                for (int indice = 0; indice < imagemProfundidade.Length; indice++)
                {
                    if (imagemProfundidade[indice].Depth < distanciaMaxima)
                    {
                        int  indiceImageCores = indice * 4;
                        byte maiorValorCor    = Math.Max(bytesImagem[indiceImageCores], Math.Max(bytesImagem[indiceImageCores + 1], bytesImagem[indiceImageCores + 2]));

                        bytesImagem[indiceImageCores]     = maiorValorCor;
                        bytesImagem[indiceImageCores + 1] = maiorValorCor;
                        bytesImagem[indiceImageCores + 2] = maiorValorCor;
                    }
                }
            }
        }
Beispiel #16
0
        private void nui_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (DepthImageFrame dif = e.OpenDepthImageFrame())
            {//DepthImageFrame dif = e.OpenDepthImageFrame();
                if (dif != null)
                {
                    DepthImagePixel[] depthPixels = new DepthImagePixel[this.sensor.DepthStream.FramePixelDataLength];
                    //byte[] pre = new byte[dif.Width * dif.Height * 4];
                    byte[]  pre      = new byte[this.sensor.DepthStream.FramePixelDataLength * sizeof(int)];
                    short[] preshort = new short[dif.PixelDataLength];
                    int     minDepth = dif.MinDepth;
                    int     maxDepth = dif.MaxDepth;
                    //dif.CopyPixelDataTo(preshort);
                    dif.CopyDepthImagePixelDataTo(depthPixels);

                    for (int i = 0; i < depthPixels.Length; ++i)
                    {
                        short depth = depthPixels[i].Depth;
                        pre[i] = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
                    }

                    processDepthFrame(pre);
                    depthFrameTex = generateDepthTex();

                    if (OnDepthFrame != null)
                    {
                        OnDepthFrame();
                    }

                    fps.PushFrame();
                    Ready = true;
                }
            }
        }
        /// <summary>
        /// 距離データの更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void kinect_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (DepthImageFrame frame = e.OpenDepthImageFrame()) {
                if (frame == null)
                {
                    return;
                }

                // DepthImagePixel で取得すると、16bitの距離データ+プライヤーインデックス
                // が取得できる
                DepthImagePixel[] depth = new DepthImagePixel[frame.PixelDataLength];
                frame.CopyDepthImagePixelDataTo(depth);

                // 中心点の距離を表示する
                int index = (frame.Height / 2) * frame.Width + (frame.Width / 2);
                textDepth.Text = string.Format("{0}mm", depth[index].Depth);

                // 可視画像に変換する(14bitで16m)
                // どこまでいけるかは不明だけどOpenNI時の10m弱くらいが限界?
                short[] pixel = new short[frame.PixelDataLength];
                for (int i = 0; i < depth.Length; i++)
                {
                    pixel[i] = (short)~(depth[i].Depth * 0xFFFF / 0x3FFF);
                }

                imageDepth.Source = BitmapSource.Create(frame.Width, frame.Height, 96, 96,
                                                        PixelFormats.Gray16, null, pixel, frame.Width * frame.BytesPerPixel);
            }
        }
        private void CalibrateFrameByFrame(object sender, DepthImageFrameReadyEventArgs e)
        {
            DepthImageFrame depthFrame = e.OpenDepthImageFrame();

            if (depthFrame == null)
            {
                return;
            }
            depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);
            depthFrame.Dispose();
            for (int i = 0; i < this.depthPixels.Length; ++i)
            {
                if (!depthPixelsReference[i].Contains(depthPixels[i].Depth))
                {
                    depthPixelsReference[i].Add(depthPixels[i].Depth);
                }
            }
            currentCalibrateImageNb++;
            if (currentCalibrateImageNb >= CALIBRATE_LEVEL && !isCalibrate)
            {
                isCalibrate = true;
                kinectSensor.DepthFrameReady += this.SensorDepthFrameReady;
                kinectSensor.DepthFrameReady -= this.CalibrateFrameByFrame;
            }
        }
Beispiel #19
0
        public TDepthFrame(DepthImageFrame sensorFrame)
        {
            //TODO This can be done better
            var depthImagePixels = new DepthImagePixel[sensorFrame.PixelDataLength];

            sensorFrame.CopyDepthImagePixelDataTo(depthImagePixels);

            var depthData = new short[sensorFrame.PixelDataLength];

            for (int i = 0; i < sensorFrame.PixelDataLength; i++)
            {
                depthData[i] = depthImagePixels[i].Depth;
            }

            DepthData = depthData;

            PixelDataLength = sensorFrame.PixelDataLength;
            BytesPerPixel   = sensorFrame.BytesPerPixel;
            FrameNumber     = sensorFrame.FrameNumber;
            Width           = sensorFrame.Width;
            Height          = sensorFrame.Height;
            Timestamp       = sensorFrame.Timestamp;

            MinDepth = sensorFrame.MinDepth;
            MaxDepth = sensorFrame.MaxDepth;
        }
        private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            if (this.isTreating == true)
            {
                return;
            }
            this.isTreating = true;
            DepthImageFrame depthFrame = e.OpenDepthImageFrame();

            if (depthFrame != null)
            {
                depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);
                depthFrame.Dispose();

                int colorPixelIndex = 0;
                int lenght          = this.depthPixels.Length;
                for (int i = 0; i < lenght; ++i)
                {
                    short depth = this.depthPixels[i].Depth;

                    byte intensity;
                    if (depth < MIN_DEPTH || depth > MAX_DEPTH || !IsUserPixel(i, depth))
                    {
                        intensity = (byte)255;
                    }
                    else
                    {
                        intensity = (byte)0;
                    }

                    // Write out blue byte
                    this.colorPixels[colorPixelIndex++] = intensity;

                    // Write out green byte
                    this.colorPixels[colorPixelIndex++] = intensity;

                    // Write out red byte
                    this.colorPixels[colorPixelIndex++] = intensity;

                    //colorPixelIndex++; si on a pas de transparence (bgr32)
                    if (intensity == 0)
                    {
                        this.colorPixels[colorPixelIndex++] = 255;
                    }
                    else
                    {
                        this.colorPixels[colorPixelIndex++] = 0;
                    }
                }
                // Ecriture de l'image
                this.colorBitmap.WritePixels(
                    new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                    this.colorPixels,
                    this.colorBitmap.PixelWidth * sizeof(int),
                    0);
                OnImageReady();
            }
            this.isTreating = false;
        }
Beispiel #21
0
        /// <summary>
        /// DepthFrameReady event handler to initialize the environment to get ready for surface detection.
        /// User should be away from the surface.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void InitializeEnvironment(object sender, DepthImageFrameReadyEventArgs e)
        {
            if (!b_InitializeEnvironment)
            {
                this.sensor.DepthFrameReady -= InitializeEnvironment;
                DebugMsg("Blocked InitializeEnvironment");
                return;
            }

            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    surfaceDetection.emptyFrame = new DepthImagePixel[this.sensor.DepthStream.FramePixelDataLength];
                    depthFrame.CopyDepthImagePixelDataTo(surfaceDetection.emptyFrame);
                    DebugMsg("Background depth frame captured");

                    // Get the min and max reliable depth for the current frame
                    int minDepth = depthFrame.MinDepth;
                    int maxDepth = depthFrame.MaxDepth;

                    // Convert the depth to RGB
                    int colorPixelIndex = 0;
                    for (int i = 0; i < this.sensor.DepthStream.FramePixelDataLength; ++i)
                    {
                        // Get the depth for this pixel
                        short depth     = surfaceDetection.emptyFrame[i].Depth;
                        byte  intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
                        this.depthImageColor[colorPixelIndex++] = intensity; // Write the blue byte
                        this.depthImageColor[colorPixelIndex++] = intensity; // Write the green byte
                        this.depthImageColor[colorPixelIndex++] = intensity; // Write the red byte

                        // We're otuputting BGR, the last byte in teh 32 bits is unused so skip it
                        // If we were outputting BGRA, we would write alpha here.
                        ++colorPixelIndex;
                    }

                    // Write the pixel data into our bitmap
                    this.depthBitmap.WritePixels(
                        new Int32Rect(0, 0, this.depthBitmap.PixelWidth, this.depthBitmap.PixelHeight),
                        this.depthImageColor,
                        this.depthBitmap.PixelWidth * sizeof(int),
                        0);


                    DebugMsg("Un-hook InitializeEnvironment");
                    this.sensor.DepthFrameReady -= InitializeEnvironment;
                    b_InitializeEnvironment      = false;
                    DebugMsg("Hook  up DefineSurface");

                    if (this.surfaceDetection.surface != null)
                    {
                        b_ColorPlaneDepthFrame       = true;
                        this.sensor.DepthFrameReady += ColorPlaneDepthFrame;
                    }
                }
            }
        }
        /// <summary>
        /// Updates the user view inset window, which displays user masks as reported in the depth
        /// image.
        /// </summary>
        /// <param name="depthFrame">New depth frame from the sensor.</param>
        private void UpdateUserView(DepthImageFrame depthFrame)
        {
            if (null == this.depthData || this.depthData.Length != depthFrame.PixelDataLength)
            {
                // If necessary, allocate new buffer for the depth data.
                this.depthData = new DepthImagePixel[depthFrame.PixelDataLength];
            }

            // Store the depth data.
            depthFrame.CopyDepthImagePixelDataTo(this.depthData);

            int width  = depthFrame.Width;
            int height = depthFrame.Height;

            if (null == this.userViewBitmap ||
                this.userViewBitmap.PixelWidth != width ||
                this.userViewBitmap.PixelHeight != height)
            {
                // If necessary, allocate new bitmap in BGRA format.
                // Set it as the source of the UserView Image control.
                this.userViewBitmap = new WriteableBitmap(
                    width,
                    height,
                    96.0,
                    96.0,
                    PixelFormats.Bgra32,
                    null);

                this.UserView.Source = this.userViewBitmap;
            }

            // Write the per-user colors into the user view bitmap, one pixel at a time.
            this.userViewBitmap.Lock();

            unsafe
            {
                uint *userViewBits = (uint *)this.userViewBitmap.BackBuffer;
                fixed(uint *userColors = &this.userColors[0])
                {
                    // Walk through each pixel in the depth data.
                    fixed(DepthImagePixel *depthData = &this.depthData[0])
                    {
                        DepthImagePixel *depthPixel    = depthData;
                        DepthImagePixel *depthPixelEnd = depthPixel + this.depthData.Length;

                        while (depthPixel < depthPixelEnd)
                        {
                            // Lookup a pixel color based on the player index.
                            // Store the color in the user view bitmap's buffer.
                            *(userViewBits++) = *(userColors + (depthPixel++)->PlayerIndex);
                        }
                    }
                }
            }

            this.userViewBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
            this.userViewBitmap.Unlock();
        }
Beispiel #23
0
        private void depth_FrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            current_frame_number_for_stop++;
            if (waiting == 0)
            {
                waiting += 2;
            }
            else
            {
                waiting++;
            }
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    int count       = 0;
                    int frameNumber = 0;
                    if (depthFrame.Timestamp == 0)
                    {
                        waiting -= 1;
                        return;
                    }

                    if (depthFrame.Timestamp < FirstTimeStamp)
                    {
                        FirstTimeStamp = depthFrame.Timestamp;
                    }

                    frameNumber         = GetRealCurrentFrame(depthFrame.Timestamp - FirstTimeStamp);
                    count               = frameNumber - PreDepthFrameNumber;
                    PreDepthFrameNumber = frameNumber;
                    //Console.WriteLine("Depth {0} {1} {2} {3} {4}", FirstTimeStamp, depthFrame.Timestamp, depthFrame.Timestamp - FirstTimeStamp, frameNumber, count);

                    depthFrame.CopyDepthImagePixelDataTo(depthPixels);
                    //int minDepth = depthFrame.MinDepth;
                    //int maxDepth = depthFrame.MaxDepth;
                    int width  = depthFrame.Width;
                    int height = depthFrame.Height;
                    //Console.WriteLine("Depth:{0} {1}" ,DepthTS,count);

                    colorizer.TransformAndConvertDepthFrame(depthPixels, _colorPixels);

                    var depthImg = ImageConverter.Array2Image(_colorPixels, width, height, width * 4).Convert <Bgr, byte>();
                    if (depthImg.Ptr != IntPtr.Zero)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            if (depthWriter != null)
                            {
                                depthWriter.WriteFrame(depthImg);
                            }
                        }
                    }
                }
            }
            waiting -= 1;
        }
Beispiel #24
0
        private void Sensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            DepthImageFrame frame = e.OpenDepthImageFrame();


            if (frame != null)
            {
                CurrentFrame++;

                DepthImagePixel[] pixelArray = new DepthImagePixel[sensor.DepthStream.FramePixelDataLength];

                //Copy depth data to Array
                frame.CopyDepthImagePixelDataTo(pixelArray);


                if (isSet == false && focusPoint.X != -1)
                {
                    focusPoint.Depth  = pixelArray[pointToIndex(frame.Width, focusPoint)].Depth;
                    lbl_Point.Text    = "(" + focusPoint.X + "," + focusPoint.Y + ")";
                    lbl_Distance.Text = (float)focusPoint.Depth / 1000 + " m";

                    //Debug.WriteLine("Set New Focus Point On {" + focusPoint.X + "," + focusPoint.Y + "} Distance : " + (float)focusPoint.Depth / 1000 + " Meters");
                    isSet    = true;
                    focusSet = true;
                }
                else if (focusSet == true)
                {
                    lbl_Distance.Text = (float)focusPoint.Depth / 1000 + " m";
                }

                //Dont collide with graphics driver
                if (isWorking == false)
                {
                    isWorking = true;

                    t = new Thread(() =>
                    {
                        depthSet(ref pixelArray, frame.Width, frame.Height);
                    });

                    t.Start();

                    //Modify Text
                    lbl_status.Text = "Depth Average : " + average(ref pixelArray);
                }

                frame.Dispose();
            }
            else
            {
                //If frame is null do nothing...

                //Modify Text
                lbl_status.Text = "DATA : Not Received";
            }
        }
Beispiel #25
0
        // Event handler for sensor's DepthFrameReady event
        // depth stream, changed the color depends on distance
        private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    depthFrame.CopyDepthImagePixelDataTo(depthPixels);
                    byte[] depthdata = new byte[depthPixels.Length * 4];
                    int    minDepth  = depthFrame.MinDepth;
                    int    maxDepth  = depthFrame.MaxDepth;

                    for (int i = 0; i < depthPixels.Length; ++i)
                    {
                        short depth = depthPixels[i].Depth;

                        if (depth == -1)
                        {
                            depthdata[i * 4 + 0] = 0;
                            depthdata[i * 4 + 1] = 0;
                            depthdata[i * 4 + 2] = 255;
                            depthdata[i * 4 + 3] = 0;
                        }
                        else if (depth == 0)
                        {
                            depthdata[i * 4 + 0] = 0;
                            depthdata[i * 4 + 1] = 255;
                            depthdata[i * 4 + 2] = 255;
                            depthdata[i * 4 + 3] = 0;
                        }
                        else if (depth >= 800 && depth <= 1800)
                        {
                            depthdata[i * 4 + 0] = 0;
                            depthdata[i * 4 + 1] = 255;
                            depthdata[i * 4 + 2] = 0;
                            depthdata[i * 4 + 3] = 0;
                        }
                        else
                        {
                            depthdata[i * 4 + 0] = 255;
                            depthdata[i * 4 + 1] = 255;
                            depthdata[i * 4 + 2] = 255;
                            depthdata[i * 4 + 3] = 0;
                        }
                    }

                    // Write the pixel data into our bitmap
                    depthBitmap.WritePixels(
                        new Int32Rect(0, 0, this.depthBitmap.PixelWidth, this.depthBitmap.PixelHeight),
                        depthdata,
                        depthBitmap.PixelWidth * sizeof(int),
                        0);
                }
            }
        }
Beispiel #26
0
        /// <summary>
        /// Event handler for Kinect sensor's DepthFrameReady event
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    // Copy the pixel data from the image to a temporary array
                    depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);

                    // Get the min and max reliable depth for the current frame
                    int minDepth = depthFrame.MinDepth;
                    int maxDepth = depthFrame.MaxDepth;

                    // Load depth values to 1D array
                    short[] depthArray1D = new short[this.depthPixels.Length];
                    for (int i = 0; i < this.depthPixels.Length; ++i)
                    {
                        depthArray1D[i] = depthPixels[i].Depth;
                    }

                    // Convert the 1D data to the desired 2D array
                    TableVision.DepthArray2D = make2DArray(depthArray1D, this.depthBitmap.PixelHeight, this.depthBitmap.PixelWidth);

                    // Convert the depth to RGB
                    int colorPixelIndex = 0;
                    for (int i = 0; i <= TableVision.DepthArray2D.GetUpperBound(0); i++)
                    {
                        for (int j = 0; j <= TableVision.DepthArray2D.GetUpperBound(1); j++)
                        {
                            short depth     = TableVision.DepthArray2D[i, j];
                            byte  intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
                            this.renderedDepthPixels[colorPixelIndex++] = intensity;
                            this.renderedDepthPixels[colorPixelIndex++] = intensity;
                            this.renderedDepthPixels[colorPixelIndex++] = intensity;
                            ++colorPixelIndex;
                        }
                    }

                    // Write the pixel data into our bitmap
                    this.depthBitmap.WritePixels(
                        new Int32Rect(0, 0, this.depthBitmap.PixelWidth, this.depthBitmap.PixelHeight),
                        this.renderedDepthPixels,
                        this.depthBitmap.PixelWidth * sizeof(int),
                        0);

                    // If crop box is valid, draw crop box
                    if (this.cropBox.TLX != -1)
                    {
                        this.depthBitmap.DrawRectangle(this.cropBox.TLX, this.cropBox.TLY, this.cropBox.BRX, this.cropBox.BRY, System.Windows.Media.Color.FromRgb(255, 0, 0));
                    }
                }
            }
        }
Beispiel #27
0
        // Calibrate sensor with a single frame of the sensor
        private void CalibrationDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null && this.calibrationNeeded)
                {
                    Console.WriteLine("Calibrating...");

                    // Toggle Calibration image visibility
                    if (this.Image.Visibility == Visibility.Collapsed)
                    {
                        this.Image.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        this.Image.Visibility = Visibility.Collapsed;
                    }

                    // Copy the pixel data from the image to a temporary array
                    depthFrame.CopyDepthImagePixelDataTo(this.depthPixelsCalibration);
                    this.calibrationNeeded = false;
                    this.calibrated        = true;

                    for (int i = 0; i < this.depthPixelsCalibration.Length; ++i)
                    {
                        // Get the depth for this pixel
                        short depth = depthPixels[i].Depth;

                        if (this.minPixelDepth == 0)
                        {
                            this.minPixelDepth = depth;
                        }
                        else if (depth < this.minPixelDepth && depth != 0)
                        {
                            this.minPixelDepth = depth;
                        }

                        if (depth > maxPixelDepth)
                        {
                            this.maxPixelDepth = depth;
                        }

                        this.avgPixelDepth += depth;
                    }

                    // Calculate calibrated wall threshold
                    this.avgPixelDepth  = this.avgPixelDepth / this.depthPixelsCalibration.Length;
                    this.wallDistThresh = (int)((this.maxPixelDepth - this.avgPixelDepth) * .80);

                    Console.WriteLine("Pixel: " + minPixelDepth.ToString() + " : " + maxPixelDepth.ToString() + " : " + avgPixelDepth.ToString() + " : " + this.wallDistThresh.ToString());
                }
            }
        }
Beispiel #28
0
        private void handleDepthImageFrame(DepthImageFrame depthFrame)
        {
            using (depthFrame)
            {
                if (depthFrame != null)
                {
                    DepthImagePixel[] depthPixels = new DepthImagePixel[depthFrame.PixelDataLength];
                    depthFrame.CopyDepthImagePixelDataTo(depthPixels);

                    ThreadPool.QueueUserWorkItem(new WaitCallback(o => DepthFrameCallback(depthFrame.Timestamp, depthFrame.FrameNumber, depthPixels)));
                }
            }
        }
        public void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            DepthImagePixel[] depthPixels = new DepthImagePixel[this.adapter.sensor.DepthStream.FramePixelDataLength];

            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    depthFrame.CopyDepthImagePixelDataTo(depthPixels);
                    this.adapter.OnDepthFrameAvailable(depthPixels);
                }
            }
        }
        private Bitmap CreateBitmapFromSensor3(DepthImageFrame frame)
        {
            int minDepth    = frame.MinDepth;
            int maxDepth    = frame.MaxDepth;
            int colorsNumer = maxDepth - minDepth + 10;

            Color[] colors = new Color[colorsNumer];
            for (int colorCnt = 0; colorCnt < colorsNumer; colorCnt++)
            {
                colors[colorCnt] = MapRainbowColor(colorCnt, 0, colorsNumer);
            }
            int width      = frame.Width;
            int height     = frame.Height;
            var pixelData2 = new DepthImagePixel[frame.PixelDataLength];

            int[][] localDistance = new int[height][];
            frame.CopyDepthImagePixelDataTo(pixelData2);
            var bmp = new Bitmap(frame.Width, frame.Height);

            for (int y = 0; y < height; y++)
            {
                int basePositionY = y * width;
                localDistance[y] = new int[width];
                for (int x = 0; x < width; x++)
                {
                    int realOositionY = basePositionY + x;
                    if (pixelData2[realOositionY].IsKnownDepth)
                    {
                        //byte red = (byte)(pixelData2[realOositionY].Depth >> 8);
                        //byte green = 125;
                        //byte blue = (byte)(pixelData2[realOositionY].Depth & 255); ;
                        //bmp.SetPixel(x, y, Color.FromArgb(red, green, blue));
                        int depthLocal = pixelData2[realOositionY].Depth;
                        localDistance[y][x] = depthLocal;

                        if (depthLocal > maxDepth)
                        {
                            depthLocal = maxDepth;
                        }
                        else if (depthLocal < minDepth)
                        {
                            depthLocal = minDepth;
                        }
                        bmp.SetPixel(x, y, colors[depthLocal - minDepth]);
                    }
                }
            }
            distance = localDistance;
            return(bmp);
        }