示例#1
0
        public SerialInput()
        {
            InitializeComponent();

            // init the serial port
            serialPort = new SerialPort();

            // Set the read/write timeouts
            serialPort.ReadTimeout = -1;
            serialPort.WriteTimeout = -1;
            serialPort.Parity = Parity.None;
            serialPort.DataBits = 8;
            serialPort.StopBits = StopBits.Two;
            serialPort.Handshake = Handshake.None;
            serialPort.NewLine = "\r\n";

            // Linked to ui console
            uiconsole = MainForm.UIConsole;

            if (isColor)
            {
                imageMask = new FxMatrixMask(64, 64);
            }

            imageMask = new FxMatrixMask(64, 64);
            imageMaskColorMap = new ColorMap(ColorMapDefaults.Jet);

            // Create a visual view
            imageMaskView = new ImageElement(imageMask.ToFxMatrixF(), imageMaskColorMap);
            canvas1.AddElement(imageMaskView, false);

            imageView = new ImageElement(imageMask.ToFxMatrixF(), imageMaskColorMap);
            imageView._Position.x = imageMaskView.Size.X +  10f;
            imageView._Position.Y = 0;
            canvas1.AddElement(imageView, false);

            canvas1.FitView();

            // add the timer for the fps measure
            fpsTimer = new System.Windows.Forms.Timer();
            fpsTimer.Interval = 1000;
            watch.Start();

            fpsTimer.Tick += (s, te) =>
            {
                watch.Stop();
                float fps = fpsCount * 1000.0f / watch.ElapsedMilliseconds;
                //uiconsole.WriteLine("FPS:" + fps.ToString());
                //Console.WriteLine("FPS:" + fps.ToString());
                fpsLabel.Text = fps.ToString();
                fpsCount = 0;
                watch.Reset();
                watch.Start();
            };
            fpsTimer.Start();
        }
示例#2
0
        private void button_SaveImage_Click(object sender, EventArgs e)
        {
            sensor.Stop();
            SaveFileDialog sfd = new SaveFileDialog();
            if(sfd.ShowDialog()== System.Windows.Forms.DialogResult.OK){
                ColorMap map = new ColorMap(ColorMapDefaults.Gray);
                depthImageMatrix.SaveImage(sfd.FileName + "_depth.jpg", map);
                colorImageMatrix.SaveImage(sfd.FileName + "_color.jpg", map);

                depthImageMatrix.SaveCsv(sfd.FileName + "_depth.csv");
                depthImageMatrixAve.SaveCsv(sfd.FileName + "_ave_depth.csv");
            }
            sensor.Start();
        }
示例#3
0
        public ImageElement(Matrix.FxMatrixF mat, ColorMap map)
        {
            InitToolStrips();

            // set the position and the size of the element
            Position = new Vector.FxVector2f(0);
            Size = new Vector.FxVector2f(mat.Width, mat.Height);

            // set the size of the image
            Width = mat.Width;
            Height = mat.Height;

            // allocate the memory for the internal image
            internalImage = new byte[mat.Width * mat.Height * 4];
            Pitch = mat.Width * 4;

            UpdateInternalImage(mat, map);
        }
示例#4
0
        public SerialCapture()
        {
            InitializeComponent();

            LoadConfigurations();

            imageMask = new FxMatrixMask(64, 64);
            imageMaskColorMap = new ColorMap(ColorMapDefaults.Jet);

            // Create a visual view
            imageMaskView = new ImageElement(imageMask.ToFxMatrixF(), imageMaskColorMap);
            canvas1.AddElement(imageMaskView, false);

            imageView = new ImageElement(imageMask.ToFxMatrixF(), imageMaskColorMap);
            imageView._Position.x = imageMaskView.Size.X + 10f;
            imageView._Position.Y = 0;
            canvas1.AddElement(imageView, false);

            canvas1.FitView();
        }
示例#5
0
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ColorMap map = new ColorMap(ColorMapDefaults.Gray);
                lock (depthImageMatrix)
                {
                    depthImageMatrix.SaveImage(sfd.FileName + "_depth.jpg", map);
                    depthImageMatrix.SaveCsv(sfd.FileName + "_depth.csv");
                }


                lock(depthImageMatrixAve)
                {
                    depthImageMatrixAve.SaveImage(sfd.FileName + "_ave_depth.jpg", map);
                    depthImageMatrixAve.SaveCsv(sfd.FileName + "_ave_depth.csv");
                }
            }

        }
示例#6
0
        /// <summary>
        /// Handles the depth frame data arriving from the sensor
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void Reader_FrameArrived(object sender, DepthFrameArrivedEventArgs e)
        {
            DepthFrameReference frameReference = e.FrameReference;

            if (this.startTime == null)
            {
                this.startTime = frameReference.RelativeTime;
            }

            try
            {
                DepthFrame frame = frameReference.AcquireFrame();

                if (frame != null)
                {
                    // DepthFrame is IDisposable
                    using (frame)
                    {
                        FrameDescription frameDescription = frame.FrameDescription;

                        #region FPS
                        this.framesSinceUpdate++;

                        // update status unless last message is sticky for a while
                        if (DateTime.Now >= nextStatusUpdate)
                        {
                            // calcuate fps based on last frame received
                            double fps = 0.0;

                            if (stopwatch.IsRunning)
                            {
                                stopwatch.Stop();
                                fps = framesSinceUpdate / stopwatch.Elapsed.TotalSeconds;
                                stopwatch.Reset();
                            }

                            nextStatusUpdate = DateTime.Now + TimeSpan.FromSeconds(1);
                            toolStripLabel_fps.Text = fps + " " + (frameReference.RelativeTime - this.startTime).ToString() + "mS";
                        }

                        if (!stopwatch.IsRunning)
                        {
                            framesSinceUpdate = 0;
                            stopwatch.Start();
                        } 
                        #endregion

                        // verify data and write the new depth frame data to the display bitmap
                        if (((frameDescription.Width * frameDescription.Height) == frameData.Length))
                        {
                            // Copy the pixel data from the image to a temporary array
                            frame.CopyFrameDataToArray(frameData);

                            coordinateMapper.MapDepthFrameToColorSpace(frameData, colorPoints);

                            // Get the min and max reliable depth for the current frame
                            ushort minDepth = frame.DepthMinReliableDistance;
                            ushort maxDepth = frame.DepthMaxReliableDistance;
                            float imaxDepth = 1.0f / maxDepth;
                            for (int i = 0; i < this.frameData.Length; ++i)
                            {
                                // Get the depth for this pixel
                                ushort depth = this.frameData[i];

                                // 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).
                                //pixels[i] = 1.0f - ((depth >= minDepth && depth <= maxDepth) ? depth : 0) * imaxDepth;
                                depthImageMatrix[i] = 1.0f - depth * imaxDepth;
                            }

                            if (!oneTimeShot)
                            {
                                oneTimeShot = true;
                                ColorMap cm = new ColorMap(ColorMapDefaults.DeepBlue);
                                List<FxVector3f> Points = new List<FxVector3f>();
                                List<FxVector3f> Colors = new List<FxVector3f>();
                                for (int x = 0; x < frameDescription.Width; x++)
                                {
                                    for (int y = 0; y < frameDescription.Height; y++)
                                    {
                                        FxVector3f p;
                                        p.x = x * 0.08f;
                                        p.z = y * 0.08f;
                                        p.y = depthImageMatrix[x,y]*5.0f;
                                        Points.Add(p);

#if false
                                        // calculate index into depth array
                                        int depthIndex = (x * frameDescription.Width) + y;

                                        // retrieve the depth to color mapping for the current depth pixel
                                        ColorSpacePoint colorPoint = this.colorPoints[depthIndex];

                                        // make sure the depth pixel maps to a valid point in color space
                                        int colorX = (int)Math.Floor(colorPoint.X + 0.5);
                                        int colorY = (int)Math.Floor(colorPoint.Y + 0.5);

                                        // calculate index into color array
                                        int colorIndex = ((colorY * colorWidth) + colorX) * this.bytesPerPixel;


#else
                                        byte b = (byte)(depthImageMatrix[x, y]*256);
                                        Colors.Add(new FxVector3f(cm[b,0]/256.0f, cm[b,1]/256.0f, cm[b,2]/256.0f));
#endif
                                    }
                                }

                                PointCloud pc = new PointCloud(Points, Colors);

                                /// add the mesh to the engine mesh list
                                Engine.g_MeshManager.AddMesh(pc);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                // ignore if the frame is no longer available
            }
        }
示例#7
0
        public void UpdateInternalImage(Matrix.FxMatrixF mat, ColorMap map, bool useInvMap = false)
        {
            // link the extran matrix with the internal
            internalMatrix = mat;

            // check if we need to create a new internal buffer
            if (mat.Width != Width || mat.Height != Height)
            {
                lock (this)
                {
                    // set the properties of the image
                    BitmapProperties bitmapProps = new BitmapProperties();
                    bitmapProps.PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);

                    // set the size of the image
                    Width = mat.Width;
                    Height = mat.Height;

                    if (mImageBitmap != null)
                    {
                        mImageBitmap.Dispose();
                    }

                    // make the bitmap for Direct2D1
                    mImageBitmap = new SharpDX.Direct2D1.Bitmap(this.Parent.RenderVariables.renderTarget,
                                                                new Size2(Width, Height),
                                                                bitmapProps);
                }
            }

            unsafe
            {
                try
                {
                    int size = Width * Height;
                    fixed (byte* dst = internalImage)
                    {
                        fixed (float* src = mat.Data)
                        {
                            byte* pDst = dst;
                            float* pSrc = src;

                            if (useInvMap)
                            {
            #if false
                                float* pSrcEnd = pSrc + mat.Size;
                                for (; pSrc < pSrcEnd; pSrc++)
                                {
                                    byte id = (byte)(255 - *(pSrc) * 255);
                                    *(pDst++) = map[id, 2];
                                    *(pDst++) = map[id, 1];
                                    *(pDst++) = map[id, 0];
                                    *(pDst++) = 255;
                                }
            #else
                                int step = size / 8;
                                Parallel.For(0, 8, (s) =>
                                {
                                    int end = (s + 1) * step;
                                    for (int i = s * step; i < end; i++)
                                    {
                                        byte id = (byte)(255 - *(pSrc + i) * 255);
                                        int i4 = i * 4;
                                        *(pDst + i4) = map[id, 2];
                                        *(pDst + i4 + 1) = map[id, 1];
                                        *(pDst + i4 + 2) = map[id, 0];
                                        *(pDst + i4 + 3) = 255;

                                    }
                                });
            #endif
                            }
                            else
                            {
            #if false

                                float* pSrcEnd = pSrc + mat.Size;
                                for(; pSrc < pSrcEnd; pSrc++) {
                                    byte id = (byte)(*(pSrc) * 255);
                                    *(pDst++) = map[id, 2];
                                    *(pDst++) = map[id, 1];
                                    *(pDst++) = map[id, 0];
                                    *(pDst++) = 255;
                                }
            #else
                                int step = size / 8;
                                Parallel.For(0, 8, (s) =>
                                {
                                    int end = (s + 1) * step;
                                    for (int i = s * step; i < end; i++)
                                    {
                                        byte id = (byte)(*(pSrc + i) * 255);
                                        int i4 = i * 4;
                                        *(pDst + i4) = map[id, 2];
                                        *(pDst + i4 + 1) = map[id, 1];
                                        *(pDst + i4 + 2) = map[id, 0];
                                        *(pDst + i4 + 3) = 255;

                                    }
                                });
            #endif
                            }
                        }
                    }

                    // write to the specific bitmap not create a new one
                    mImageBitmap.CopyFromMemory(internalImage, Width * 4);
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
            }
        }
示例#8
0
文件: ColorMap.cs 项目: fxbit/FxMath
        public static ColorMap GetColorMap(ColorMapDefaults colorMap)
        {
            if (cacheColorMap.ContainsKey(colorMap))
                return cacheColorMap[colorMap];

            var value = new ColorMap(colorMap);
            cacheColorMap.Add(colorMap, value);
            return value;
        }