【OpenCV】Delaunay三角剖分

    xiaoxiao2021-03-26  29

    Delaunay三角剖分

    Delaunay三角剖分算法简述

     

    static void draw_point(Mat& img, Point2f fp, Scalar color) { circle(img, fp, 2, color, CV_FILLED, CV_AA, 0); } // Draw delaunay triangles static void draw_delaunay(Mat& img, Subdiv2D& subdiv, Scalar delaunay_color) { std::vector<Vec6f> triangleList; subdiv.getTriangleList(triangleList); std::vector<Point> pt(3); Size size = img.size(); Rect rect(0, 0, size.width, size.height); for (size_t i = 0; i < triangleList.size(); i++) { Vec6f t = triangleList[i]; pt[0] = Point(cvRound(t[0]), cvRound(t[1])); pt[1] = Point(cvRound(t[2]), cvRound(t[3])); pt[2] = Point(cvRound(t[4]), cvRound(t[5])); // Draw rectangles completely inside the image. if (rect.contains(pt[0]) && rect.contains(pt[1]) && rect.contains(pt[2])) { line(img, pt[0], pt[1], delaunay_color, 1, CV_AA, 0); line(img, pt[1], pt[2], delaunay_color, 1, CV_AA, 0); line(img, pt[2], pt[0], delaunay_color, 1, CV_AA, 0); } } } //Draw voronoi diagram static void draw_voronoi(Mat& img, Subdiv2D& subdiv) { std::vector<std::vector<Point2f> > facets; std::vector<Point2f> centers; subdiv.getVoronoiFacetList(std::vector<int>(), facets, centers); std::vector<Point> ifacet; std::vector<std::vector<Point> > ifacets(1); for (size_t i = 0; i < facets.size(); i++) { ifacet.resize(facets[i].size()); for (size_t j = 0; j < facets[i].size(); j++) ifacet[j] = facets[i][j]; Scalar color; color[0] = std::rand() & 255; color[1] = std::rand() & 255; color[2] = std::rand() & 255; fillConvexPoly(img, ifacet, color, 8, 0); ifacets[0] = ifacet; polylines(img, ifacets, true, Scalar(), 1, CV_AA, 0); circle(img, centers[i], 3, Scalar(), CV_FILLED, CV_AA, 0); } }

     

     

    //-------Delaunay-------- Size size = img.size(); Rect rect(0, 0, size.width, size.height); Subdiv2D subdiv(rect); for (int i = 0; i < 68; i++){ cv::Point2f p; p.x = points[i].x; p.y = points[i].y; subdiv.insert(p); } Scalar delaunay_color(255, 255, 255), points_color(0, 0, 255); draw_delaunay(img, subdiv, delaunay_color); imshow("cccccc", img);


    Taily老段的微信公众号,欢迎交流学习

    https://blog.csdn.net/taily_duan/article/details/81214815


     

     

     

     

     

    转载请注明原文地址: https://ju.6miu.com/read-663492.html

    最新回复(0)