c++ - Delaunay 三角剖分 opencv c++

标签 c++ opencv

多亏了这段代码,我用 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);

结果:

enter image description here

最佳答案

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

在你的显示函数中写:

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


    }
}

关于c++ - Delaunay 三角剖分 opencv c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16603780/

相关文章:

c++ - 在代码中包含大量常量数据是一种不好的做法吗?

c++ - std::mt19937_64 比 std::mt19937 快吗?

c++ - 将 int 附加到 std::string

c++ - C++ fstream:get()跳过第一行和最后一行

android - 使用opencv在android中抓取连续帧

c++ - OpenCV 将子矩阵复制到图像的另一个 ROI 中

opencv - 打开文件时出错 (/home/vaibhav/opencv/modules/highgui/src/cap_ffmpeg_impl.hpp :553) in openCV

C++ cout 有时只打印

opencv - opencv中是否有用于处理Blob的好的库?

python - 在 Jython 中使用 JavaCV