如何从Subdiv2D Delaunay三角剖分中获取顶点索引

问题描述 投票:0回答:2

我正在使用OpenCV 3.1并想知道如何在Subdiv2D Delaunay三角剖分中获得边的顶点索引,而不仅仅是顶点坐标?我想使用此索引来保持顶点值以进行进一步插值。

例如:

cv::Subdiv2D subdiv(rect);
// Insert some points to subdiv....
cv::Point2f org, dst;
subdiv.edgeOrg(edge, &org); // starting point of edge

这只给出了org中顶点的坐标。

我已经在OpenCV 2.4中看到过,我们可以使用CvSubdiv2DPoint类型,除了标准的Point32f之外还有id。在OpenCV 3.1中,我找不到这个结构,看起来它因为某些原因被删除了。

c++ opencv opencv3.0 delaunay
2个回答
2
投票

如果有其他人看这个问题。您可以创建一个继承自subdiv2D的类,并实现您自己的函数,以这种方式返回索引:

。H

#ifndef __Subdiv2DIndex__
#define __Subdiv2DIndex__

#include <vector>
#include <opencv2\opencv.hpp>
using namespace cv;

class Subdiv2DIndex : public Subdiv2D
{
public :
  Subdiv2DIndex(Rect rectangle);

  //Source code of Subdiv2D: https://github.com/opencv/opencv/blob/master/modules/imgproc/src/subdivision2d.cpp#L762
  //The implementation tweaks getTrianglesList() so that only the indice of the triangle inside the image are returned
  void getTrianglesIndices(std::vector<int> &ind) const;
};

#endif

.cpp #include“Subdiv2DIndex.h”

Subdiv2DIndex::Subdiv2DIndex(Rect rectangle) : Subdiv2D{rectangle}
{
}

void Subdiv2DIndex::getTrianglesIndices(std::vector<int> &triangleList) const
{
  triangleList.clear();
  int i, total = (int)(qedges.size() * 4);
  std::vector<bool> edgemask(total, false);
  const bool filterPoints = true;
  Rect2f rect(topLeft.x, topLeft.y, bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);

  for (i = 4; i < total; i += 2)
  {
    if (edgemask[i])
      continue;
    Point2f a, b, c;
    int edge_a = i;
    int indexA = edgeOrg(edge_a, &a) -4;
    if (filterPoints && !rect.contains(a))
      continue;
    int edge_b = getEdge(edge_a, NEXT_AROUND_LEFT);
    int indexB = edgeOrg(edge_b, &b) - 4;
    if (filterPoints && !rect.contains(b))
      continue;
    int edge_c = getEdge(edge_b, NEXT_AROUND_LEFT);
    int indexC = edgeOrg(edge_c, &c) - 4;
    if (filterPoints && !rect.contains(c))
      continue;
    edgemask[edge_a] = true;
    edgemask[edge_b] = true;
    edgemask[edge_c] = true;

    triangleList.push_back(indexA);
    triangleList.push_back(indexB);
    triangleList.push_back(indexC);
  }
}

0
投票

事实证明,在OpenCV 3中,cv::Subdiv2D中的一些函数现在是void,它们返回一个整数。在我的例子中,我发现subdiv.edgeOrg(...)subdiv.edgeDst(...)将顶点id作为整数返回。

© www.soinside.com 2019 - 2024. All rights reserved.