【问题标题】:Delaunay triangulation opencv c++Delaunay三角剖分opencv c ++
【发布时间】:2013-05-12 07:28:06
【问题描述】:

感谢这段代码,我使用 openCv 进行了 delaunay 三角测量: example code (特别是 draw_subdiv)。 但是,当我想显示三角剖分时,我得到了不属于三角剖分的网格和线。这些线是由于三角剖分算法开始其工作时考虑到张贴在“无穷大”处的三角形。

你能解释一下如何只将网格绘制到凸包中(没有这条线)吗?

显示功能:

 void draw_subdiv(Mat &img, Subdiv2D& subdiv, Scalar delaunay_color)
{

  vector<Vec6f> triangleList;
  subdiv.getTriangleList(triangleList);
  vector<Point> pt(3);

  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]));

      line(img, pt[0], pt[1], delaunay_color, 1);
      line(img, pt[1], pt[2], delaunay_color, 1);
      line(img, pt[2], pt[0], delaunay_color, 1);
    }
}

主要功能:

Mat image = imread(argv[1], 1);

 ..... ....
 //creat delaunay                                                                                                                                 
 Scalar delaunay_color(255, 255, 255), point_color(0,0,255);
 Rect rect(0,0,image.cols, image.rows);

  Subdiv2D subdiv(rect);

 for(int i = 0; i < point.getDim(); ++i)
    {
      Point2f fp(point.getCoord()[i].real(), point.getCoord()[i].imag());
      subdiv.insert(fp);
    }

 draw_subdiv(image, subdiv, delaunay_color);
 imwrite("data/delaunay.jpg", image);

结果:

【问题讨论】:

  • 请贴出你的展示功能。

标签: c++ opencv


【解决方案1】:

嗯,我认为这很容易。只需检测点何时超出图像而不绘制它们。

在你的显示函数中写:

 void draw_subdiv(Mat &img, Subdiv2D& subdiv, Scalar delaunay_color)
{
  bool draw;
  vector<Vec6f> triangleList;
  subdiv.getTriangleList(triangleList);
  vector<Point> pt(3);

  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]));
      // MY PIECE OF CODE
      draw=true;

      for(int i=0;i<3;i++){
         if(pt[i].x>img.width||pt[i].y>img.heigth||pt[i].x<0||pt[i].y<0)
            draw=false;
      }
      if (draw){
         line(img, pt[0], pt[1], delaunay_color, 1);
         line(img, pt[1], pt[2], delaunay_color, 1);
         line(img, pt[2], pt[0], delaunay_color, 1);
      }


    }
}

【讨论】:

    猜你喜欢
    • 2015-01-07
    • 2012-03-04
    • 1970-01-01
    • 2015-03-07
    • 2019-04-18
    • 2021-05-14
    • 1970-01-01
    • 2017-05-03
    • 2017-06-18
    相关资源
    最近更新 更多