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