OpenCV中Delaunay三角网算法例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <opencv2/opencv.hpp>
#include <vector>
 
using namespace cv;
using namespace std;
 
typedef struct _TRIANGLE_DESC_
{
    Point pt1, pt2, pt3;
    _TRIANGLE_DESC_(const Point _pt1, const Point _pt2, const Point _pt3):
        pt1(_pt1), pt2(_pt2), pt3(_pt3){}
}TRIANGLE_DESC;
 
vector<TRIANGLE_DESC> delaunayAlgorithm(const Rect boundRc,const vector<Point>& points)
{
    if (points.empty())
    {
        return vector<TRIANGLE_DESC>();
    }
    vector<TRIANGLE_DESC> result;
 
    vector<Vec6f> _temp_result;
    Subdiv2D subdiv2d(boundRc);
    for (const auto point : points)
    {
        subdiv2d.insert(Point2f((float)point.x, (float)point.y));
    }  
    subdiv2d.getTriangleList(_temp_result);
 
    for (const auto _tmp_vec : _temp_result)
    {
        Point pt1((int)_tmp_vec[0], (int)_tmp_vec[1]);
        Point pt2((int)_tmp_vec[2], (int)_tmp_vec[3]);
        Point pt3((int)_tmp_vec[4], (int)_tmp_vec[5]);
        result.push_back(TRIANGLE_DESC(pt1, pt2, pt3));
    }
    return result;
}
 
int main(int argc, char* argv[])
{
    const int width = 400;
    const int height = 400;
    Mat srcImg(height, width, CV_8UC3, Scalar(255,255,255));
    const vector<Point> testPoints = {
        Point(23, 45), Point(243, 145), Point(308, 25),
        Point(180, 230), Point(343, 145), Point(108, 25)
    };
    for (const auto point : testPoints)
    {
        circle(srcImg, point, 1, Scalar(0), 2);
    }
 
    //
    const Rect pageRc(0, 0, width, height);
    const auto triangles = delaunayAlgorithm(pageRc,testPoints);
    for (const auto triangle : triangles)
    {
        line(srcImg, triangle.pt1, triangle.pt2, Scalar(255, 0, 0));
        line(srcImg, triangle.pt1, triangle.pt3, Scalar(255, 0, 0));
        line(srcImg, triangle.pt2, triangle.pt3, Scalar(255, 0, 0));
    }
    imshow("src", srcImg);
    waitKey(0);
 
    return 0;
}

  

posted @   *神气*  阅读(3976)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示