/// <summary>
        /// Sets the data of ColorVideo.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="colorImageFrame"></param>
        public void KinectColorFrameReady(object sender, ColorImageFrameReadyEventArgs colorImageFrame)
        {
            //Get raw image
            ColorVideoFrame = colorImageFrame.OpenColorImageFrame();

            if (ColorVideoFrame != null)
            {
                //Create array for pixel data and copy it from the image frame
                PixelData = new Byte[ColorVideoFrame.PixelDataLength];
                ColorVideoFrame.CopyPixelDataTo(PixelData);

                //Convert RGBA to BGRA, Kinect and XNA uses different color-formats.
                BgraPixelData = new Byte[ColorVideoFrame.PixelDataLength];
                for (int i = 0; i < PixelData.Length; i += 4)
                {
                    BgraPixelData[i] = PixelData[i + 2];
                    BgraPixelData[i + 1] = PixelData[i + 1];
                    BgraPixelData[i + 2] = PixelData[i];
                    BgraPixelData[i + 3] = (Byte)255; //The video comes with 0 alpha so it is transparent
                }

                // Create a texture and assign the realigned pixels
                ColorVideo = new Texture2D(Graphics.GraphicsDevice, ColorVideoFrame.Width, ColorVideoFrame.Height);
                ColorVideo.SetData(BgraPixelData);
                ColorVideoFrame.Dispose();
            }
        }
        public static Bitmap ColorImageFrameToBitmap(ColorImageFrame colorFrame)
        {
            byte[] pixelBuffer = new byte[colorFrame.PixelDataLength];
            colorFrame.CopyPixelDataTo(pixelBuffer);

            System.Drawing.Imaging.PixelFormat pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppRgb;

            if (colorFrame.Format == ColorImageFormat.InfraredResolution640x480Fps30)
            {
                pixelFormat = System.Drawing.Imaging.PixelFormat.Format16bppRgb565;
            }

            Bitmap bitmapFrame = new Bitmap(colorFrame.Width, colorFrame.Height, pixelFormat);
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bitmapFrame.Width, bitmapFrame.Height);
            BitmapData bitmapData = bitmapFrame.LockBits(rect, ImageLockMode.WriteOnly, bitmapFrame.PixelFormat);

            IntPtr intPointer = bitmapData.Scan0;
            Marshal.Copy(pixelBuffer, 0, intPointer, colorFrame.PixelDataLength);

            bitmapFrame.UnlockBits(bitmapData);

            bitmapData = null;
            pixelBuffer = null;

            return bitmapFrame;
        }
Example #3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="colorFrame"></param>
        /// <param name="headList"></param>
        private void FillBitmap( ColorImageFrame colorFrame, List<SkeletonPoint> headList )
        {
            // 描画の準備
            using ( var drawContecxt = drawVisual.RenderOpen() ) {
                // 画像情報をバッファにコピー
                colorFrame.CopyPixelDataTo( pixelBuffer );

                // カメラの画像情報から、背景のビットマップを作成し描画する
                var backgroundImage = new WriteableBitmap( colorFrame.Width, colorFrame.Height, 96, 96,
                    PixelFormats.Bgr32, null );
                backgroundImage.WritePixels( new Int32Rect( 0, 0, colorFrame.Width, colorFrame.Height ),
                    pixelBuffer, colorFrame.Width * 4, 0 );
                drawContecxt.DrawImage( backgroundImage,
                    new Rect( 0, 0, colorFrame.Width, colorFrame.Height ) );

                // 頭の位置にマスク画像を表示する
                foreach ( SkeletonPoint head in headList ) {
                    ColorImagePoint headPoint = kinect.MapSkeletonPointToColor( head, rgbFormat );
                    drawContecxt.DrawImage( maskImage,
                        new Rect( headPoint.X - 64, headPoint.Y - 64, 128, 128 ) );
                }
            }

            // 画面に表示するビットマップに描画する
            bmpBuffer.Render( drawVisual );
        }
Example #4
0
        public RecordedVideoFrame(ColorImageFrame colorFrame)
        {
            if (colorFrame != null)
            {
                byte[] bits = new byte[colorFrame.PixelDataLength];
                colorFrame.CopyPixelDataTo(bits);

                int BytesPerPixel = colorFrame.BytesPerPixel;
                int Width = colorFrame.Width;
                int Height = colorFrame.Height;

                var bmp = new WriteableBitmap(Width, Height, 96, 96, PixelFormats.Bgr32, null);
                bmp.WritePixels(new System.Windows.Int32Rect(0, 0, Width, Height), bits, Width * BytesPerPixel, 0);
                JpegBitmapEncoder jpeg = new JpegBitmapEncoder();
                jpeg.Frames.Add(BitmapFrame.Create(bmp));
                var SaveStream = new MemoryStream();
                jpeg.Save(SaveStream);
                SaveStream.Flush();
                JpegData = SaveStream.ToArray();
            }
            else
            {
                return;
            }
        }
Example #5
0
        void SensorColorFrameReady(object sender, kinect.ColorImageFrameReadyEventArgs e)
        {
            using (kinect.ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    // Copy the pixel data from the image to a temporary array
                    colorFrame.CopyPixelDataTo(this.colorFramePixels);

                    for (int i = 3; i < this.colorFramePixels.Length - 4; i += 4)
                    {
                        this.colorFramePixels[i] = 255;  // set the alpha to max
                    }

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

            // draw a sighting frame for precise alignment:
            // Horisontally the frame spans 16.56 degrees on every side and 12.75 degrees either up or down (at 74" the size it covers is 44"W by 33.5"H, i.e. 33.12 degrees by 25.5 degrees)
            // see http://writeablebitmapex.codeplex.com/
            //int sightFrameColor = 255;  // Pbgra32
            Color sightFrameColor = Colors.Red;

            colorBitmapVideo.DrawLine((int)colorBitmapVideo.Width / 2, 0, (int)colorBitmapVideo.Width / 2, (int)colorBitmapVideo.Height, sightFrameColor);
            colorBitmapVideo.DrawLine(0, (int)colorBitmapVideo.Height / 2, (int)colorBitmapVideo.Width, (int)colorBitmapVideo.Height / 2, sightFrameColor);
            colorBitmapVideo.DrawRectangle((int)colorBitmapVideo.Width / 4, (int)colorBitmapVideo.Height / 4,
                                           (int)colorBitmapVideo.Width * 3 / 4, (int)colorBitmapVideo.Height * 3 / 4, sightFrameColor);
        }
Example #6
0
        public WriteableBitmap bmpFromColorFrame(ColorImageFrame NewFrame, bool SkeletonWanted, SkeletonFrame SkelFrame)
        {
            if (bmpColor == null)
            {
                bmpColor = new WriteableBitmap(frameWidth, frameHeight, 96, 96, PixelFormats.Bgr32, null);
            }

            if (NewFrame == null)
            {
                throw new InvalidOperationException("Null Image");
            }

            NewFrame.CopyPixelDataTo(colorPixels);
            // Write the pixel data into our bitmap
            bmpColor.WritePixels(
            new Int32Rect(0, 0, bmpColor.PixelWidth, bmpColor.PixelHeight),
                colorPixels,
                bmpColor.PixelWidth * sizeof(int),
                0);
            //Skeleton
            if (SkeletonWanted != false && SkelFrame != null)
            {
                return bmpWithSkelFromColor(bmpColor, SkelFrame);
            }
            return bmpColor;
        }
Example #7
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="colorFrame"></param>
        /// <param name="headList"></param>
        private void FillBitmap( ColorImageFrame colorFrame, List<Tuple<SkeletonPoint, Matrix4>> headList )
        {
            // 描画の準備
            using ( var drawContecxt = drawVisual.RenderOpen() ) {
                // 画像情報をバッファにコピー
                colorFrame.CopyPixelDataTo( pixelBuffer );

                // カメラの画像情報から、背景のビットマップを作成し描画する
                var backgroundImage = new WriteableBitmap( colorFrame.Width, colorFrame.Height, 96, 96,
                    PixelFormats.Bgr32, null );
                backgroundImage.WritePixels( new Int32Rect( 0, 0, colorFrame.Width, colorFrame.Height ),
                    pixelBuffer, colorFrame.Width * 4, 0 );
                drawContecxt.DrawImage( backgroundImage,
                    new Rect( 0, 0, colorFrame.Width, colorFrame.Height ) );

                // 頭の位置にマスク画像を表示する
                foreach ( var head in headList ) {
                    ColorImagePoint headPoint = kinect.MapSkeletonPointToColor( head.Item1, rgbFormat );

                    // 頭の位置の向きに回転させたマスク画像を描画する
                    Matrix4 hm = head.Item2;
                    Matrix rot = new Matrix( -hm.M11, hm.M12,
                                             -hm.M21, hm.M22,
                                             headPoint.X, headPoint.Y );
                    drawContecxt.PushTransform( new MatrixTransform( rot ) );

                    drawContecxt.DrawImage( maskImage,
                        new Rect( -64, -64, 128, 128 ) );
                    drawContecxt.Pop();
                }
            }

            // 画面に表示するビットマップに描画する
            bmpBuffer.Render( drawVisual );
        }
        public KServerColorStreamPaquet(ColorImageFrame _imageFrame, int _idSensor)
        {
            idSensor = _idSensor;
            imageFrame = _imageFrame;

            byte[] pxlData = new byte[imageFrame.PixelDataLength];
            imageFrame.CopyPixelDataTo(pxlData);

            jpgImage = new MemoryStream();
            unsafe {
               fixed (byte* ptr = pxlData) {
                  using (Bitmap image = new Bitmap(imageFrame.Width, imageFrame.Height, imageFrame.Width*4, PixelFormat.Format32bppArgb, new IntPtr(ptr))) {
                     image.Save(jpgImage, ImageFormat.Jpeg);
                  }
               }
            }

            /* Sets the size of the paquet */
            setBodySize((uint)(jpgImage.Length+5));
            byte[] size = BitConverter.GetBytes((UInt32)(jpgImage.Length+5));

            Array.Reverse(size);
            Buffer.BlockCopy(size, 0, data, 0, size.Length);

            /* Builds the paquet */
            build();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ColorImageFrameLuminanceSource"/> class.
 /// </summary>
 /// <param name="bitmap">The bitmap.</param>
 /// <param name="flipTheImage">if set to <c>true</c> [flip the image].</param>
 public ColorImageFrameLuminanceSource(ColorImageFrame bitmap, bool flipTheImage)
    : base(bitmap.Width, bitmap.Height)
 {
    var pixelData = new byte[bitmap.PixelDataLength];
    var bitmapFormat = BitmapFormat.Unknown;
    switch (bitmap.Format)
    {
       case ColorImageFormat.InfraredResolution640x480Fps30:
          // not sure, what BitmapFormat should be selected
          break;
       case ColorImageFormat.RawBayerResolution1280x960Fps12:
       case ColorImageFormat.RawBayerResolution640x480Fps30:
          // not sure, what BitmapFormat should be selected
          break;
       case ColorImageFormat.RgbResolution1280x960Fps12:
       case ColorImageFormat.RgbResolution640x480Fps30:
          bitmapFormat = BitmapFormat.BGR32;
          break;
       case ColorImageFormat.RawYuvResolution640x480Fps15:
       case ColorImageFormat.YuvResolution640x480Fps15:
          // not sure, what BitmapFormat should be selected
          break;
       default:
          break;
    }
    bitmap.CopyPixelDataTo(pixelData);
    CalculateLuminance(pixelData, bitmapFormat);
    if (flipTheImage)
    {
       // flip the luminance values because the kinect has it flipped before
       FlipLuminanceValues();
    }
 }
        private BitmapSource ObterImagemSensorRGB(ColorImageFrame quadro)
        {
            using (quadro)
            {
                byte[] bytesImagem = new byte[quadro.PixelDataLength];
                quadro.CopyPixelDataTo(bytesImagem);

                return BitmapSource.Create(quadro.Width, quadro.Height,960 , 960, PixelFormats.Bgr32, null, bytesImagem, quadro.Width * quadro.BytesPerPixel);
            }
        }
        private byte[] ObterImagemSensorRGB(ColorImageFrame quadro)
        {
            if (quadro == null) return null;

            using (quadro)
            {
                byte[] bytesImagem = new byte[quadro.PixelDataLength];
                quadro.CopyPixelDataTo(bytesImagem);

                return bytesImagem;
            }
        }
 private ImageSource ObterImagemRGB(ColorImageFrame quadro)
 {
     if (quadro == null) return null;
     using (quadro)
     {
         byte[] bytesImagem = new byte[quadro.PixelDataLength];
         quadro.CopyPixelDataTo(bytesImagem);
         return BitmapSource.Create(quadro.Width, quadro.Height,
         96, 96, PixelFormats.Gray16, null, bytesImagem,
         quadro.Width * quadro.BytesPerPixel);
     }  
 }
 private BitmapSource ObterImagemSensorRGB(ColorImageFrame quadro)
 {
     using (quadro)
     {
         byte[] bytesImagem = new byte[quadro.PixelDataLength];
         quadro.CopyPixelDataTo(bytesImagem);
         int dpiX = 96;
         int dpiY = 96;
         int strideImage = quadro.Width * quadro.BytesPerPixel;
         return BitmapSource.Create(quadro.Width, quadro.Height,
         dpiX, dpiY, PixelFormats.Bgr32, null, bytesImagem,
         strideImage);
     }
 }
        public void synchronize(
            DepthImageFrame depthFrame,
            ColorImageFrame colorFrame,
            SkeletonFrame skletonFrame,
            Boolean isPauseMode
            )
        {
            IsPauseMode = isPauseMode;
            colorFrame.CopyPixelDataTo(_colorByte);

            //Console.WriteLine("max depth: "+depthFrame.MaxDepth);
            depthFrame.CopyDepthImagePixelDataTo(_depthPixels);

            _sensor.CoordinateMapper.MapColorFrameToDepthFrame(
                ColorImageFormat.RgbResolution640x480Fps30,
                DepthImageFormat.Resolution640x480Fps30,
                _depthPixels,
                _depthPoint
                );

            for (int i = 0; i < _pixelDepthDataLength; i++)
            {
                _depthShort[i] = (short)_depthPoint[i].Depth;
                _depthByte[i] = (byte)(_depthPoint[i].Depth*0.064-1);
            }

            skletonFrame.CopySkeletonDataTo(totalSkeleton);
            Skeleton firstSkeleton = (from trackskeleton in totalSkeleton
                                      where trackskeleton.TrackingState == SkeletonTrackingState.
                                      Tracked
                                      select trackskeleton).FirstOrDefault();

            _isCreation = true;
            if (firstSkeleton != null)
            {
                if (firstSkeleton.Joints[JointType.Spine].TrackingState == JointTrackingState.Tracked)
                {
                    IsSkeletonDetected = true;
                    UserSkeleton[SkeletonDataType.RIGHT_HAND] =
                        ScalePosition(firstSkeleton.Joints[JointType.HandRight].Position);
                    UserSkeleton[SkeletonDataType.LEFT_HAND] =
                        ScalePosition(firstSkeleton.Joints[JointType.HandLeft].Position);
                    UserSkeleton[SkeletonDataType.SPINE] =
                        ScalePosition(firstSkeleton.Joints[JointType.Spine].Position);
                    return;
                }
            }
            IsSkeletonDetected = false;
            _isCreation = false;
        }
 public void Update(ColorImageFrame frame)
 {
     var pixelData = new byte[frame.PixelDataLength];
     frame.CopyPixelDataTo(pixelData);
     if (Bitmap == null)
     {
         Bitmap = new WriteableBitmap(frame.Width, frame.Height,
         96, 96, PixelFormats.Bgr32, null);
     }
     int stride = Bitmap.PixelWidth * Bitmap.Format.BitsPerPixel / 8;
     Int32Rect dirtyRect = new Int32Rect(0, 0, Bitmap.PixelWidth, Bitmap.PixelHeight);
     Bitmap.WritePixels(dirtyRect, pixelData, stride, 0);
     RaisePropertyChanged(() => Bitmap);
 }
Example #16
0
        public static Bitmap colorFrameToAforge(ColorImageFrame frame)
        {
            byte[] pixels = new byte[frame.PixelDataLength];
            frame.CopyPixelDataTo(pixels);
            int stride = frame.Width * 4;
            BitmapSource bitmapSource = BitmapSource.Create(frame.Width, frame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);

            MemoryStream outStream = new MemoryStream();
            BitmapEncoder enc = new BmpBitmapEncoder();
            enc.Frames.Add(BitmapFrame.Create(bitmapSource));
            enc.Save(outStream);

            Bitmap bitmap = new Bitmap(outStream);
            return bitmap;
        }
Example #17
0
        private static Bitmap ColorImageFrameToBitmap(ColorImageFrame colorFrame)
        {
            byte[] pixelBuffer = new byte[colorFrame.PixelDataLength];
            colorFrame.CopyPixelDataTo(pixelBuffer);

            Bitmap bitmapFrame = new Bitmap(colorFrame.Width, colorFrame.Height, PixelFormat.Format32bppRgb);
            BitmapData bitmapData = bitmapFrame.LockBits(new Rectangle(0, 0, colorFrame.Width, colorFrame.Height), ImageLockMode.WriteOnly, bitmapFrame.PixelFormat);

            IntPtr intPointer = bitmapData.Scan0;
            Marshal.Copy(pixelBuffer, 0, intPointer, colorFrame.PixelDataLength);

            bitmapFrame.UnlockBits(bitmapData);
            colorFrame.Dispose();

            return bitmapFrame;
        }
        void Context_AllFramesUpdated(KinectSensor sensor, ColorImageFrame cf, DepthImageFrame df, SkeletonFrame sf)
        {
            this.sensor = sensor;
            if (colorImage == null)
            {
                colorImage = new byte[cf.PixelDataLength];
                depthImage = new short[df.PixelDataLength];
                skeletonData = new Skeleton[sf.SkeletonArrayLength];
            }

            cf.CopyPixelDataTo(colorImage);
            df.CopyPixelDataTo(depthImage);
            sf.CopySkeletonDataTo(skeletonData);

            TrackFace();
        }
        public static Image<Gray, Byte> cropImage(KinectSensor sensor, ColorImageFrame colorFrame, Skeleton skeleton)
        {
            Image<Bgr, byte> tarImage = null;

            Joint head = skeleton.Joints[JointType.Head];
            if (head.TrackingState == JointTrackingState.NotTracked) return null;

            ColorImagePoint headPoint = sensor.CoordinateMapper.MapSkeletonPointToColorPoint(head.Position, colorFrame.Format);

            byte[] pixelData = new byte[colorFrame.PixelDataLength];
            colorFrame.CopyPixelDataTo(pixelData);
            Bitmap bitmap = null;
            try
            {
                WriteableBitmap wBitmap = new WriteableBitmap(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null);
                Int32Rect rect = new Int32Rect(0, 0, colorFrame.Width, colorFrame.Height);
                int stride = colorFrame.Width * colorFrame.BytesPerPixel;
                wBitmap.WritePixels(rect, pixelData, stride, 0);
                bitmap = bitmapSourceToBitmap(wBitmap);

                long detectionTime;
                List<Rectangle> faces = new List<Rectangle>();
                List<Rectangle> eyes = new List<Rectangle>();

                Image<Bgr, byte> image = new Image<Bgr, byte>(bitmap);
                DetectFace.Detect(image, "haarcascade_frontalface_alt2.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime);

                foreach( var face in faces )
                {
                    if (face.X < headPoint.X && face.X + face.Width > headPoint.X && face.Y < headPoint.Y && face.Y + face.Height > headPoint.Y)
                    {
                        image.ROI = face;
                        tarImage = image.Copy();
                        tarImage = tarImage.Resize(200, 200, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            if (tarImage == null) return null;

            return tarImage.Convert<Gray, Byte>();
        }
        /// <summary>
        ///  プレーヤーだけ表示する
        /// </summary>
        /// <param name="colorFrame"></param>
        /// <param name="depthFrame"></param>
        /// <returns></returns>
        private byte[] BackgroundMask( KinectSensor kinect,
      ColorImageFrame colorFrame, DepthImageFrame depthFrame )
        {
            ColorImageStream colorStream = kinect.ColorStream;
              DepthImageStream depthStream = kinect.DepthStream;

              // RGBカメラのピクセルごとのデータを取得する
              byte[] colorPixel = new byte[colorFrame.PixelDataLength];
              colorFrame.CopyPixelDataTo( colorPixel );

              // 距離カメラのピクセルごとのデータを取得する
              short[] depthPixel = new short[depthFrame.PixelDataLength];
              depthFrame.CopyPixelDataTo( depthPixel );

              // 距離カメラの座標に対応するRGBカメラの座標を取得する(座標合わせ)
              ColorImagePoint[] colorPoint = new ColorImagePoint[depthFrame.PixelDataLength];
              kinect.MapDepthFrameToColorFrame( depthStream.Format, depthPixel,
            colorStream.Format, colorPoint );

              // 出力バッファ(初期値は白(255,255,255))
              byte[] outputColor = new byte[colorPixel.Length];
              for ( int i = 0; i < outputColor.Length; i += Bgr32BytesPerPixel ) {
            outputColor[i] = 255;
            outputColor[i + 1] = 255;
            outputColor[i + 2] = 255;
              }

              for ( int index = 0; index < depthPixel.Length; index++ ) {
            // プレイヤーを取得する
            int player = depthPixel[index] & DepthImageFrame.PlayerIndexBitmask;

            // 変換した結果が、フレームサイズを超えることがあるため、小さいほうを使う
            int x = Math.Min( colorPoint[index].X, colorStream.FrameWidth - 1 );
            int y = Math.Min( colorPoint[index].Y, colorStream.FrameHeight - 1 );
            int colorIndex = ((y * depthFrame.Width) + x) * Bgr32BytesPerPixel;

            // プレーヤーを検出した座標だけ、RGBカメラの画像を使う
            if ( player != 0 ) {
              outputColor[colorIndex] = colorPixel[colorIndex];
              outputColor[colorIndex + 1] = colorPixel[colorIndex + 1];
              outputColor[colorIndex + 2] = colorPixel[colorIndex + 2];
            }
              }

              return outputColor;
        }
Example #21
0
		public void Record(ColorImageFrame frame)
		{
			writer.Write((int) FrameType.Color);

			var timeSpan = DateTime.Now.Subtract(referenceTime);
			referenceTime = DateTime.Now;
			writer.Write((long) timeSpan.TotalMilliseconds);
			writer.Write(frame.BytesPerPixel);
			writer.Write((int) frame.Format);
			writer.Write(frame.Width);
			writer.Write(frame.Height);

			writer.Write(frame.FrameNumber);

			writer.Write(frame.PixelDataLength);
			var bytes = new byte[frame.PixelDataLength];
			frame.CopyPixelDataTo(bytes);
			writer.Write(bytes);
		}
Example #22
0
        protected unsafe void ProcessFrame(ColorImageFrame frame)
        {
            var bytes = new byte[frame.PixelDataLength];
            frame.CopyPixelDataTo(bytes);

            BitmapData bitmapData = this.CurrentValue.LockBits(new System.Drawing.Rectangle(0, 0, this.Width, this.Height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            byte* pDest = (byte*)bitmapData.Scan0.ToPointer();
            int pointer = 0;

            var maxIndex = this.Width * this.Height;
            for (int index = 0; index < maxIndex; index++) {
                pDest[0] = bytes[pointer];
                pDest[1] = bytes[pointer + 1];
                pDest[2] = bytes[pointer + 2];
                pDest += 3;
                pointer += 4;
            }
            this.CurrentValue.UnlockBits(bitmapData);
            this.OnNewDataAvailable();
        }
Example #23
0
        public static void WritePixelsForColorImageFrame(ColorImageFrame frame, WriteableBitmap bitmap)
        {
            if (frame == null)
            {
                //lost frame
                return;
            }

            if (bitmap == null)
            {
                throw new ArgumentNullException("bitmap", "WriteableBitmap cannot be null, create a new WriteableBitmap() before calling this method");
            }

            byte[] colorPixels = new byte[frame.PixelDataLength];
            frame.CopyPixelDataTo(colorPixels);
            Int32Rect imageSize = new Int32Rect(0,0,frame.Width,frame.Height);
            int stride = frame.Width * frame.BytesPerPixel; 

            bitmap.WritePixels(imageSize, colorPixels, stride, 0);
        }
Example #24
0
 public static Bitmap frameToImage(ColorImageFrame f)
 {
     byte[] pixeldata =
              new byte[f.PixelDataLength];
     f.CopyPixelDataTo(pixeldata);
     Bitmap bmap = new Bitmap(
            f.Width,
            f.Height,
            PixelFormat.Format32bppRgb);
     BitmapData bmapdata = bmap.LockBits(
       new Rectangle(0, 0,
                  f.Width, f.Height),
       ImageLockMode.WriteOnly,
       bmap.PixelFormat);
     IntPtr ptr = bmapdata.Scan0;
     Marshal.Copy(pixeldata, 0, ptr,
                f.PixelDataLength);
     bmap.UnlockBits(bmapdata);
     return bmap;
 }
        public void Update(ColorImageFrame frame)
        {
            var pixelData = new byte[frame.PixelDataLength];

            frame.CopyPixelDataTo(pixelData);

            if (Bitmap == null)
            {
                Bitmap = new WriteableBitmap(frame.Width, frame.Height,
                                             96, 96, PixelFormats.Bgr32, null);
            }

            int stride = Bitmap.PixelWidth * Bitmap.Format.BitsPerPixel / 8;
            Int32Rect dirtyRect = new Int32Rect(0, 0, Bitmap.PixelWidth, Bitmap.PixelHeight);

            if (frame.Format == ColorImageFormat.RawYuvResolution640x480Fps15)
            {
                if (yuvTemp == null)
                    yuvTemp = new int[frame.Width * frame.Height];

                int current = 0;

                for (int uyvyIndex = 0; uyvyIndex < pixelData.Length; uyvyIndex += 4)
                {
                    byte u = pixelData[uyvyIndex];
                    byte y1 = pixelData[uyvyIndex + 1];
                    byte v = pixelData[uyvyIndex + 2];
                    byte y2 = pixelData[uyvyIndex + 3];

                    yuvTemp[current++] = ConvertFromYUV(y1, u, v);
                    yuvTemp[current++] = ConvertFromYUV(y2, u, v);
                }

                Bitmap.WritePixels(dirtyRect, yuvTemp, stride, 0);
            }
            else
                Bitmap.WritePixels(dirtyRect, pixelData, stride, 0);

            RaisePropertyChanged(() => Bitmap);
        }
        private BitmapSource ObterImagemSensorRGB(ColorImageFrame quadro)
        {
            if (quadro == null) return null;

            using (quadro)
            {
                byte[] bytesImagem = new byte[quadro.PixelDataLength];
                quadro.CopyPixelDataTo(bytesImagem);

                if (chkEscalaCinza.IsChecked.HasValue && chkEscalaCinza.IsChecked.Value)
                    for (int indice = 0; indice < bytesImagem.Length; indice += quadro.BytesPerPixel)
                    {
                        byte maiorValorCor = Math.Max(bytesImagem[indice], Math.Max(bytesImagem[indice + 1], bytesImagem[indice + 2]));

                        bytesImagem[indice] = maiorValorCor;
                        bytesImagem[indice + 1] = maiorValorCor;
                        bytesImagem[indice + 2] = maiorValorCor;
                    }

                return BitmapSource.Create(quadro.Width, quadro.Height, 960, 960, PixelFormats.Bgr32, null, bytesImagem, quadro.Width * quadro.BytesPerPixel);
            }
        }
Example #27
0
        public void Record(ColorImageFrame frame)
        {
            // Header
            writer.Write((int)KinectRecordOptions.Color);

            // Data
            TimeSpan timeSpan = DateTime.Now.Subtract(referenceTime);
            referenceTime = DateTime.Now;
            writer.Write((long)timeSpan.TotalMilliseconds);
            writer.Write(frame.BytesPerPixel);
            writer.Write((int)frame.Format);
            writer.Write(frame.Width);
            writer.Write(frame.Height);

            writer.Write(frame.FrameNumber);

            // Bytes
            writer.Write(frame.PixelDataLength);
            byte[] bytes = new byte[frame.PixelDataLength];
            frame.CopyPixelDataTo(bytes);
            writer.Write(bytes);
        }
Example #28
0
 private Bitmap ColorImageFrameToBitmap(ColorImageFrame colorFrame)
 {
     byte[] pixelBuffer = new byte[colorFrame.PixelDataLength];
     colorFrame.CopyPixelDataTo(pixelBuffer);
     Bitmap bitmapFrame = ArrayToBitmap(pixelBuffer, colorFrame.Width, colorFrame.Height, PixelFormat.Format32bppRgb);
     return bitmapFrame;
 }
Example #29
0
 // Call this function inside AllFramesReady to display a color debugging feed
 void display_color_feed(ColorImageFrame colorFrame)
 {
     byte[] pixels = new byte[colorFrame.PixelDataLength];
     colorFrame.CopyPixelDataTo(pixels);
     int stride = colorFrame.Width * 4;
     debugImage.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
 }
Example #30
0
        /// <summary>
        /// Sets the desired picture box image.  Can do so for up to 4 images.
        /// </summary>
        /// <param name="imageIndex">The index of the picture box image to set.</param>
        /// <param name="frame">The Kinect's color image frame to use.</param>
        void setImage(int imageIndex, ColorImageFrame frame)
        {
            // Check that the frame is not empty.
            if (frame != null)
            {
                // Create an array of bytes to hold the data.
                byte[] pixelData = new byte[frame.PixelDataLength];
                // Copy the pixel data to the array.
                frame.CopyPixelDataTo(pixelData);

                // Create a bmp to hold the actual image.
                Bitmap bmp = new Bitmap(frame.Width, frame.Height, PixelFormat.Format32bppRgb);
                // Create the bitmap data to hold the data for the image.
                BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
                // Create the pointer to the bmp data.
                IntPtr ptr = bmpData.Scan0;
                // Then copy the actual data to the bmp.
                System.Runtime.InteropServices.Marshal.Copy(pixelData, 0, ptr, frame.PixelDataLength);
                // Unlock the bmp data bits so they can be used.
                bmp.UnlockBits(bmpData);
                // Switch through the image index.
                switch (imageIndex)
                {
                    case 0:
                        // Set the color image for the 1st picture box to the bmp image.
                        this.kinectPB1.Image = bmp;
                        break;
                    case 1:
                        // Set the color image for the 2nd picture box to the bmp image.
                        this.kinectPB2.Image = bmp;
                        break;
                    case 2:
                        // Set the color image for the 3rd picture box to the bmp image.
                        this.kinectPB3.Image = bmp;
                        break;
                    case 3:
                        // Set the color image for the 4th picture box to the bmp image.
                        this.kinectPB4.Image = bmp;
                        break;
                }

                // Dispose of the frame.
                frame.Dispose();
            }
        }
Example #31
0
        static Bitmap ImageToBitmap(
            ColorImageFrame image)
        {
            try
            {
                if (image != null)
                {
                    var pixeldata =
                        new byte[image.PixelDataLength];

                    image.CopyPixelDataTo(pixeldata);

                    var bmap = new Bitmap(
                        image.Width,
                        image.Height,
                        PixelFormat.Format32bppRgb);
                    BitmapData bmapdata = bmap.LockBits(
                        new Rectangle(0, 0,
                                      image.Width, image.Height),
                        ImageLockMode.WriteOnly,
                        bmap.PixelFormat);
                    var ptr = bmapdata.Scan0;
                    Marshal.Copy(pixeldata, 0, ptr,
                                 image.PixelDataLength);
                    bmap.UnlockBits(bmapdata);

                    return bmap;
                }
            }
            catch (Exception ex)
            {
                Log.Error("",ex);//MainForm.LogExceptionToFile(ex);
            }
            return null;
        }