我在C#中使用opencv来检测网络摄像头图像中的轮廓。如果轮廓有4个点,我想使用它来使用opencv的getPerspective
和warpPerspective
校正包含在此轮廓中的像素的透 View 。职能。
这是获得这4分的代码:
Imgproc.approxPolyDP(perimMat,polyMat,0.005 * perim, true); //get contour of the current detected perimeter as polyMat
List<Point> polyMatList = polyMat.toList(); //convert it to a list of points
if (polyMatList.Count == 4){ //this contour has 4 points, we can use it for getting perspective
Debug.Log("p1x: " + polyMatList[0].x + "p1y: " + polyMatList[0].y); //example log: p1x: 203,p1y: 111
}
因此,既然我有了这些要点列表,我需要确保其顺序为左上,右上,右下,左下,以便可以在
getPerspective
中使用它。我该怎么做呢?我看过其他语言的示例,但是它们通常使用numpy之类的东西进行排序。我对C#不太熟悉(由于Unity而使用它),但是我假设我可以从中借鉴一些辅助方法。
到目前为止,This gist一直是我调整透 View 的主要指南,
for_point_warp
函数似乎提供了不错的指南。我只是不知道C#等效项。 最佳答案
//Begin by sorting your list by y values using List.sort()
polyMatList.sort( (pnt_a, pnt_b) => pnt_b.y - pnt_a.y ); // points 0 & 1 will by definition be your top points and points 2, 3 will be definition be your bottom points.
// now your top 2 points may be out of order since we only sorted by y in the previous step
Point tempPoint;
if(polyMatList[0].x > polyMatList[1].x)
{
tempPoint = polyMatList[0];
polyMatList[0] = polyMatList[1];
polyMatList[1] = tempPoint ;
}
// same goes for your bottom two points
if(polyMatList[2].x > polyMatList[3].x)
{
tempPoint = polyMatList[2];
polyMatList[2] = polyMatList[3];
polyMatList[3] = tempPoint ;
}
//now your list will be ordered tl, tr, bl, br