aruco_ar_demo.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <opencv2/highgui.hpp>
  2. #include <opencv2/calib3d.hpp>
  3. #include <opencv2/videoio.hpp>
  4. #include <opencv2/ovis.hpp>
  5. #include <opencv2/aruco.hpp>
  6. #include <iostream>
  7. #define KEY_ESCAPE 27
  8. using namespace cv;
  9. int main()
  10. {
  11. Mat img;
  12. std::vector<std::vector<Point2f>> corners;
  13. std::vector<int> ids;
  14. std::vector<Vec3d> rvecs;
  15. std::vector<Vec3d> tvecs;
  16. const Size2i imsize(800, 600);
  17. const double focal_length = 800.0;
  18. // aruco
  19. Ptr<aruco::Dictionary> adict = aruco::getPredefinedDictionary(aruco::DICT_4X4_50);
  20. //Mat out_img;
  21. //aruco::drawMarker(adict, 0, 400, out_img);
  22. //imshow("marker", out_img);
  23. // random calibration data, your mileage may vary
  24. Mat1d cm = Mat1d::zeros(3, 3); // init empty matrix
  25. cm.at<double>(0, 0) = focal_length; // f_x
  26. cm.at<double>(1, 1) = focal_length; // f_y
  27. cm.at<double>(2, 2) = 1; // f_z
  28. Mat K = getDefaultNewCameraMatrix(cm, imsize, true);
  29. // AR scene
  30. ovis::addResourceLocation("packs/Sinbad.zip"); // shipped with Ogre
  31. Ptr<ovis::WindowScene> win = ovis::createWindow(String("arucoAR"), imsize, ovis::SCENE_INTERACTIVE | ovis::SCENE_AA);
  32. win->setCameraIntrinsics(K, imsize);
  33. win->createEntity("sinbad", "Sinbad.mesh", Vec3i(0, 0, 5), Vec3f(1.57, 0.0, 0.0));
  34. win->createLightEntity("sun", Vec3i(0, 0, 100));
  35. // video capture
  36. VideoCapture cap{0};
  37. cap.set(CAP_PROP_FRAME_WIDTH, imsize.width);
  38. cap.set(CAP_PROP_FRAME_HEIGHT, imsize.height);
  39. std::cout << "Press ESCAPE to exit demo" << std::endl;
  40. while (ovis::waitKey(1) != KEY_ESCAPE) {
  41. cap.read(img);
  42. win->setBackground(img);
  43. aruco::detectMarkers(img, adict, corners, ids);
  44. waitKey(1);
  45. if (ids.size() == 0)
  46. continue;
  47. aruco::estimatePoseSingleMarkers(corners, 5, K, noArray(), rvecs, tvecs);
  48. win->setCameraPose(tvecs.at(0), rvecs.at(0), true);
  49. }
  50. return 0;
  51. }