Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get vertex index from Subdiv2D Delaunay triangulation

I am using OpenCV 3.1 and want to know how to get the vertex index of an edge in Subdiv2D Delaunay triangulation, not only the vertex coordinates? I want to use this index to keep the vertex value for further interpolation.

For example:

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

This gives me only the coordinates of the vertex in org.

I've seen that in OpenCV 2.4, we could use the CvSubdiv2DPoint type, which held the id in addition to the standard Point32f. In OpenCV 3.1, I can't find this struct and it looks like it had been removed for some reason.

like image 461
Elad Joseph Avatar asked Dec 17 '25 14:12

Elad Joseph


2 Answers

If any one else look at that question. You can create a class that herits from subdiv2D and implement your own function that returns index this way:

.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);
  }
}
like image 98
Gabriel Beauchemin-Dauphinais Avatar answered Dec 19 '25 04:12

Gabriel Beauchemin-Dauphinais


So turned out that in OpenCV 3 some of the functions in cv::Subdiv2D which were void now returns an integer. In my case I found out that subdiv.edgeOrg(...) and subdiv.edgeDst(...) returns the vertex id as integer.

like image 36
Elad Joseph Avatar answered Dec 19 '25 02:12

Elad Joseph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!