Table of contents
1. Implementation idea
A polygon is composed of multiple straight lines, so you can form a polygon by drawing multiple straight lines connected end to end
2. Code implementation
void drawPolygon(cv::cuda::GpuMat& nv12Frame, const std::vector<cv::Point>& vertices, cv::Scalar color, int thickness)
{
for (int i = 0;i<vertices.size();i++)
{
//最后一个点和第一个点组成一条线
if (i + 1 == vertices.size())
{
int last_pos = vertices.size() - 1;
drawLine(nv12Frame, {
vertices.at(0).x, vertices.at(0).y }, {
vertices.at(last_pos).x, vertices.at(last_pos).y }, thickness, color);
}
else
{
drawLine(nv12Frame, {
vertices.at(i).x, vertices.at(i).y }, {
vertices.at(i + 1).x, vertices.at(i + 1).y }, thickness, color);
}
}
}
The implementation of drawLine uses cuda to draw a straight line in the previous blog post