2

输入:我在 xy 平面上有大约 50000 个点,如下图所示。

现在,我需要在三角形 ROI 中获得所有可能的点。如何得到它。它可以是opencv或Matlab。

下面是我需要获取三角形区域的可能点的示例。

在此处输入图像描述

4

3 回答 3

5

MATLAB 有一个inpolygon命令:inpolygon

例如,这段代码

 xv = [0.1 0.4 0.15 0.1]; yv = [0 0.4 0.8 0];
 x = rand(250,1); y = rand(250,1);
 in = inpolygon(x,y,xv,yv);
 plot(xv,yv,x(in),y(in),'r+',x(~in),y(~in),'bo')

生成这样的图片:

在此处输入图像描述

于 2013-02-27T05:40:44.837 回答
3

像这样的东西,在 C++ 中?

Mat plane = imread("plane.png"); // this is the 50 000 pixel plane image

// I assume your triangles are here. E.e. findContours function would save them here, but I don't know your situation.
vector<vector<Point> > triangles;

// this size is something like 1000, so here are only points that should be checkedd if they are inside some triangle
    vector<Point> points; 

// lets loop all rois and check if point is inside one of them
for (int j = 0; j < triangles.size(); j++) {
    for (int i = 0; i < points.size(); i++) {
     double test = pointPolygonTest(trigangles[j], points[i] false);
     if (test < 0) {
      cout << " point " << points[i] << " is outside of triangle " << j << endl;
     } else if (test > 0) {
      cout << " point " << points[i] << " is inside of triangle " << j << endl;
     } else if (test == 0) {
      cout << " point " << points[i] << " is on edge of triangle " << j << endl;
     }
    }
}

更多信息:http ://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=pointpolygontest#pointpolygontest

这是 OpenCV 的示例:http ://docs.opencv.org/trunk/doc/tutorials/imgproc/shapeescriptors/point_polygon_test/point_polygon_test.html

于 2013-02-27T06:00:35.990 回答
1

在 OpenCV 中,您可以快速过滤掉不在每个三角形的最小边界矩形中的点。该矩形可以预先手动计算或使用cv::boundingRect()计算。命中测试是使用Rect::contains()完成的。这是一个快速操作(至少比 cv::pointPolygonTest 快得多),这将过滤掉显然不在任何三角形中的点。之后,您使用cv::pointPolygonTest()测试通过过滤器的点。

那是:

std::vector<cv::Point> pointList;
std::vector<cv::Rect> rectList;
std::vector<std::vector<Point>> triangleList;

for (int pointIndex = 0; pointIndex < pointList.size(); pointIndex++)
{
  for (int rectIndex = 0; rectIndex < rectList.size(); rectIndex++)
  {
    if (!rectList[rectIndex].contains(pointList[pointIndex]))
      continue;

    if (cv::pointPolygonTest(triangleList[rectIndex], pointList[pointIndex], false) < 0)
      continue;

    /* TODO Mark the point for future use */
  }
}
于 2013-02-27T16:39:06.833 回答