python 二维点集三角网格化

该代码示例展示了如何使用matplotlib和numpy库在Python中进行二维点集的三角网格化。首先,通过matplotlib的Triangulation类获取三角顶点列表,然后将这些顶点映射到图像上并绘制出三角形。最后,使用opencv的polylines函数绘制出边界,并显示和保存结果图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python 二维点集三角网格化

代码:

import cv2 as cv
import matplotlib.tri as tri
import numpy as np


# 获取三角顶点列表
def get_tri(x, y):
    triObj = tri.Triangulation(x, y)
    print(triObj.triangles)

    tri_lst = []
    triangles = np.array(triObj.triangles)
    NC = len(triangles)
    for i in range(NC):
        vertices = np.zeros((3, 2), dtype=np.float64)
        for j in range(3):
            vertices[j, 0] = x[triangles[i, j]]
            vertices[j, 1] = y[triangles[i, j]]

        points = np.array(vertices, np.int32)
        points = points.reshape((-1, 1, 2))
        tri_lst.append(points)

    return tri_lst


def main():
    # 准备数据
    x = np.random.randint(100, 600, [5])
    y = np.random.randint(100, 600, [5])

    out_point = [[50, 50], [50, 650], [650, 50], [650, 650]]
    for item in out_point:
        x = np.append(x, item[0])
        y = np.append(y, item[1])

    # 创建图片
    img_base = np.zeros([700, 700], np.uint8)

    # 获取三角形
    tri_lst = get_tri(x, y)

    # 绘制三角形
    for item in tri_lst:
        cv.polylines(img_base, [item], True, (255, 255, 255), 2)
        # print(item)

    # 显示图片
    cv.imshow('base', img_base)
    cv.imwrite('base.jpg', img_base)
    cv.waitKey()
    cv.destroyAllWindows()


if __name__ == '__main__':
    main()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值