想了解背后算法可以参考文章,或者阅读源码,我在这里只是调用下函数接口,对算法不了解。
加个关键字:KinectV2和AR结合,OpenCV AR SDK
以下是代码,注意你得有aruco的库,木有的话要去下载才行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
// ArucoVideoCapture.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "opencv-include/opencv2/aruco.hpp" #include "opencv-include/opencv2/opencv.hpp" #include <iostream> using namespace std; using namespace cv; int main(int argc, char *argv[]) { VideoCapture inputVideo; int waitTime; inputVideo.open(0); inputVideo.set(CAP_PROP_FRAME_WIDTH, 1280); inputVideo.set(CAP_PROP_FRAME_HEIGHT, 720); waitTime = 10; double totalTime = 0; int totalIterations = 0; Ptr<aruco::Dictionary> dictionary = aruco::getPredefinedDictionary(aruco::DICT_ARUCO_ORIGINAL); Mat out; dictionary->drawMarker(100, 600, out, 5); Ptr<aruco::DetectorParameters> detectorParams = aruco::DetectorParameters::create(); detectorParams->minDistanceToBorder = 0; while (inputVideo.grab()) { Mat image, imageCopy; inputVideo.retrieve(image); double tick = (double)getTickCount(); vector< int > ids; vector< vector< Point2f > > corners, rejected; vector< Vec3d > rvecs, tvecs; // detect markers and estimate pose aruco::detectMarkers(image, dictionary, corners, ids, detectorParams, rejected); double currentTime = ((double)getTickCount() - tick) / getTickFrequency(); totalTime += currentTime; totalIterations++; // draw results image.copyTo(imageCopy); if (ids.size() > 0) { aruco::drawDetectedMarkers(imageCopy, corners, ids); if (totalIterations % 30 == 0) { cout << "Detection Time = " << currentTime * 1000 << " ms " << "(Mean = " << 1000 * totalTime / double(totalIterations) << " ms)" << endl; } } imshow("out", imageCopy); char key = (char)waitKey(waitTime); if (key == 27) break; } return 0; } |