• pyclipper和ClipperLib操作多边型


    目录

    1. 等距离缩放多边形

    1.1 python

    1.2 c++


    1. 等距离缩放多边形

    1.1 python

    1. 环境配置
    2. pip install opencv-python opencv-contrib-python
    3. pip install pyclipper
    4. pip install numpy
    1. import cv2
    2. import numpy as np
    3. import pyclipper
    4. def equidistant_zoom_contour(contour, margin):
    5. """
    6. 等"距离"缩放多边形轮廓点
    7. :param contour: 一个图形的轮廓格式[[[x1, x2]],...],shape是(-1, 1, 2)
    8. :param margin: 轮廓外扩的像素距离,margin正数是外扩,负数是缩小
    9. :return: 外扩后的轮廓点
    10. """
    11. pco = pyclipper.PyclipperOffset()
    12. # 参数限制,默认成2,这里设置大一些,主要是用于多边形的尖角是否用圆角代替
    13. pco.MiterLimit = 10 # 2是圆角,10是尖角
    14. contour = contour[:, 0, :]
    15. pco.AddPath(contour, pyclipper.JT_MITER, pyclipper.ET_CLOSEDPOLYGON)
    16. solution = pco.Execute(margin)
    17. solution = np.array(solution).reshape(-1, 1, 2).astype(int)
    18. return solution
    19. if __name__ == '__main__':
    20. poly = np.array([[[200, 200]], [[200, 300]], [[400, 350]], [[350, 200]], [[300, 200]], [[200, 100]]])
    21. contour1 = equidistant_zoom_contour(poly, 20) # 等距离
    22. img = np.zeros((500, 500, 3))
    23. cv2.polylines(img, [contour1], True, (0, 255, 0), 3)
    24. cv2.polylines(img, [poly], True, (0, 0, 255), 3)
    25. cv2.namedWindow("img", cv2.WINDOW_NORMAL), cv2.imshow("img", img), cv2.waitKey()

    参数MiterLimit=10是尖角(左图),默认值是2,圆角(右图) 

     

    1.2 c++

    第一版clipper: Download Clipper

    最新版clipper2: https://github.com/AngusJohnson/Clipper2

    官方介绍:https://angusj.com/clipper2/Docs/Overview.htm 

     (1)这里使用旧版clipper,下载后解压

    (2)vs2019配置clipper环境,只需要添加包含目录即可。

    (3) 添加现有clipper头文件和源码 clipper.cpp和clipper.hpp

    1. #include
    2. #include
    3. #include
    4. using namespace ClipperLib;
    5. std::vector equidistant_zoom_contour(const std::vector>& contours, double margin) {
    6. ClipperOffset co;
    7. co.MiterLimit = 10; // 默认2圆角,10尖角
    8. co.AddPaths(contours, jtMiter, etClosedPolygon);
    9. Paths solution;
    10. co.Execute(solution, margin);
    11. std::vector result;
    12. for (const auto& path : solution) {
    13. result.insert(result.end(), path.begin(), path.end());
    14. }
    15. return result;
    16. }
    17. int main() {
    18. Paths poly = {
    19. {{200, 200}, {200, 300}, {400, 350}, {350, 200}, {300, 200}, {200, 100}}
    20. };
    21. double margin = 20.0;
    22. std::vector contour1 = equidistant_zoom_contour(poly, margin);
    23. cv::Mat img = cv::Mat::zeros(500, 500, CV_8UC3);
    24. std::vector> contours_cv(1);
    25. for (const auto& point : contour1) {
    26. contours_cv[0].emplace_back(point.X, point.Y);
    27. }
    28. cv::polylines(img, contours_cv, true, cv::Scalar(0, 255, 0), 3);
    29. contours_cv.clear();
    30. for (const auto& path : poly) {
    31. std::vector contour_cv;
    32. for (const auto& point : path) {
    33. contour_cv.emplace_back(point.X, point.Y);
    34. }
    35. contours_cv.push_back(contour_cv);
    36. }
    37. cv::polylines(img, contours_cv, true, cv::Scalar(0, 0, 255), 3);
    38. cv::namedWindow("img", cv::WINDOW_NORMAL);
    39. cv::imshow("img", img);
    40. cv::waitKey(0);
    41. return 0;
    42. }

    MiterLimit默认2圆角(左图),10尖角 (右图)

     

    待续。。。

    参考:https://www.cnblogs.com/01black-white/p/15292193.html

  • 相关阅读:
    bpf对内核的观测
    【STM32F4系列】【HAL库】电机控制(转速和角度)(PID实战1)
    sql 注入(2), 文件读写 木马植入 远程控制
    利用MobaXterm连接服务器的全程配置
    Vue中实现3D得球自动旋转
    go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
    Vue-声明周期函数
    ICPC 2022网络赛(1) - D Find the Number(暴搜预处理)
    Python多进程
    前端需要学习哪些技术?
  • 原文地址:https://blog.csdn.net/jizhidexiaoming/article/details/134435885