OpenCV based on C++ draws polygons, and polygon multiple edges are drawn with different colors

To draw polygons using the C++ based OpenCV library and use different colors for different sides of the polygon, you can follow these steps:

  1. First, make sure you have installed the OpenCV library and configured your development environment.

  2. Import the necessary header files:

#include <opencv2/opencv.hpp>
  1. Create a blank image, then draw the polygon, choosing a different color for each edge:
int main() {
    
    
    // 创建一个空白的图像
    cv::Mat image(500, 500, CV_8UC3, cv::Scalar(255, 255, 255));

    // 定义多边形的顶点
    std::vector<cv::Point> points;
    points.push_back(cv::Point(100, 100));
    points.push_back(cv::Point(300, 100));
    points.push_back(cv::Point(400, 300));
    points.push_back(cv::Point(200, 400));
    
    // 定义每条边的颜色
    std::vector<cv::Scalar> colors;
    colors.push_back(cv::Scalar(255, 0, 0));   // 蓝色
    colors.push_back(cv::Scalar(0, 255, 0));   // 绿色
    colors.push_back(cv::Scalar(0, 0, 255));   // 红色
    colors.push_back(cv::Scalar(255, 255, 0)); // 青色

    // 在图像上绘制多边形的各条边
    for (size_t i = 0; i < points.size(); i++) {
    
    
        cv::line(image, points[i], points[(i + 1) % points.size()], colors[i], 2);
    }

    // 显示图像
    cv::imshow("Polygon with Different Colored Edges", image);
    cv::waitKey(0);

    return 0;
}

In this example, we create a blank image, define the vertices of the polygon and the color of each edge, and then use the cv::line function to draw the edges of the polygon, Use a different color for each edge. Finally, display the drawn image through cv::imshow.

Please make sure you have configured the OpenCV library and adjust the polygon vertices and colors according to your needs.

Guess you like

Origin blog.csdn.net/CCCrunner/article/details/132273151