public MainForm() { InitializeComponent(); posit = new Posit(positObject, -200); coposit = new CoplanarPosit(copositObject, 200); }
private void CreatePosit( ) { if (glyphSize != 0) { // create glyph's model float sizeHalf = glyphSize / 2; Vector3[] glyphModel = new Vector3[] { new Vector3(-sizeHalf, 0, sizeHalf), new Vector3(sizeHalf, 0, sizeHalf), new Vector3(sizeHalf, 0, -sizeHalf), new Vector3(-sizeHalf, 0, -sizeHalf), }; posit = new CoplanarPosit(glyphModel, cameraFocalLength); } else { posit = null; } }
// 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); } }
void camera_NewFrame(object sender, NewFrameEventArgs eventArgs) { float sizeHalf = 113.0f / 2; Vector3[] glyphModel = new Vector3[] { new Vector3(-sizeHalf, 0, sizeHalf), new Vector3(sizeHalf, 0, sizeHalf), new Vector3(sizeHalf, 0, -sizeHalf), new Vector3(-sizeHalf, 0, -sizeHalf), }; try { GlyphRecognizer gr = new GlyphRecognizer(glyphDatabase); List <ExtractedGlyphData> glyphs = gr.FindGlyphs(eventArgs.Frame); System.Drawing.Image img = (Bitmap)eventArgs.Frame.Clone(); double x = 0; double y = 0; String glyphName = String.Empty; AForge.Point[] glyphPoints = new AForge.Point[4]; foreach (var glyph in glyphs) { if (glyph.RecognizedGlyph != null) { glyphName = glyph.RecognizedGlyph.Name; x = (glyph.Quadrilateral[0].X + glyph.Quadrilateral[2].X) / 2; y = (glyph.Quadrilateral[0].Y + glyph.Quadrilateral[2].Y) / 2; for (int i = 0; i < 4; i++) { glyphPoints[i] = new AForge.Point( (int)(glyph.RecognizedQuadrilateral[i].X - x), (int)y - glyph.RecognizedQuadrilateral[i].Y); } break; } } MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); bi.Freeze(); Dispatcher.BeginInvoke(new ThreadStart(delegate { //Canvas.SetLeft(pictureGlyph, x-60); //Canvas.SetTop(pictureGlyph, y-60); image1.Source = bi; Title = "Found glyphs " + glyphs.Count + " " + glyphName; if (glyphName != "") { Dispatcher.BeginInvoke(new ThreadStart(delegate { Console.WriteLine(MODEL_PATH); MODEL_PATH = CurrentGallery.UrlPrefix + "\\" + glyphName + ".3ds"; float yaw, pitch, roll; CoplanarPosit posit = new CoplanarPosit(glyphModel, 1); Matrix3x3 positRotation; Vector3 positTranslation; posit.EstimatePose(glyphPoints, out positRotation, out positTranslation); positRotation.ExtractYawPitchRoll(out yaw, out pitch, out roll); visualisation3Dmodel(MODEL_PATH, yaw, pitch, roll, new Vector3D(positTranslation.X, positTranslation.Y, positTranslation.Z)); })); } })); } catch (Exception ex) { MessageBox.Show(ex.Message); } }