使用VC2017结合OPENCV4.30实现机器人坐标系与传感器坐标系的手眼标定。可以计算出机器人的工具坐标系和传感器坐标系的相对位置关系矩阵。自己亲自测试,好用。
// HandEyeCalibrationTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <opencv2/opencv.hpp> #include <iostream> #include <math.h> //#pragma comment( lib, "opencv_world430.lib" ) using namespace std; using namespace cv; Mat R_T2HomogeneousMatrix(const Mat& R, const Mat& T); void HomogeneousMtr2RT(Mat& HomoMtr, Mat& R, Mat& T); bool isRotatedMatrix(Mat& R); Mat eulerAngleToRotateMatrix(const Mat& eulerAngle, const std::string& seq); Mat quaternionToRotatedMatrix(const Vec4d& q); Mat attitudeVectorToMatrix(const Mat& m, bool useQuaternion, const string& seq); //数据使用的已有的值 //相机中13组标定板的位姿,x,y,z,rx,ry,rz, Mat_<double> CalPose = (cv::Mat_<double>(9, 6) << -2.602917e+01, -2.058909e+01, 2.001158e+02, -2.222308e+00, -2.200891e+00, 1.038314e-02, -2.109788e+01, -1.966766e+01, 1.820841e+02, 2.145852e+00, 2.143222e+00, 3.322979e-01, -2.571942e+01, -1.872411e+01, 2.013287e+02, -2.108920e+00, -2.134951e+00, 1.931600e-01, -2.488133e+01, -1.828509e+01, 1.956522e+02, -2.047373e+00, -2.027428e+00, 4.624128e-02, -2.920918e+01, -2.170171e+01, 2.143397e+02, 2.174664e+00, 2.123216e+00, -1.765171e-01, -2.518449e+01, -1.630078e+01, 2.110271e+02, 2.229811e+00, 2.162015e+00, -2.832562e-01, -2.729219e+01, -1.855265e+01, 1.884390e+02, -2.130600e+00, -2.041023e+00, 2.669174e-02, -2.754954e+01, -1.465493e+01, 1.886429e+02, -2.204029e+00, -2.089585e+00, -1.583544e-01, -2.716471e+01, -1.718814e+01, 1.924382e+02, -2.287743e+00, -2.088326e+00, -2.886300e-01); //机械臂末端13组位姿,x,y,z,rx,ry,rz Mat_<double> ToolPose = (cv::Mat_<double>(9, 6) << 1437.754, 385.919, 40.011, -8.792, 18.557, -17.376, 1437.714, 387.617, 34.178, -23.661, 16.950, -21.540, 1440.563, 382.431, 36.997, 0.829, 22.108, -12.817, 1440.555, 385.317, 45.598, -1.599, 30.965, -14.361, 1433.627, 385.860, 37.482, -4.907, 10.576, -16.978, 1439.363, 390.483, 34.553, 0.388, 13.051, -15.847, 1438.171, 387.592, 36.215, -4.281, 27.707, -17.485, 1442.171, 388.187, 41.308, -13.644, 25.154, -21.693, 1439.222, 392.200, 48.119, -19.865, 22.540, -25.981); int main(int argc, char** argv) { //数据声明 vector<Mat> R_gripper2base; vector<Mat> T_gripper2base; vector<Mat> R_target2cam; vector<Mat> T_target2cam; Mat R_cam2gripper = Mat(3, 3, CV_64FC1); //相机与机械臂末端坐标系的旋转矩阵与平移矩阵 Mat T_cam2gripper = Mat(3, 1, CV_64FC1); Mat Homo_cam2gripper = Mat(4, 4, CV_64FC1); vector<Mat> Homo_target2cam; vector<Mat> Homo_gripper2base; Mat tempR, tempT, temp; for (int i = 0; i < CalPose.rows; i++) //计算标定板与相机间的齐次矩阵(旋转矩阵与平移向量) { temp = attitudeVectorToMatrix(CalPose.row(i), false, ""); //注意seq为空,相机与标定板间的为旋转向量 Homo_target2cam.push_back(temp); HomogeneousMtr2RT(temp, tempR, tempT); /*cout << i << "::" << temp << endl; cout << i << "::" << tempR << endl; cout << i << "::" << tempT << endl;*/ R_target2cam.push_back(tempR); T_target2cam.push_back(tempT); } for (int j = 0; j < ToolPose.rows; j++) //计算机械臂末端坐标系与机器人基坐标系之间的齐次矩阵 { temp = attitudeVectorToMatrix(ToolPose.row(j), false, "xyz"); //注意seq不是空,机械臂末端坐标系与机器人基坐标系之间的为欧拉角 Homo_gripper2base.push_back(temp); HomogeneousMtr2RT(temp, tempR, tempT); /*cout << j << "::" << temp << endl; cout << j << "::" << tempR << endl; cout << j << "::" << tempT << endl;*/ R_gripper2base.push_back(tempR); T_gripper2base.push_back(tempT); } //TSAI计算速度最快 calibrateHandEye(R_gripper2base, T_gripper2base, R_target2cam, T_target2cam, R_cam2gripper, T_cam2gripper, CALIB_HAND_EYE_TSAI); Homo_cam2gripper = R_T2HomogeneousMatrix(R_cam2gripper, T_cam2gripper); cout << Homo_cam2gripper << endl; cout << "Homo_cam2gripper 是否包含旋转矩阵:" << isRotatedMatrix(Homo_cam2gripper) << endl; FileStorage fs(".\\Homo_cam2gripper.xml", FileStorage::WRITE); fs << "Homo_cam2gripper" << Homo_cam2gripper; fs.release(); /////////////////////////////////////////////////////////////////////////////////////////////////////////// /************************************************** * @note 手眼系统精度测试,原理是标定板在机器人基坐标系中位姿固定不变, * 可以根据这一点进行演算 **************************************************/ //使用1,2组数据验证 标定板在机器人基坐标系中位姿固定不变 cout << "1 : " << Homo_gripper2base[0] * Homo_cam2gripper * Homo_target2cam[0] << endl; cout << "2 : " << Homo_gripper2base[1] * Homo_cam2gripper * Homo_target2cam[1] << endl; //标定板在相机中的位姿 cout << "3 : " << Homo_target2cam[1] << endl; cout << "4 : " << Homo_cam2gripper.inv() * Homo_gripper2base[1].inv() * Homo_gripper2base[0] * Homo_cam2gripper * Homo_target2cam[0] << endl; cout << "----手眼系统测试-----" << endl; cout << "机械臂下标定板XYZ为:" << endl; for (size_t i = 0; i < Homo_target2cam.size(); i++) { Mat chessPos{ 0.0,0.0,0.0,1.0 }; //4*1矩阵,单独求机械臂坐标系下,标定板XYZ Mat worldPos = Homo_gripper2base[i] * Homo_cam2gripper * Homo_target2cam[i] * chessPos; cout << i << ": " << worldPos.t() << endl; } waitKey(0); return 0; } /************************************************** * @brief 将旋转矩阵与平移向量合成为齐次矩阵 * @note * @param Mat& R 3*3旋转矩阵 * @param Mat& T 3*1平移矩阵 * @return Mat 4*4齐次矩阵 **************************************************/ Mat R_T2HomogeneousMatrix(const Mat& R, const Mat& T) { Mat HomoMtr; Mat_<double> R1 = (Mat_<double>(4, 3) << R.at<double>(0, 0), R.at<double>(0, 1), R.at<double>(0, 2), R.at<double>(1, 0), R.at<double>(1, 1), R.at<double>(1, 2), R.at<double>(2, 0), R.at<double>(2, 1), R.at<double>(2, 2), 0, 0, 0); Mat_<double> T1 = (Mat_<double>(4, 1) << T.at<double>(0, 0), T.at<double>(1, 0), T.at<double>(2, 0), 1); cv::hconcat(R1, T1, HomoMtr); //矩阵拼接 return HomoMtr; } /************************************************** * @brief 齐次矩阵分解为旋转矩阵与平移矩阵 * @note * @param const Mat& HomoMtr 4*4齐次矩阵 * @param Mat& R 输出旋转矩阵 * @param Mat& T 输出平移矩阵 * @return **************************************************/ void HomogeneousMtr2RT(Mat& HomoMtr, Mat& R, Mat& T) { //Mat R_HomoMtr = HomoMtr(Rect(0, 0, 3, 3)); //注意Rect取值 //Mat T_HomoMtr = HomoMtr(Rect(3, 0, 1, 3)); //R_HomoMtr.copyTo(R); //T_HomoMtr.copyTo(T); /*HomoMtr(Rect(0, 0, 3, 3)).copyTo(R); HomoMtr(Rect(3, 0, 1, 3)).copyTo(T);*/ Rect R_rect(0, 0, 3, 3); Rect T_rect(3, 0, 1, 3); R = HomoMtr(R_rect); T = HomoMtr(T_rect); } /************************************************** * @brief 检查是否是旋转矩阵 * @note * @param * @param * @param * @return true : 是旋转矩阵, false : 不是旋转矩阵 **************************************************/ bool isRotatedMatrix(Mat& R) //旋转矩阵的转置矩阵是它的逆矩阵,逆矩阵 * 矩阵 = 单位矩阵 { Mat temp33 = R({ 0,0,3,3 }); //无论输入是几阶矩阵,均提取它的三阶矩阵 Mat Rt; transpose(temp33, Rt); //转置矩阵 Mat shouldBeIdentity = Rt * temp33;//是旋转矩阵则乘积为单位矩阵 Mat I = Mat::eye(3, 3, shouldBeIdentity.type()); return cv::norm(I, shouldBeIdentity) < 1e-6; } /************************************************** * @brief 欧拉角转换为旋转矩阵 * @note * @param const std::string& seq 指定欧拉角的排列顺序;(机械臂的位姿类型有xyz,zyx,zyz几种,需要区分) * @param const Mat& eulerAngle 欧拉角(1*3矩阵), 角度值 * @param * @return 返回3*3旋转矩阵 **************************************************/ Mat eulerAngleToRotateMatrix(const Mat& eulerAngle, const std::string& seq) { CV_Assert(eulerAngle.rows == 1 && eulerAngle.cols == 3);//检查参数是否正确 eulerAngle /= (180 / CV_PI); //度转弧度 Matx13d m(eulerAngle); //<double, 1, 3> auto rx = m(0, 0), ry = m(0, 1), rz = m(0, 2); auto rxs = sin(rx), rxc = cos(rx); auto rys = sin(ry), ryc = cos(ry); auto rzs = sin(rz), rzc = cos(rz); //XYZ方向的旋转矩阵 Mat RotX = (Mat_<double>(3, 3) << 1, 0, 0, 0, rxc, -rxs, 0, rxs, rxc); Mat RotY = (Mat_<double>(3, 3) << ryc, 0, rys, 0, 1, 0, -rys, 0, ryc); Mat RotZ = (Mat_<double>(3, 3) << rzc, -rzs, 0, rzs, rzc, 0, 0, 0, 1); //按顺序合成后的旋转矩阵 cv::Mat rotMat; if (seq == "zyx") rotMat = RotX * RotY * RotZ; else if (seq == "yzx") rotMat = RotX * RotZ * RotY; else if (seq == "zxy") rotMat = RotY * RotX * RotZ; else if (seq == "yxz") rotMat = RotZ * RotX * RotY; else if (seq == "xyz") rotMat = RotZ * RotY * RotX; else if (seq == "xzy") rotMat = RotY * RotZ * RotX; else { cout << "Euler Angle Sequence string is wrong..."; } if (!isRotatedMatrix(rotMat)) //欧拉角特殊情况下会出现死锁 { cout << "Euler Angle convert to RotatedMatrix failed..." << endl; exit(-1); } return rotMat; } /************************************************** * @brief 将四元数转换为旋转矩阵 * @note * @param const Vec4d& q 归一化的四元数: q = q0 + q1 * i + q2 * j + q3 * k; * @return 返回3*3旋转矩阵R **************************************************/ Mat quaternionToRotatedMatrix(const Vec4d& q) { double q0 = q[0], q1 = q[1], q2 = q[2], q3 = q[3]; double q0q0 = q0 * q0, q1q1 = q1 * q1, q2q2 = q2 * q2, q3q3 = q3 * q3; double q0q1 = q0 * q1, q0q2 = q0 * q2, q0q3 = q0 * q3; double q1q2 = q1 * q2, q1q3 = q1 * q3; double q2q3 = q2 * q3; //根据公式得来 Mat RotMtr = (Mat_<double>(3, 3) << (q0q0 + q1q1 - q2q2 - q3q3), 2 * (q1q2 + q0q3), 2 * (q1q3 - q0q2), 2 * (q1q2 - q0q3), (q0q0 - q1q1 + q2q2 - q3q3), 2 * (q2q3 + q0q1), 2 * (q1q3 + q0q2), 2 * (q2q3 - q0q1), (q0q0 - q1q1 - q2q2 + q3q3)); //这种形式等价 /*Mat RotMtr = (Mat_<double>(3, 3) << (1 - 2 * (q2q2 + q3q3)), 2 * (q1q2 - q0q3), 2 * (q1q3 + q0q2), 2 * (q1q2 + q0q3), 1 - 2 * (q1q1 + q3q3), 2 * (q2q3 - q0q1), 2 * (q1q3 - q0q2), 2 * (q2q3 + q0q1), (1 - 2 * (q1q1 + q2q2)));*/ return RotMtr; } /************************************************** * @brief 将采集的原始数据转换为齐次矩阵(从机器人控制器中获得的) * @note * @param Mat& m 1*6//1*10矩阵 , 元素为: x,y,z,rx,ry,rz or x,y,z, q0,q1,q2,q3,rx,ry,rz * @param bool useQuaternion 原始数据是否使用四元数表示 * @param string& seq 原始数据使用欧拉角表示时,坐标系的旋转顺序 * @return 返回转换完的齐次矩阵 **************************************************/ Mat attitudeVectorToMatrix(const Mat& m, bool useQuaternion, const string& seq) { CV_Assert(m.total() == 6 || m.total() == 10); //if (m.cols == 1) //转置矩阵为行矩阵 // m = m.t(); Mat temp = Mat::eye(4, 4, CV_64FC1); if (useQuaternion) { Vec4d quaternionVec = m({ 3,0,4,1 }); //读取存储的四元数 quaternionToRotatedMatrix(quaternionVec).copyTo(temp({ 0,0,3,3 })); } else { Mat rotVec; if (m.total() == 6) { rotVec = m({ 3,0,3,1 }); //读取存储的欧拉角 } if (m.total() == 10) { rotVec = m({ 7,0,3,1 }); } //如果seq为空,表示传入的是3*1旋转向量,否则,传入的是欧拉角 if (0 == seq.compare("")) { Rodrigues(rotVec, temp({ 0,0,3,3 })); //罗德利斯转换 } else { eulerAngleToRotateMatrix(rotVec, seq).copyTo(temp({ 0,0,3,3 })); } } //存入平移矩阵 temp({ 3,0,1,3 }) = m({ 0,0,3,1 }).t(); return temp; //返回转换结束的齐次矩阵 }