首先,您应该使用findContours 检测每个对象。然后您可以在每个找到的轮廓上使用minimum area rectangle function。我假设你知道你的十字架的大小和坐标,所以你可以使用 MCvBox2D 的中心坐标来获得它的偏移量。此外,您可以读取框的角度属性,使其符合您的目的。
对于第二部分,我会尝试拟合最小二乘反应角。与方形相比,圆形部分似乎非常小,所以也许它会起作用。
也许 AForge 库中的 Detection of Quadrilaterlas 也可以帮助您。
编辑:
要合并你的轮廓,我会尝试这样的事情:
Rectangle merged = New Rectangle(New Point(img.Width, img.Height), New Size(0, 0)); //img is your binarized image
Contour<Point> Pad_contours= img.FindContours(CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,CvEnum.RETR_TYPE.CV_RETR_LIST);
while(Pad_contours !=null)
{
if(Pad_contours.Area <Pad_Area_max &&Pad_contours>Pad_Area_min)//Filter Pads to avoid false positive Contours
{
//Merge Pad contours into a single rectangle
if(merged.Height >0)
merged.Bottom=Math.Max(merged.Bottom,Pad_Contours.BoundingRectangle.Bottom);
else
merged.Height = contours.BoundingRectangle.Height;
merged.Top=Math.Min(merged.Top,Pad_Contours.BoundingRectangle.Top);
merged.Left=math.Min(merged.Left,Pad_Contours.BoundingRectangle.Left);
if(merged.Width>0)
merged.Right=Max(merged.Right,pad_Contours.BoundingRectangle.Right);
else
merged.Width=Pad_Contours.BoundingRectangle.Width;
}
//get next Pad
If(Pad_contours.VNext==null)
Pad_contours=Pad_contours.HNext;
else
Pad_contours = Pad_contours.VNext;
}
“合并”的矩形现在应该包含所有的 Pad。问题是这样你不会得到一个角度,因为矩形总是垂直 90°。为了解决这个问题,我将遍历上面显示的轮廓,并将每个轮廓的每个点存储在一个额外的数据容器中。然后我会使用上面提到的最小面积矩形函数并将其应用于所有收集的点。这应该会给你一个带有角度属性的边界矩形。