opencv_aruco

2023-05-16

文章参考:ArUco_木筏筏筏的博客-CSDN博客_aruco

1.01_显示识别mark.cpp

#include <opencv2\highgui.hpp>
#include <opencv2\aruco.hpp>
#include <opencv2\aruco\dictionary.hpp>
#include <opencv2\core.hpp>
#include <vector>
#include <iostream>

using namespace std;
using namespace cv;

int main()
{
	cv::Mat markerImage;
	cv::VideoCapture mVideoCapture(0);//0表示使用电脑自带摄像头,>1表示使用外置摄像头

	//创建字典,这里注意使用Ptr<>,不然无法显示结果
	Ptr<aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);//4*4位的、50种---字典
	cv::aruco::drawMarker(dictionary, 23, 240, markerImage);//生成id 23,大小240*240像素的Mark

	imshow("marker", markerImage);//显示待检测标记
	//imwrite("marker.jpg", markerImage);

	while (mVideoCapture.grab()) {//取得下一帧
		cv::Mat frame, frame_show;
		mVideoCapture.retrieve(frame);//放入Mat

		frame.copyTo(frame_show);//复制一份

		std::vector<int> ids;
		std::vector<std::vector<cv::Point2f>> corners;

		cv::aruco::detectMarkers(frame, dictionary, corners, ids);//检测该帧是否有标记

		if (ids.size() > 0)
			cv::aruco::drawDetectedMarkers(frame_show, corners, ids);//如果有,则标记出来,放入另一个Mat

		cv::imshow("video", frame_show);//显示结果

		char key = (char)waitKey(30);
		if (key == 'b') break;
	}

	return 0;
}

2.02_aruco_位置估计.cpp

#include <opencv2\highgui.hpp>
#include <opencv2\aruco.hpp>
#include <opencv2\aruco\dictionary.hpp>
#include <opencv2\core.hpp>
#include <vector>
#include <iostream>
#include <opencv2\imgproc.hpp>

using namespace std;
using namespace cv;

int main()
{
	cv::Mat markerImage;
	cv::VideoCapture mVideoCapture(0);//0表示使用电脑自带摄像头,>1表示使用外置摄像头

	//创建字典,这里注意使用Ptr<>,不然无法显示结果
	Ptr<aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);//字典(4*4位的、内含50种)
	cv::aruco::drawMarker(dictionary, 23, 240, markerImage);//生成id 23,大小240*240像素的Mark,放在markerImage矩阵中

	imshow("my_marker", markerImage);//显示待检测标记
	//imwrite("marker.jpg", markerImage);
	Mat frame;
	while (mVideoCapture.read(frame))
	{
		Mat test_image;
		cv::resize(frame, test_image, Size(800, 800));
		imshow("视频流", test_image);
		//要识别的字典库是
		auto dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME::DICT_4X4_50);
		vector<vector<Point2f>>corners, rejectedImgPoints;
		vector<int>ids;
		auto parameters = aruco::DetectorParameters::create();
		//识别检测Mark
		aruco::detectMarkers(test_image, dictionary, corners, ids, parameters, rejectedImgPoints);
		aruco::drawDetectedMarkers(test_image, corners, ids, Scalar(0, 255, 0));
		//自定义相机内参
		cv::Mat cameraMatrix, distCoeffs;
		vector<double> camera = { 657.1548323619423, 0, 291.8582472145741,0, 647.384819351103, 391.254810476919,0, 0, 1 };
		cameraMatrix = Mat(camera);
		cameraMatrix = cameraMatrix.reshape(1, 3);
		vector<double> dist = { 0.1961793476399528, -1.38146317350581, -0.002301820186177369, -0.001054637905895881, 2.458286937422959 };
		distCoeffs = Mat(dist);
		distCoeffs = distCoeffs.reshape(1, 1);
		std::vector<cv::Vec3d> rvecs;
		std::vector<cv::Vec3d> tvecs;				//实际的Mark边长0.053m
		//3纬到2纬位置估计
		cv::aruco::estimatePoseSingleMarkers(corners, 0.053, cameraMatrix, distCoeffs, rvecs, tvecs);
		for (int i = 0; i < rvecs.size(); i++)
		{
			//绘制坐标轴,检查姿态估计结果											//长度0.02m
			cv::aruco::drawAxis(test_image, cameraMatrix, distCoeffs, rvecs[i], tvecs[i], 0.02);
		}
		//namedWindow("pose", WINDOW_FREERATIO);
		imshow("画坐标轴视频流", test_image);

		char ch = cv::waitKey(1);
		if (27 == ch)
		{
			break;
		}
	}
	return 0;
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

opencv_aruco 的相关文章

随机推荐