Example #1
0
        public Form1()
        {
            InitializeComponent();

            resize = new FxVector2f(targetRes.x / (float)origRes.x, targetRes.y / (float)origRes.y);

            // create a shop
            shopPlan = FxMatrixF.Load("Katopsi.jpg", ColorSpace.Grayscale);
            shopPlan = shopPlan.Resize(targetRes.x, targetRes.y);
            shop = new Shop("Unisol", shopPlan);

               // simulation = new SimulationSimple(shop, 10, simulationMethod1);

            simulationMethod2_Setup();
            simulation = new SimulationSimple(shop, 1, simulationMethod2);

            // Defind entrance for the shop
            shop.entrancePositionsDirection.Add(new Tuple<FxVector2f, FxVector2f>(new FxVector2f(1800 * resize.x, 500 * resize.y), new FxVector2f(-1, 0)));

            // load a person image for the moving image
            imPerson = FxMatrixF.Load("person.jpg", ColorSpace.Grayscale);
               // imPerson.Exec(x => (x > 0.1) ? 0 : 1);
            imPerson.MedianFilt();
            imPersonMask = imPerson < 0.5f;

            // Init the matrix to be HD
            mat = new FxMatrixF(targetRes.x, targetRes.y);
            mat.DrawMatrix(shopPlan, new FxVector2f(0, 0), new FxVector2f(mat.Width, mat.Height), FxMatrixF.DrawInterpolationMethod.NearestNeighbor);

            // Add image element to canvas to showing the matrix
            im = new ImageElement(mat);
            canvas1.AddElement(im, true);
        }
Example #2
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();
        }
Example #3
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();
        }
Example #4
0
        public FxContour(FxMatrixMask mask, int minPix = 0, int maxPix = int.MaxValue)
        {
            int maskSize = mask.Width * mask.Height;
            var stack = new Stack<Tuple<int, int>>(1000);
            var remainMask = mask.Copy();
            var labelMap = new FxMatrixF(mask.Width, mask.Height);
            ChainList = new List<FxContourChain>();

            int labelCount = 1;
            var maskG = mask.ToFxMatrixF().Gradient(FxMatrixF.GradientMethod.Roberts) > 0;
            for (int i = 1; i < maskSize - 1; i++)
            {
                /* check if we have edge */
                if (maskG[i])
                {
                    int x;
                    int y = Math.DivRem(i, mask.Width, out x);
                    labelMap[x, y] = labelCount;
                    maskG[x, y] = false;

                    // create a new chain
                    FxContourChain newChain = new FxContourChain(x, y);

                    /* propacate the search in sub pixels */
                    Labeling_addStack(stack, x, y);
                    while (stack.Count > 0)
                    {
                        var dxy = stack.Pop();
                        x = dxy.Item1;
                        y = dxy.Item2;

                        if (x < 1 || (x >= mask.Width - 1) || y < 1 || (y >= mask.Height - 1))
                            continue;

                        if (maskG[x, y] && check(maskG, x, y))
                        {
                            // add the labeling
                            labelMap[x, y] = labelCount;
                            maskG[x, y] = false;

                            // add the new point to the chain
                            newChain.PushPoint(x, y);

                            Labeling_addStack(stack, x, y);
                        }
                    }
                    labelCount++;

                    // calc boundary rect

                    // add the new chain to the list
                    ChainList.Add(newChain);
                }
            }

            // filter the chain based on min/max size
            if (minPix != 0 || maxPix != int.MaxValue)
            {
                List<FxContourChain> tmpChainList = ChainList;
                ChainList = new List<FxContourChain>();
                foreach(FxContourChain c in tmpChainList)
                {
                    if (c.Count > minPix && c.Count < maxPix)
                        ChainList.Add(c);
                }
            }
        }
Example #5
0
 private bool check(FxMatrixMask mask, int x, int y)
 {
     return !(mask[x, y - 1] &&
              mask[x, y + 1] &&
              mask[x + 1, y - 1] &&
              mask[x + 1, y] &&
              mask[x + 1, y + 1] &&
              mask[x - 1, y - 1] &&
              mask[x - 1, y] &&
              mask[x - 1, y + 1]);
 }
Example #6
0
        private void KinectForm_Load(object sender, EventArgs e)
        {
            // Look through all sensors and start the first connected one.
            // This requires that a Kinect is connected at the time of app startup.
            // To make your app robust against plug/unplug,
            // it is recommended to use KinectSensorChooser provided in Microsoft.Kinect.Toolkit
            foreach(var potentialSensor in KinectSensor.KinectSensors) {
                if(potentialSensor.Status == KinectStatus.Connected) {
                    this.sensor = potentialSensor;
                    break;
                }
            }

            if(null == this.sensor) {
                this.Text = "No kinnect Ready";
                return;
            }

            Size depthImageSize = GetImageSize(DepthFormat);
            depthWidth = (int)depthImageSize.Width;
            depthHeight = (int)depthImageSize.Height;

            Size colorImageSize = GetImageSize(ColorFormat);
            colorWidth = (int)colorImageSize.Width;
            colorHeight = (int)colorImageSize.Height;

            // Turn on the depth and color streams to receive frames
            sensor.DepthStream.Enable(DepthFormat);
            sensor.ColorStream.Enable(ColorFormat);
            sensor.DepthStream.Range = DepthRange.Near;

            frameDataLength = sensor.DepthStream.FramePixelDataLength;

            sensor.AllFramesReady += sensor_AllFramesReady;

            int depthImageArraySize = depthWidth * depthHeight;
            int colorImageArraySize = colorWidth * colorHeight * sizeof(int);

            // Create local color pixels buffer
            colorImagePixels = new byte[colorImageArraySize];

            // Create local depth pixels buffer
            depthImagePixels = new DepthImagePixel[depthImageArraySize];

            // Allocate the depth-color mapping points
            colorCoordinates = new ColorImagePoint[depthImageArraySize];

            // allocate the Matrix
            colorImageMatrix = new FxMatrixF(colorWidth, colorHeight);
            depthImageMatrix = new FxMatrixF(depthWidth, depthHeight);
            depthImageMatrixAve = new FxMatrixF(depthWidth, depthHeight);

            // create a new image element
            colorImageElement = new ImageElement(colorImageMatrix);
            depthImageElement = new ImageElement(depthImageMatrix);
            depthImageElement.MouseClickEvent += depthImageElement_MouseClickEvent;
            canvas1.AddElements(colorImageElement,false);
            canvas1.AddElements(depthImageElement, false);
            depthImageElement.Position = new FxMaths.Vector.FxVector2f(colorWidth, 0);

            G = new FxMatrixMask(depthWidth, depthHeight, false);
            s = new FxMatrixF(depthWidth, depthHeight, 1f);
            m = new FxMatrixF(depthWidth, depthHeight, 1f);

            // init the dispatcher class
            dispatcher = Dispatcher.CurrentDispatcher;

            // Start the sensor!
            try {
                this.sensor.Start();
            } catch(IOException ex) {
                // Device is in use
                this.sensor = null;
                this.Text = ex.Message;

                return;
            } catch(InvalidOperationException ex) {
                // Device is not valid, not supported or hardware feature unavailable
                this.sensor = null;
                this.Text = ex.Message;

                return;
            }

            // Initialize and start the FPS timer
            this.fpsTimer = new DispatcherTimer();
            this.fpsTimer.Tick += new EventHandler(this.FpsTimerTick);
            this.fpsTimer.Interval = new TimeSpan(0, 0, 1);

            this.fpsTimer.Start();

            this.lastFPSTimestamp = DateTime.UtcNow;
        }
Example #7
0
        /// <summary>
        /// Draw external matrix to this matrix.
        /// </summary>
        /// <param name="matrix">The matrix that store the copied image</param>
        /// <param name="start"></param>
        /// <param name="size"></param>
        public void DrawMatrix(FxMatrixF matrix, 
            FxMatrixMask mask,
            FxVector2f start,
            FxVector2f size,
            DrawInterpolationMethod method)
        {
            int h = matrix.Height;
            int w = matrix.Width;

            float stepW = (float)(w - 1) / size.x;
            float stepH = (float)(h - 1) / size.y;

            start.x = (float)Math.Floor(start.x);
            start.y = (float)Math.Floor(start.y);

            FxVector2f end = start + size;
            if (end.x > Width)
                end.x = Width - 1;
            if (end.y > Height)
                end.y = Height - 1;

            switch (method)
            {
                case DrawInterpolationMethod.NearestNeighbor:

                    Parallel.For((int)start.y, (int)end.y, (j) =>
                    {
                        int yp = (int)Math.Floor((j - start.y) * stepH);
                        for (int i = (int)start.x, ix = 0; i < end.x; i++, ix++)
                        {
                            int xp = (int)Math.Floor(ix * stepW);
                            if (mask[xp,yp])
                                this[i, j] = matrix[xp, yp];
                        }
                    });

                    break;

                case DrawInterpolationMethod.Linear:

                    Parallel.For((int)start.y, (int)end.y, (j) =>
                    {
                        float yp = (j - start.y) * stepH;
                        int yp_n = (int)Math.Floor((j - start.y) * stepH);
                        for (int i = (int)start.x, ix = 0; i < end.x; i++, ix++)
                        {
                            float xp = ix * stepW;
                            int xp_n = (int)Math.Floor(ix * stepW);
                            if ((xp < matrix.Width - 1) && (mask[xp_n, yp_n]))
                                this[i, j] = matrix.Sample(xp, yp);
                        }
                    });

                    break;

                case DrawInterpolationMethod.Cubic:

                    Parallel.For((int)start.y, (int)end.y, (j) =>
                    {
                        float yp = (j - start.y) * stepH;
                        int yp_n = (int)Math.Floor((j - start.y) * stepH);
                        for (int i = (int)start.x, ix = 0; i < end.x; i++, ix++)
                        {
                            float xp = ix * stepW;
                            int xp_n = (int)Math.Floor(ix * stepW);
                            if ((xp < matrix.Width - 1) && (mask[xp_n, yp_n]))
                                this[i, j] = matrix.SampleCubic(xp, yp);
                        }
                    });

                    break;
            }
        }
Example #8
0
        public FxBlobTracker(FxMatrixF firstFrame)
        {
            m = firstFrame.Copy();
            s = new FxMatrixF(firstFrame.Width, firstFrame.Height,0.05f);
            G = m != -1; /* force a mask with all 1 */
            a = 0.005f;

            G_small = new FxMatrixMask(G_small_width, G_small_height);
            step_w = (int)Math.Ceiling(G.Width / (float)G_small_width);
            step_h = (int)Math.Ceiling(G.Height / (float)G_small_height);
            cG_thd = step_w * step_h / 2;

            ListBlobs = new List<FxBlob>();

            numProcessingFrames = 0;
        }
Example #9
0
        public void Process(FxMatrixMask mask)
        {
            // filter out any small points
            G_small = mask.MedianFilt(6,6);

            // create the contours of the current frame
            var contours = new FxContour(G_small);
            var listContour = contours.ChainList;

            // update the contours
            foreach (FxBlob b in ListBlobs)
            {
                Stack<FxContourChain> chainStack = new Stack<FxContourChain>();
                FxVector2f newCenter = b.Center;
                float newRadius = 0;
                FxContourChain selected = null;

                // find all the chains that are common with the old blob
                foreach (FxContourChain c in listContour)
                {
                    var c_center = c.GetCentroid();
                    var c_radius = c.GetMaxDist(c_center);

                    if (c_center.Distance(ref b.Center) < c_radius + b.Radius)
                    {
                        chainStack.Push(c);
                        newCenter.x = (newCenter.x + c_center.x) / 2.0f;
                        newCenter.y = (newCenter.y + c_center.y) / 2.0f;
                    }
                }

                // find the biger radius
                foreach (FxContourChain c in chainStack)
                {
                    var r = c.GetMaxDist(newCenter);
                    if (newRadius < r)
                    {
                        selected = c;
                        newRadius = r;
                    }
                }

                // remove the chains that we have find that can be used
                listContour.RemoveAll(c => chainStack.Contains(c));

                if (newRadius > 2*smallerRadius)
                    continue;

                // update the blob
                if (selected != null)
                {
                    b.LastContour = selected;
                    b.Center = newCenter;
                    if (b.Radius < newRadius)
                        b.Radius = (newRadius > smallerRadius) ? smallerRadius : newRadius;

                    b.NumOfElements = G_small.NumNZ(selected.RectStart, selected.RectSize);
                    b.UnSeenCount = 0;
                }

                chainStack.Clear();
            }

            // remove old blobs
            Stack<FxBlob> oldBlob = new Stack<FxBlob>();
            foreach (FxBlob b in ListBlobs)
            {
                if (G_small.NumNZ(b.Center, b.Radius) == 0)
                    b.UnSeenCount++;
                else
                    b.UnSeenCount = 0;

                if (b.UnSeenCount > 5)
                    oldBlob.Push(b);
            }
            ListBlobs.RemoveAll(c => oldBlob.Contains(c));

            // add a new blobs
            ListBlobs.AddRange(
                listContour.Where(x =>
                            {
                                var size = x.RectSize.x * x.RectSize.y;
                                return (size > 40 && size < 200);
                            })
                           .Select(x =>
                           {
                               var b = new FxBlob();
                               b.LastContour = x;
                               b.Center = x.GetCentroid();
                               b.Radius = x.GetMaxDist(b.Center);
                               if (b.Radius > smallerRadius)
                                   b.Radius = smallerRadius;
                               b.NumOfElements = G_small.NumNZ(x.RectStart, x.RectSize);
                               b.UnSeenCount = 0;
                               return b;
                           })
                           );

            // increase the counter
            numProcessingFrames++;
        }