根据你提供的代码,可以实现以下绘制效果:
static void drawImage(cv::Mat& matBGR, const ListBody& bodies)
{
for (const auto& body : bodies)
{
int id = body.id;
const int stickWidth = 4;
int number = -1;
cv::Scalar leftcolor = g_leftLinesColors[3];
cv::Scalar rightcolor = g_rightLinesColors[3];
for (const auto& limb : limbKeypointsIds)
{
auto& first_pt = body.joints[limb.first];
auto& second_pt = body.joints[limb.second];
number++;
if ((first_pt.x < 0.1 && first_pt.y < 0.1) ||
(second_pt.x < 0.1 && second_pt.y < 0.1))
{
continue;
}
cv::Scalar color;
if (limb.first <= 2 && limb.second <= 2)
color = rightcolor;
else if (limb.second % 2 == 0)
color = rightcolor;
else if (limb.second % 2 != 0)
color = leftcolor;
cv::line(matBGR,
cv::Point2d(first_pt.x, first_pt.y),
cv::Point2d(second_pt.x, second_pt.y),
color,
stickWidth);
}
for (int idx = 0; idx < OBS_JOINT_MAX; ++idx)
{
cv::Point2d pt = cv::Point2d(body.joints[idx].x, body.joints[idx].y);
// 绘制关节点
if (pt.x > 0.1 && pt.y > 0.1)
{
cv::Scalar pointColor;
if (idx <= 2)
pointColor = cv::Scalar(255, 255, 255);
else if (idx % 2 == 0)
pointColor = cv::Scalar(255, 255, 255);
else if (idx % 2 != 0)
pointColor = cv::Scalar(255, 255, 0);
cv::circle(matBGR, pt, 4, pointColor, -1);
}
}
}
}
这段代码使用 OpenCV 库绘制身体关键点和连线。通过遍历 bodies 数组中的每个 body,依次获取关节点的坐标信息进行绘制。根据需要修改 cv::circle 和 cv::line 函数的参数来调整关节点和连线的颜色、粗细等属性。
内容由零声教学AI助手提供,问题来源于学员提问




