输入:我在xy平面上有大约50000个点,如下图所示。
现在,我需要获得三角形ROI中的所有可能点。怎么弄它。它可以是opencv或Matlab。
以下是我需要获取三角区域可能点的样本。
答案 0 :(得分: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')
生成如下图片:
答案 1 :(得分: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;
}
}
}
答案 2 :(得分: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 */
}
}