Example #1
0
        /// <summary>
        ///   Gets two points defining the axis of the object.
        /// </summary>
        ///
        public LineSegment GetAxis(AxisOrientation axis)
        {
            double x1, y1;
            double x2, y2;

            if (axis == AxisOrientation.Horizontal)
            {
                y1 = Math.Cos(-Angle - Math.PI) * Rectangle.Height / 2.0;
                x1 = Math.Sin(-Angle - Math.PI) * Rectangle.Width / 2.0;

                y2 = Math.Cos(-Angle) * Rectangle.Height / 2.0;
                x2 = Math.Sin(-Angle) * Rectangle.Width / 2.0;
            }
            else
            {
                y1 = Math.Cos(-(Angle + Math.PI / 2) - Math.PI) * Rectangle.Height / 2.0;
                x1 = Math.Sin(-(Angle + Math.PI / 2) - Math.PI) * Rectangle.Width / 2.0;

                y2 = Math.Cos(-(Angle + Math.PI / 2)) * Rectangle.Height / 2.0;
                x2 = Math.Sin(-(Angle + Math.PI / 2)) * Rectangle.Width / 2.0;
            }

            Point start = new Point((float)(Center.X + x1), (float)(Center.Y + y1));
            Point end   = new Point((float)(Center.X + x2), (float)(Center.Y + y2));

            if (start.DistanceTo(end) == 0)
            {
                return(null);
            }

            return(new LineSegment(start, end));
        }
Example #2
0
        // Mouse click on the image - accept new point or reject it (depending on mouse button)
        private void pictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (pointIndexToLocate != -1)
            {
                pictureBox.Cursor  = Cursors.Default;
                pictureBox.Capture = false;
                statusLabel.Text   = string.Empty;

                if (e.Button == MouseButtons.Left)
                {
                    int x = Math.Max(0, Math.Min(e.X, imageSize.Width - 1));
                    int y = Math.Max(0, Math.Min(e.Y, imageSize.Height - 1));

                    imagePoints[pointIndexToLocate] = new Accord.Point(x - imageSize.Width / 2, imageSize.Height / 2 - y);

                    TextBox imagePointTextBox = (TextBox)imagePointsGroupBox.Controls[string.Format("imagePoint{0}Box", pointIndexToLocate + 1)];
                    imagePointTextBox.Text = imagePoints[pointIndexToLocate].ToString();
                }
                else
                {
                    imagePoints[pointIndexToLocate] = pointPreviousValue;
                }

                ClearEstimation();

                pointIndexToLocate = -1;
                pictureBox.Invalidate();
            }
        }
Example #3
0
        private void ChangeTimerGaussParameters(float x, float y)
        {
            _gaussTimer?.Stop();
            _gaussTimer?.Dispose();
            _gaussTimer          = new Timer();
            _gaussTimer.Interval = _gaussRefreshTime;
            _gaussTimer.Tick    += (s, ea) =>
            {
                textBoxPosition.Text = "X: " + x.ToString() + "; " + "Y: " + y.ToString();
                Bitmap bitmap = (Bitmap)_bitmap.Clone();
                Dictionary <string, List <double> > fitting = Analyzer.GaussianFittingXY(bitmap, (int)x, (int)y, (int)_gaussSize);

                //Refresh crop
                Bitmap    cropImage = new Bitmap(bitmap);
                Rectangle rect      = new Rectangle((int)((x - _gaussSize / 2) < 0 ? 0 : (x - _gaussSize / 2)), (int)((y - _gaussSize / 2) < 0 ? 0 : (y - _gaussSize / 2)), (int)_gaussSize, (int)_gaussSize);
                if (rect.X + rect.Width > bitmap.Width || rect.Y + rect.Height > bitmap.Height)
                {
                    rect.Width  = Math.Min(bitmap.Width - rect.X, bitmap.Height - rect.Y);
                    rect.Height = rect.Width;
                    _gaussSize  = rect.Height;
                }
                cropImage = cropImage.Clone(rect, cropImage.PixelFormat);

                BeginInvoke((Action)(() =>
                {
                    pictureBoxSnap.Image = cropImage;

                    _chartXLine.Clear();
                    List <List <double> > input = new List <List <double> >();
                    input.Add(fitting["xLine"]);
                    input.Add(fitting["gXLine"]);
                    _chartXLine.Values = input;
                    _chartXLine.Draw();
                    splitContainer3.Panel1.Controls.Clear();
                    splitContainer3.Panel1.Controls.Add(_chartXLine);

                    _chartYLine.Clear();
                    input = new List <List <double> >();
                    input.Add(fitting["yLine"]);
                    input.Add(fitting["gYLine"]);
                    _chartYLine.Values = input;
                    _chartYLine.Draw();
                    splitContainer3.Panel2.Controls.Clear();
                    splitContainer3.Panel2.Controls.Add(_chartYLine);
                }));

                if (_followLight)
                {
                    Point newPosition = Analyzer.FollowLight((Bitmap)cropImage.Clone(), (int)x, (int)y, (int)_gaussSize);
                    if (newPosition != new Point(x, y))
                    {
                        ChangeTimerGaussParameters(newPosition.X, newPosition.Y);
                    }
                }
            };
            _gaussTimer.Start();
        }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Blob"/> class.
 /// </summary>
 ///
 /// <param name="source">Source blob to copy.</param>
 ///
 /// <remarks><para>This copy constructor leaves <see cref="Image"/> property not initialized. The blob's
 /// image may be extracted later using <see cref="BlobCounterBase.ExtractBlobsImage( Bitmap, Blob, bool )"/>
 /// or <see cref="BlobCounterBase.ExtractBlobsImage( UnmanagedImage, Blob, bool )"/> method.</para></remarks>
 ///
 public Blob(Blob source)
 {
     // copy everything except image
     id          = source.id;
     rect        = source.rect;
     cog         = source.cog;
     area        = source.area;
     fullness    = source.fullness;
     colorMean   = source.colorMean;
     colorStdDev = source.colorStdDev;
 }
Example #5
0
        public void RansacLineConstructorTest()
        {
            Point[] points = new Point[500];

            {
                for (int i = 0; i < points.Length; i++)
                {
                    double x = i;
                    double y = i; //+ 5 * Accord.Math.Tools.Random.NextDouble();
                    points[i] = new Point((float)x, (float)y);
                }

                RansacLine target = new RansacLine(0.80, 0.9);

                Line actual = target.Estimate(points);
                Line expected = Line.FromPoints(points[0], points[499]);

                Assert.AreEqual(expected.Slope, actual.Slope, 1e-3);
                Assert.AreEqual(expected.Intercept, actual.Intercept, 1e-2);
            }

            {
                for (int i = 0; i < points.Length; i++)
                {
                    double x = i;
                    double y = i + 5 * Accord.Math.Tools.Random.NextDouble();
                    points[i] = new Point((float)x, (float)y);
                }

                RansacLine target = new RansacLine(0.80, 0.9);

                Line actual = target.Estimate(points);

                Assert.AreEqual(1.0, actual.Slope, 1e-3);
                Assert.AreEqual(0.0, actual.Intercept, 1e-2);
            }

            {
                for (int i = 0; i < points.Length; i++)
                {
                    double x = i + 50;
                    double y = i + 50 + 5 * Accord.Math.Tools.Random.NextDouble();
                    points[i] = new Point((float)x, (float)y);
                }

                RansacLine target = new RansacLine(0.80, 0.9);

                Line actual = target.Estimate(points);

                Assert.AreEqual(1.0, actual.Slope, 1e-3);
                Assert.AreEqual(0.0, actual.Intercept, 1e-2);
            }
        }
Example #6
0
        public void RansacLineConstructorTest()
        {
            Point[] points = new Point[500];

            {
                for (int i = 0; i < points.Length; i++)
                {
                    double x = i;
                    double y = i; //+ 5 * Accord.Math.Tools.Random.NextDouble();
                    points[i] = new Point((float)x, (float)y);
                }

                RansacLine target = new RansacLine(0.80, 0.9);

                Line actual   = target.Estimate(points);
                Line expected = Line.FromPoints(points[0], points[499]);

                Assert.AreEqual(expected.Slope, actual.Slope, 1e-3);
                Assert.AreEqual(expected.Intercept, actual.Intercept, 1e-2);
            }

            {
                for (int i = 0; i < points.Length; i++)
                {
                    double x = i;
                    double y = i + 5 * Accord.Math.Tools.Random.NextDouble();
                    points[i] = new Point((float)x, (float)y);
                }

                RansacLine target = new RansacLine(0.80, 0.9);

                Line actual = target.Estimate(points);

                Assert.AreEqual(1.0, actual.Slope, 1e-3);
                Assert.AreEqual(0.0, actual.Intercept, 1e-2);
            }

            {
                for (int i = 0; i < points.Length; i++)
                {
                    double x = i + 50;
                    double y = i + 50 + 5 * Accord.Math.Tools.Random.NextDouble();
                    points[i] = new Point((float)x, (float)y);
                }

                RansacLine target = new RansacLine(0.80, 0.9);

                Line actual = target.Estimate(points);

                Assert.AreEqual(1.0, actual.Slope, 1e-3);
                Assert.AreEqual(0.0, actual.Intercept, 1e-2);
            }
        }
Example #7
0
        // One of the locate point button were clicked
        private void locatePointButton_Click(object sender, EventArgs e)
        {
            pictureBox.Capture = true;

            Button sourceButton = (Button)sender;

            pointIndexToLocate = int.Parse((string)sourceButton.Tag);

            pointPreviousValue = imagePoints[pointIndexToLocate];
            imagePoints[pointIndexToLocate] = emptyPoint;

            statusLabel.Text = string.Format("Locate point #{0} in the image ...", pointIndexToLocate + 1);
            pictureBox.Invalidate();
        }
Example #8
0
        private Accord.Point[] PerformProjection(Vector3[] model, Matrix4x4 transformationMatrix, int viewSize)
        {
            Accord.Point[] projectedPoints = new Accord.Point[model.Length];

            for (int i = 0; i < model.Length; i++)
            {
                Vector3 scenePoint = (transformationMatrix * model[i].ToVector4()).ToVector3();

                projectedPoints[i] = new Accord.Point(
                    (int)(scenePoint.X / scenePoint.Z * viewSize),
                    (int)(scenePoint.Y / scenePoint.Z * viewSize));
            }

            return(projectedPoints);
        }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Blob"/> class.
 /// </summary>
 ///
 /// <param name="source">Source blob to copy.</param>
 /// <param name="copyImage">Set to true to copy the blob Image</param>
 ///
 /// <remarks><para>This copy constructor leaves <see cref="Image"/> property not initialized unless
 /// <see param="copyImage"/> is set to true. The blob's image may be extracted later using
 /// <see cref="BlobCounterBase.ExtractBlobsImage( Bitmap, Blob, bool )"/>
 /// or <see cref="BlobCounterBase.ExtractBlobsImage( UnmanagedImage, Blob, bool )"/> method.</para></remarks>
 ///
 public Blob(Blob source, bool copyImage = false)
 {
     // copy everything except image
     id          = source.id;
     rect        = source.rect;
     cog         = source.cog;
     area        = source.area;
     fullness    = source.fullness;
     colorMean   = source.colorMean;
     colorStdDev = source.colorStdDev;
     if (copyImage)
     {
         image = source.image;
     }
 }
Example #10
0
        private void PictureBoxStream_Click(object sender, EventArgs e)
        {
            //Bitmap bitmap = (Bitmap)pictureBoxStream.Image.Clone();
            _followLight = false;
            if (_bitmap == null)
            {
                return;
            }

            Bitmap bitmap = (Bitmap)_bitmap.Clone();
            //Calculate the position on bitmap
            float x0      = 0;
            float y0      = 0;
            float bitmapW = pictureBoxStream.Width;
            float bitmapH = pictureBoxStream.Height;

            if (pictureBoxStream.Width / (float)pictureBoxStream.Height < bitmap.Width / (float)bitmap.Height)
            {
                bitmapW = pictureBoxStream.Width;
                bitmapH = bitmapW * bitmap.Height / bitmap.Width;
                y0      = (float)(pictureBoxStream.Height - bitmapH) / 2;
            }
            else if (pictureBoxStream.Width / (float)pictureBoxStream.Height > bitmap.Width / (float)bitmap.Height)
            {
                bitmapH = pictureBoxStream.Height;
                bitmapW = bitmapH * bitmap.Width / bitmap.Height;
                x0      = (float)(pictureBoxStream.Width - bitmapW) / 2;
            }

            float x = ((MouseEventArgs)e).Location.X - x0;
            float y = ((MouseEventArgs)e).Location.Y - y0;

            //Real coordinate in image
            x *= bitmap.Width / bitmapW;
            y *= bitmap.Height / bitmapH;

            _gaussPosition = new Point((int)x, (int)y);
            ChangeTimerGaussParameters((int)x, (int)y);
        }
Example #11
0
        private void Recalculate()
        {
            int pointsCount = objectPoints.Length;

            objectScenePoints = new Vector3[pointsCount];
            projectedPoints   = new Accord.Point[pointsCount];

            int cx = ClientRectangle.Width / 2;
            int cy = ClientRectangle.Height / 2;

            for (int i = 0; i < pointsCount; i++)
            {
                objectScenePoints[i] = (perspectiveMatrix *
                                        (viewMatrix *
                                         (worldMatrix * objectPoints[i].ToVector4()))).ToVector3();

                projectedPoints[i] = new Accord.Point(
                    (int)(cx * objectScenePoints[i].X),
                    (int)(cy * objectScenePoints[i].Y));
            }

            Invalidate();
        }
Example #12
0
        private Accord.Point[] PerformProjection(Vector3[] model, Matrix4x4 transformationMatrix, int viewSize)
        {
            Accord.Point[] projectedPoints = new Accord.Point[model.Length];

            for (int i = 0; i < model.Length; i++)
            {
                Vector3 scenePoint = (transformationMatrix * model[i].ToVector4()).ToVector3();

                projectedPoints[i] = new Accord.Point(
                    (int)(scenePoint.X / scenePoint.Z * viewSize),
                    (int)(scenePoint.Y / scenePoint.Z * viewSize));
            }

            return projectedPoints;
        }
        private void Recalculate( )
        {
            int pointsCount = objectPoints.Length;
            objectScenePoints = new Vector3[pointsCount];
            projectedPoints = new Accord.Point[pointsCount];

            int cx = ClientRectangle.Width / 2;
            int cy = ClientRectangle.Height / 2;

            for ( int i = 0; i < pointsCount; i++ )
            {
                objectScenePoints[i] = ( perspectiveMatrix *
                                       ( viewMatrix *
                                       ( worldMatrix * objectPoints[i].ToVector4( ) ) ) ).ToVector3( );

                projectedPoints[i] = new Accord.Point(
                    (int) ( cx * objectScenePoints[i].X ),
                    (int) ( cy * objectScenePoints[i].Y ) );
            }

            Invalidate( );
        }
Example #14
0
        // One of the locate point button were clicked
        private void locatePointButton_Click(object sender, EventArgs e)
        {
            pictureBox.Capture = true;

            Button sourceButton = (Button)sender;
            pointIndexToLocate = int.Parse((string)sourceButton.Tag);

            pointPreviousValue = imagePoints[pointIndexToLocate];
            imagePoints[pointIndexToLocate] = emptyPoint;

            statusLabel.Text = string.Format("Locate point #{0} in the image ...", pointIndexToLocate + 1);
            pictureBox.Invalidate();
        }
Example #15
0
        // Mouse click on the image - accept new point or reject it (depending on mouse button)
        private void pictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (pointIndexToLocate != -1)
            {
                pictureBox.Cursor = Cursors.Default;
                pictureBox.Capture = false;
                statusLabel.Text = string.Empty;

                if (e.Button == MouseButtons.Left)
                {
                    int x = Math.Max(0, Math.Min(e.X, imageSize.Width - 1));
                    int y = Math.Max(0, Math.Min(e.Y, imageSize.Height - 1));

                    imagePoints[pointIndexToLocate] = new Accord.Point(x - imageSize.Width / 2, imageSize.Height / 2 - y);

                    TextBox imagePointTextBox = (TextBox)imagePointsGroupBox.Controls[string.Format("imagePoint{0}Box", pointIndexToLocate + 1)];
                    imagePointTextBox.Text = imagePoints[pointIndexToLocate].ToString();
                }
                else
                {
                    imagePoints[pointIndexToLocate] = pointPreviousValue;
                }

                ClearEstimation();

                pointIndexToLocate = -1;
                pictureBox.Invalidate();
            }
        }
Example #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Blob"/> class.
 /// </summary>
 /// 
 /// <param name="source">Source blob to copy.</param>
 /// 
 /// <remarks><para>This copy constructor leaves <see cref="Image"/> property not initialized. The blob's
 /// image may be extracted later using <see cref="BlobCounterBase.ExtractBlobsImage( Bitmap, Blob, bool )"/>
 /// or <see cref="BlobCounterBase.ExtractBlobsImage( UnmanagedImage, Blob, bool )"/> method.</para></remarks>
 /// 
 public Blob(Blob source)
 {
     // copy everything except image
     id = source.id;
     rect = source.rect;
     cog = source.cog;
     area = source.area;
     fullness = source.fullness;
     colorMean = source.colorMean;
     colorStdDev = source.colorStdDev;
 }
Example #17
0
        /// <summary>
        ///   Gets two points defining the axis of the object.
        /// </summary>
        /// 
        public LineSegment GetAxis(AxisOrientation axis)
        {
            double x1, y1;
            double x2, y2;

            if (axis == AxisOrientation.Horizontal)
            {
                y1 = Math.Cos(-Angle - Math.PI) * Rectangle.Height / 2.0;
                x1 = Math.Sin(-Angle - Math.PI) * Rectangle.Width / 2.0;

                y2 = Math.Cos(-Angle) * Rectangle.Height / 2.0;
                x2 = Math.Sin(-Angle) * Rectangle.Width / 2.0;
            }
            else
            {
                y1 = Math.Cos(-(Angle + Math.PI / 2) - Math.PI) * Rectangle.Height / 2.0;
                x1 = Math.Sin(-(Angle + Math.PI / 2) - Math.PI) * Rectangle.Width / 2.0;

                y2 = Math.Cos(-(Angle + Math.PI / 2)) * Rectangle.Height / 2.0;
                x2 = Math.Sin(-(Angle + Math.PI / 2)) * Rectangle.Width / 2.0;
            }

            Point start = new Point((float)(Center.X + x1), (float)(Center.Y + y1));
            Point end = new Point((float)(Center.X + x2), (float)(Center.Y + y2));

            if (start.DistanceTo(end) == 0)
                return null;

            return new LineSegment(start, end);
        }
Example #18
0
        public void Load(string fileName)
        {
            FileStream   strm   = new FileStream(fileName, FileMode.Open);
            StreamReader reader = new StreamReader(strm);

            // read header
            string rawLine = reader.ReadLine();
            int    counter = 0;

            while ((rawLine = reader.ReadLine()) != null)
            {
                string[] elements = rawLine.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

                double parameter1 = Convert.ToDouble(elements[1]);
                double parameter2 = Convert.ToDouble(elements[2]);

                Line2D line = new Line2D(parameter1, parameter2, counter < m_rows);
                if (counter < m_rows)
                {
                    m_rowLines.Add(line);
                }
                else
                {
                    m_columnLines.Add(line);
                }

                counter++;
            }

            // now do the intersections
            for (int i = 0; i < m_rows; i++)
            {
                Line2D row = m_rowLines[i];
                for (int j = 0; j < m_cols; j++)
                {
                    Line2D column       = m_columnLines[j];
                    Point? intersection = row.IntersectWith(column);

                    if (intersection != null)
                    {
                        m_gridPoints[i, j] = intersection.Value;
                    }
                }
            }

            // now fill the polygons
            RansacHomographyEstimator ransac = new RansacHomographyEstimator(0.001, 0.99);

            for (int i = 0; i < m_rows - 1; i++)
            {
                for (int j = 0; j < m_cols - 1; j++)
                {
                    m_polygons[i, j] = new Polygon2D();
                    m_polygons[i, j].Points.Add(m_gridPoints[i, j]);
                    m_polygons[i, j].Points.Add(m_gridPoints[i, j + 1]);
                    m_polygons[i, j].Points.Add(m_gridPoints[i + 1, j + 1]);
                    m_polygons[i, j].Points.Add(m_gridPoints[i + 1, j]);

                    if (Constants.DebugPrint)
                    {
                        Debug.WriteLine("\tPolygon:");
                        Debug.WriteLine("\t\t" + m_polygons[i, j].Points[0]);
                        Debug.WriteLine("\t\t" + m_polygons[i, j].Points[1]);
                        Debug.WriteLine("\t\t" + m_polygons[i, j].Points[2]);
                        Debug.WriteLine("\t\t" + m_polygons[i, j].Points[3]);
                    }

                    Accord.Point[] templatePts = new Accord.Point[4];
                    templatePts[0] = new Accord.Point(j / (float)(m_cols - 1), i / (float)(m_rows - 1));
                    templatePts[1] = new Accord.Point((j + 1) / (float)(m_cols - 1), i / (float)(m_rows - 1));
                    templatePts[2] = new Accord.Point((j + 1) / (float)(m_cols - 1), (i + 1) / (float)(m_rows - 1));
                    templatePts[3] = new Accord.Point(j / (float)(m_cols - 1), (i + 1) / (float)(m_rows - 1));

                    Accord.Point[] realPoints = new Accord.Point[4];
                    for (int k = 0; k < 4; k++)
                    {
                        realPoints[k] = new Accord.Point((float)m_polygons[i, j].Points[k].X, (float)m_polygons[i, j].Points[k].Y);
                    }

                    Accord.Point[][] matchedPoints = new Accord.Point[2][];
                    matchedPoints[1] = templatePts;
                    matchedPoints[0] = realPoints;

                    MatrixH homography = ransac.Estimate(matchedPoints);
                    m_homographies.Add(m_polygons[i, j], homography);
                }
            }

            reader.Close();
            reader.Dispose();
            reader = null;

            strm.Close();
            strm.Dispose();
            strm = null;
        }