3D pose estimation algorithm.

The class implements an algorithm for 3D object's pose estimation from it's 2D coordinates obtained by perspective projection, when the object is described none coplanar points. The idea of the implemented math and algorithm is described in "Model-Based Object Pose in 25 Lines of Code" paper written by Daniel F. DeMenthon and Larry S. Davis (the implementation of the algorithm is almost 1 to 1 translation of the pseudo code given by the paper, so should be easy to follow).

At this point the implementation works only with models described by 4 points, which is the minimum number of points enough for 3D pose estimation.

The 4 model's point must not be coplanar, i.e. must not reside all within same planer. See CoplanarPosit for coplanar case.

Read 3D Pose Estimation article for additional information and samples.

Sample usage:

// points of real object - model Vector3[] positObject = new Vector3[4] { new Vector3( 28, 28, -28 ), new Vector3( -28, 28, -28 ), new Vector3( 28, -28, -28 ), new Vector3( 28, 28, 28 ), }; // focal length of camera used to capture the object float focalLength = 640; // depends on your camera or projection system // initialize POSIT object Posit posit = new Posit( positObject, focalLength ); // 2D points of te object - projection AForge.Point[] projectedPoints = new AForge.Point[4] { new AForge.Point( -4, 29 ), new AForge.Point( -180, 86 ), new AForge.Point( -5, -102 ), new AForge.Point( 76, 137 ), }; // estimate pose Matrix3x3 rotationMatrix; Vector3 translationVector; posit.EstimatePose( projectedPoints, out rotationMatrix, out translationVector );
Example #1
0
        public MainForm( )
        {
            InitializeComponent( );

            posit = new Posit( positObject, -200 );
            coposit = new CoplanarPosit( copositObject, 200 );
        }
Example #2
0
        // Estimate 3D position
        private void EstimatePose( )
        {
            try
            {
                // check if all image coordinates are specified
                if ( ( string.IsNullOrEmpty( imagePoint1Box.Text ) ) ||
                     ( string.IsNullOrEmpty( imagePoint2Box.Text ) ) ||
                     ( string.IsNullOrEmpty( imagePoint3Box.Text ) ) ||
                     ( string.IsNullOrEmpty( imagePoint4Box.Text ) ) )
                {
                    throw new ApplicationException( "Some image coordinates are not specified." );
                }

                // check if all model coordnates are specified
                if ( ( string.IsNullOrEmpty( modelPoint1xBox.Text ) ) ||
                     ( string.IsNullOrEmpty( modelPoint2xBox.Text ) ) ||
                     ( string.IsNullOrEmpty( modelPoint3xBox.Text ) ) ||
                     ( string.IsNullOrEmpty( modelPoint4xBox.Text ) ) ||
                     ( string.IsNullOrEmpty( modelPoint1yBox.Text ) ) ||
                     ( string.IsNullOrEmpty( modelPoint2yBox.Text ) ) ||
                     ( string.IsNullOrEmpty( modelPoint3yBox.Text ) ) ||
                     ( string.IsNullOrEmpty( modelPoint4yBox.Text ) ) ||
                     ( ( !useCoplanarPosit ) && (
                       ( string.IsNullOrEmpty( modelPoint1zBox.Text ) ) ||
                       ( string.IsNullOrEmpty( modelPoint2zBox.Text ) ) ||
                       ( string.IsNullOrEmpty( modelPoint3zBox.Text ) ) ||
                       ( string.IsNullOrEmpty( modelPoint4zBox.Text ) ) ) ) )
                {
                    throw new ApplicationException( "Some model coordinates are not specified." );
                }

                // calculate model's center
                Vector3 modelCenter = new Vector3(
                    ( modelPoints[0].X + modelPoints[1].X + modelPoints[2].X + modelPoints[3].X ) / 4,
                    ( modelPoints[0].Y + modelPoints[1].Y + modelPoints[2].Y + modelPoints[3].Y ) / 4,
                    ( modelPoints[0].Z + modelPoints[1].Z + modelPoints[2].Z + modelPoints[3].Z ) / 4
                );

                // calculate ~ model's radius
                modelRadius = 0;
                foreach ( Vector3 modelPoint in modelPoints )
                {
                    float distanceToCenter = ( modelPoint - modelCenter ).Norm;
                    if ( distanceToCenter > modelRadius )
                    {
                        modelRadius = distanceToCenter;
                    }
                }

                if ( !useCoplanarPosit )
                {
                    Posit posit = new Posit( modelPoints, focalLength );
                    posit.EstimatePose( imagePoints, out rotationMatrix, out translationVector );

                    bestPoseButton.Visible = alternatePoseButton.Visible = false;
                }
                else
                {
                    CoplanarPosit coposit = new CoplanarPosit( modelPoints, focalLength );
                    coposit.EstimatePose( imagePoints, out rotationMatrix, out translationVector );

                    bestRotationMatrix = coposit.BestEstimatedRotation;
                    bestTranslationVector = coposit.BestEstimatedTranslation;

                    alternateRotationMatrix = coposit.AlternateEstimatedRotation;
                    alternateTranslationVector = coposit.AlternateEstimatedTranslation;

                    bestPoseButton.Visible = alternatePoseButton.Visible = true;
                }

                isPoseEstimated = true;
                UpdateEstimationInformation( );
                pictureBox.Invalidate( );
            }
            catch ( ApplicationException ex )
            {
                MessageBox.Show( ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
            }
        }
        /// <summary>
        /// Estemate the 3D position of te glyph.
        /// </summary>
        /// <param name="imageSize">Image size is more like size of the coordinate system.</param>
        /// <param name="useCoplanarPosit"></param>
        /// <param name="yaw"></param>
        /// <param name="pitch"></param>
        /// <param name="roll"></param>
        public void EstimateOrientation(bool useCoplanarPosit, out float yaw, out float pitch, out float roll)
        {
            AForge.Point[] tmpQuadrilateral = new AForge.Point[this.Quadrilateral.Count];

            for (int index = 0; index < this.Quadrilateral.Count; index++)
            {
                tmpQuadrilateral[index] = new AForge.Point(this.Quadrilateral[index].X, this.Quadrilateral[index].Y);
            }

            //
            this.focalLength = this.CoordinateSystemSize.Width;

            // Scale the coordinates
            for (int index = 0; index < tmpQuadrilateral.Length; index++)
            {
                float x = System.Math.Max(0, System.Math.Min(tmpQuadrilateral[index].X, this.CoordinateSystemSize.Width - 1));
                float y = System.Math.Max(0, System.Math.Min(tmpQuadrilateral[index].Y, this.CoordinateSystemSize.Height - 1));

                tmpQuadrilateral[index] = new AForge.Point(x - this.CoordinateSystemSize.Width / 2, this.CoordinateSystemSize.Height / 2 - y);
            }

            // Calculate model's center
            Vector3 modelCenter = new Vector3(
                (modelPoints[0].X + modelPoints[1].X + modelPoints[2].X + modelPoints[3].X) / 4,
                (modelPoints[0].Y + modelPoints[1].Y + modelPoints[2].Y + modelPoints[3].Y) / 4,
                (modelPoints[0].Z + modelPoints[1].Z + modelPoints[2].Z + modelPoints[3].Z) / 4
            );

            // Calculate ~ model's radius.
            this.ModelRadius = 0;
            foreach (Vector3 modelPoint in modelPoints)
            {
                float distanceToCenter = (modelPoint - modelCenter).Norm;
                if (distanceToCenter > this.ModelRadius)
                {
                    this.ModelRadius = distanceToCenter;
                }
            }

            if (!useCoplanarPosit)
            {
                Posit posit = new Posit(modelPoints, focalLength);
                posit.EstimatePose(tmpQuadrilateral, out rotationMatrix, out translationVector);
            }
            else
            {
                CoplanarPosit coposit = new CoplanarPosit(modelPoints, this.focalLength);
                coposit.EstimatePose(tmpQuadrilateral, out rotationMatrix, out translationVector);

                this.bestRotationMatrix = coposit.BestEstimatedRotation;
                this.bestTranslationVector = coposit.BestEstimatedTranslation;

                this.alternateRotationMatrix = coposit.AlternateEstimatedRotation;
                this.alternateTranslationVector = coposit.AlternateEstimatedTranslation;
            }

            // Get the rotation.
            this.rotationMatrix.ExtractYawPitchRoll(out yaw, out pitch, out roll);

            // Transform to degree.
            yaw *= (float)(180.0 / System.Math.PI);
            pitch *= (float)(180.0 / System.Math.PI);
            roll *= (float)(180.0 / System.Math.PI);
        }