實作在姿態辨識之後
在marker上用線畫出正立方體
---
程式碼:
void drawCube(Mat intrinsic, Mat distortion, Vec3d rvec, Vec3d tvec, Mat _frame)
{
double unit = 0.05;
Mat frame = _frame.clone();
Point3d pointO = Point3d(0, 0, 0);
Point3d pointX = Point3d(unit, 0, 0);
Point3d pointXY = Point3d(unit, unit, 0);
Point3d pointY = Point3d(0, unit, 0);
Point3d pointZ = Point3d(0, 0, unit);
Point3d pointXZ = Point3d(unit, 0, unit);
Point3d pointXYZ = Point3d(unit, unit, unit);
Point3d pointYZ = Point3d(0, unit, unit);
vector<Point3f> Point3D;
vector<Point2f> Point2D;
Point3D.push_back(pointO);
Point3D.push_back(pointX);
Point3D.push_back(pointXY);
Point3D.push_back(pointY);
Point3D.push_back(pointZ);
Point3D.push_back(pointXZ);
Point3D.push_back(pointXYZ);
Point3D.push_back(pointYZ);
projectPoints(Point3D, rvec, tvec, intrinsic, distortion, Point2D);
circle(frame, cv::Point(Point2D[0].x, Point2D[0].y), 3, Scalar(255, 0, 255));
circle(frame, cv::Point(Point2D[1].x, Point2D[1].y), 3, Scalar(255, 0, 255));
circle(frame, cv::Point(Point2D[2].x, Point2D[2].y), 3, Scalar(255, 0, 255));
circle(frame, cv::Point(Point2D[3].x, Point2D[3].y), 3, Scalar(255, 0, 255));
//底板兒
line(frame, cv::Point(Point2D[0].x, Point2D[0].y), cv::Point(Point2D[1].x, Point2D[1].y), Scalar(0, 255, 255), 2, 8, 0);
line(frame, cv::Point(Point2D[1].x, Point2D[1].y), cv::Point(Point2D[2].x, Point2D[2].y), Scalar(0, 255, 255), 2, 8, 0);
line(frame, cv::Point(Point2D[2].x, Point2D[2].y), cv::Point(Point2D[3].x, Point2D[3].y), Scalar(0, 255, 255), 2, 8, 0);
line(frame, cv::Point(Point2D[3].x, Point2D[3].y), cv::Point(Point2D[0].x, Point2D[0].y), Scalar(0, 255, 255), 2, 8, 0);
//直立四柱
line(frame, cv::Point(Point2D[0].x, Point2D[0].y), cv::Point(Point2D[4].x, Point2D[4].y), Scalar(0, 0, 255), 2, 8, 0);
line(frame, cv::Point(Point2D[1].x, Point2D[1].y), cv::Point(Point2D[5].x, Point2D[5].y), Scalar(0, 0, 255), 2, 8, 0);
line(frame, cv::Point(Point2D[2].x, Point2D[2].y), cv::Point(Point2D[6].x, Point2D[6].y), Scalar(0, 0, 255), 2, 8, 0);
line(frame, cv::Point(Point2D[3].x, Point2D[3].y), cv::Point(Point2D[7].x, Point2D[7].y), Scalar(0, 0, 255), 2, 8, 0);
//蓋子
line(frame, cv::Point(Point2D[7].x, Point2D[7].y), cv::Point(Point2D[4].x, Point2D[4].y), Scalar(0, 255, 0), 2, 8, 0);
line(frame, cv::Point(Point2D[4].x, Point2D[4].y), cv::Point(Point2D[5].x, Point2D[5].y), Scalar(0, 255, 0), 2, 8, 0);
line(frame, cv::Point(Point2D[5].x, Point2D[5].y), cv::Point(Point2D[6].x, Point2D[6].y), Scalar(0, 255, 0), 2, 8, 0);
line(frame, cv::Point(Point2D[6].x, Point2D[6].y), cv::Point(Point2D[7].x, Point2D[7].y), Scalar(0, 255, 0), 2, 8, 0);
imshow("Cube", frame);
}
---
運行結果:
No comments:
Post a Comment