Esempio n. 1
0
        private void Read()
        {
            int count = 0;
            Byte[] buffer = new Byte[130];

            int numBytes = (isColor) ? 130 : 10;

            Byte[,] imageBytes = new Byte[130, numBytes];

            int row_id = 0;

            FxBlobTracker fxtracker = new FxBlobTracker(imageMask.ToFxMatrixF());
            FxMatrixF image = new FxMatrixF(64,64);

            while (_continue)
            {
                try
                {
                    // Read one row
                    row_id = readRow(buffer, numBytes) - 32;

                    // save the row
                    if (row_id >= 0 &&
                        row_id < 256)
                    {
                        for (int i = 0; i < numBytes; i++)
                            imageBytes[row_id, i] = buffer[i];
                    }

                    // Show results
                    if (isColor)
                    {
                        if (row_id == 0)
                        {
                            for (int i = 0; i < 64; i++)
                            {
                                for (int j = 0; j < 64; j++)
                                {

                                    byte c = imageBytes[i, j * 2];
                                    byte d = imageBytes[i, j * 2 + 1];

                                    byte r = (byte)(c & 0xF8);
                                    byte g = (byte)(((c & 0x07) << 5) + ((d & 0xE0) >> 3));
                                    byte b = (byte)((d & 0x1F) << 3);

                                    // Select the bit
                                    if (false)
                                    {
                                        // RGB332
                                        image[j, i] = ((r & 0xE0) |
                                                        ((g >> 3) & 0x1C) |
                                                        ((b >> 6) & 0x03)
                                                       ) / 255.0f;
                                    }
                                    else
                                    {
                                        image[j, i] = ((b * 0.3f) + (g * 0.59f) + (r * 0.11f)) / 255.0f;
                                        if (image[j, i] > 1.0f)
                                            image[j, i] = 1.0f;
                                    }
                                }
                            }

                            // Update the show image
                            imageMaskView.UpdateInternalImage(image, imageMaskColorMap);

                            /* refresh images */
                            fpsCount++;
                            canvas1.ReDraw();
                        }
                    }
                    else
                    {
                        if (row_id == 63)
                        {
                            //Console.WriteLine("Read Image");

                            for (int i = 0; i < 64; i++)
                            {
                                int bindex = 0;
                                for (int j = 0; j < 64; j++)
                                {
                                    byte b = imageBytes[i, bindex];

                                    // Select the bit
                                    imageMask[j, i] = ((b & (1 << 7 - j % 8)) > 0);

                                    // Move to the next byte
                                    if (j % 8 == 7)
                                        bindex++;
                                }
                            }
                            /* process the new matrix */
                            //fxtracker.Process(imageMask.ToFxMatrixF());
                            fxtracker.Process(imageMask);

                            image = imageMask.ToFxMatrixF();

                            var blobs = new FxContour(fxtracker.G_small);
                            var result = blobs.ToFxMatrixF(64, 64);

                            foreach (FxBlob b in fxtracker.ListBlobs)
                            {
                                result.DrawCircle(b.Center, b.Radius, 0.5f);
                                image.DrawCircle(b.Center, b.Radius, 0.5f);
                            }

                            // Update the show image
                            imageMaskView.UpdateInternalImage(image, imageMaskColorMap);
                            imageView.UpdateInternalImage(result, imageMaskColorMap);

                            /* refresh images */
                            fpsCount++;
                            canvas1.ReDraw();
                        }
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
            }

            Console.WriteLine(count);
        }
Esempio n. 2
0
        private void toolStripButton_ellipse_extract_Click(object sender, EventArgs e)
        {
            // extract the rectangle that contain 
            if ((imMat as object != null) &&
                (rect != null))
            {
                int numLabels;

                // get the sub matrix
                var subMat = imMat.GetSubMatrix(rect.StartPoint, rect.EndPoint) as FxMatrixF;

                // make binary the image based on the start and end point of rectangle
                float t = subMat[0, 0];
                if (t > subMat[subMat.Width - 1, subMat.Height - 1])
                    t = subMat[subMat.Width - 1, subMat.Height - 1];
                var binMat = subMat < t - 0.02f;
                
                // find the labels of the image
                var labels = binMat.Labeling(out numLabels);

                var imSub = new ImageElement(binMat.ToFxMatrixF(), new ColorMap(ColorMapDefaults.Jet));
                imSub._Position.x = ieEllipseImage.Size.x + ieEllipseImage.Position.x;
                imSub.lockMoving = true;
                canvas_ellipse.AddElement(imSub, false, false);

                var imSub2 = new ImageElement(labels, new ColorMap(ColorMapDefaults.Jet));
                imSub2._Position.x = ieEllipseImage.Size.x + ieEllipseImage.Position.x;
                imSub2._Position.y = imSub.Size.y + imSub.Position.y;
                imSub2.lockMoving = true;
                canvas_ellipse.AddElement(imSub2, false, false);

                WriteLine("Num Labels : " + numLabels.ToString());

                var contours = new FxContour(binMat, 10, 300);

                WriteLine("Num Contours : " + contours.NumChains);

                results = new FxMatrixF(3, contours.NumChains);

                int i=0;
                foreach (var x in contours.ChainList)
                {
                    float delta = 1;
                    // draw the rectanges in the sub image
                    FxVector2f start = x.RectStart + imSub2._Position + new FxVector2f(1, 1);
                    FxVector2f end = start + x.RectSize;
                    FxMaths.Geometry.Rectangle r = new FxMaths.Geometry.Rectangle(start, end);
                    gpeEllipseImage.AddGeometry(r, false);


                    // draw the rectanges in main image
                    start = x.RectStart + rect.StartPoint + new FxVector2f(-delta, -delta);
                    end = start + x.RectSize + new FxVector2f(2 * delta, 2 * delta);
                    r = new FxMaths.Geometry.Rectangle(start, end);
                    gpeEllipseImage.AddGeometry(r, false);


                    // draw the centroids 
                    FxVector2f cen = x.GetCentroid() + rect.StartPoint;
                    var l = new FxMaths.Geometry.Line(cen - new FxVector2f(0, 2), cen + new FxVector2f(0, 2));
                    l.LineWidth = 0.5f;
                    gpeEllipseImage.AddGeometry(l, false);
                    l = new FxMaths.Geometry.Line(cen - new FxVector2f(2, 0), cen + new FxVector2f(2, 0));
                    l.LineWidth = 0.5f;
                    gpeEllipseImage.AddGeometry(l, false);


                    // calculate the depth
                    float[] depth = new float[4];
                    FxVector2f pos = x.RectStart + rect.StartPoint;
                    depth[0] = imMat[pos.x - delta, pos.y - delta];
                    depth[1] = imMat[pos.x + x.RectSize.x + delta, pos.y - delta];
                    depth[2] = imMat[pos.x - delta, pos.y + x.RectSize.y + delta];
                    depth[3] = imMat[pos.x + x.RectSize.x + delta, pos.y + x.RectSize.y + delta];
                    results[2, i] = (depth[0] + depth[1] + depth[2] + depth[3]) / 4.0f;


                    // save centroid
                    results[0, i] = cen.x;
                    results[1, i] = cen.y;

                    // print the centroid
                    WriteLine("[" + i.ToString() + "] Depth:" + results[2, i].ToString() + "  Pixels:" + x.Count + " Centroid: " + cen.ToString());
                    i++;


                    // show the vector of one blob
                    if (i == 1)
                    {
                        FxVectorF vec_i = new FxVectorF(x.Count);
                        FxVectorF vec_r = new FxVectorF(x.Count);
                        vec_i[0] = x[0].i;
                        vec_r[0] = x[0].r;
                        for (int j = 1; i < x.Count; j++)
                        {
                            vec_i[j] = vec_i[j - 1] + x[j].i;
                            vec_r[j] = vec_r[j - 1] + x[j].r;
                        }


                    }
                }

                canvas_ellipse.ReDraw();

                results.SaveCsv("ellipseResults.csv");

            }
        }
Esempio n. 3
0
        private void ellipseDetectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                // load image
                imMat = FxMatrixF.Load(ofd.FileName);
                ieEllipseImage = new ImageElement(imMat, new ColorMap(ColorMapDefaults.Jet));
                ieEllipseImage.lockMoving = true;
                canvas_ellipse.AddElement(ieEllipseImage);

                // add plot element 
                gpeEllipseImage = new GeometryPlotElement();
                canvas_ellipse.AddElement(gpeEllipseImage);


                var contours = new FxContour(imMat<0.2f);

                WriteLine("Num Contours : " + contours.NumChains);

                int i = 0;
                float pe_pos_y = ieEllipseImage._Position.y;
                foreach (var cont in contours.ChainList)
                {
                    // draw the rectanges in main image
                    FxVector2f start = cont.RectStart ;
                    FxVector2f end = start + cont.RectSize;
                    FxMaths.Geometry.Rectangle r = new FxMaths.Geometry.Rectangle(start, end);
                    gpeEllipseImage.AddGeometry(r, false);


                    // draw the centroids 
                    FxVector2f cen = cont.GetCentroid();
                    var l = new FxMaths.Geometry.Line(cen - new FxVector2f(0, 2), cen + new FxVector2f(0, 2));
                    l.LineWidth = 0.5f;
                    gpeEllipseImage.AddGeometry(l, false);
                    l = new FxMaths.Geometry.Line(cen - new FxVector2f(2, 0), cen + new FxVector2f(2, 0));
                    l.LineWidth = 0.5f;
                    gpeEllipseImage.AddGeometry(l, false);


                    // add the numbering of the contours
                    var t = new TextElement(i.ToString());
                    t._Position = cen;
                    t.FontColor = SharpDX.Color.Green;
                    t._TextFormat.fontSize = 16.0f;
                    canvas_ellipse.AddElement(t);

                    // show the chain vector in plot
                    {
                        FxVectorF vec_i = new FxVectorF(cont.Count);
                        FxVectorF vec_r = new FxVectorF(cont.Count);
                        vec_i[0] = cont[0].i;
                        vec_r[0] = cont[0].r;
                        for (int j = 1; j < cont.Count; j++)
                        {
                            vec_i[j] = vec_i[j - 1] + cont[j].i;
                            vec_r[j] = vec_r[j - 1] + cont[j].r;
                        }

                        // show  the plot of this vector
                        var pe = new PloterElement(vec_i, PlotType.Lines, System.Drawing.Color.Blue);
                        pe._Position.x = ieEllipseImage._Position.x + ieEllipseImage.Size.x;
                        pe._Position.y = pe_pos_y;
                        pe.AddPlot(vec_r, PlotType.Lines, System.Drawing.Color.Red);
                        pe.CenterYOrigin();
                        pe.FitPlots();
                        canvas_ellipse.AddElement(pe);

                        // update the y of pe for the next one
                        pe_pos_y += pe.Size.y;

                        // debug the ellipse

                        for (int j = 0; j < cont.Count; j++)
                        {
                            imMat[(int)(vec_r[j] + cont.StartPoint.x), (int)(vec_i[j] + cont.StartPoint.y)] = 0.8f/contours.NumChains + 0.1f;
                        }
                        ieEllipseImage.UpdateInternalImage(imMat, new ColorMap(ColorMapDefaults.Jet));

                    }



                    i++;
                }

                canvas_ellipse.ReDraw();
            }
        }
Esempio n. 4
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++;
        }