中央の長方形を見つける必要があるこのような画像がいくつかあります
EmguCVの例のバリエーションを使用して長方形を見つけ、これに付属しています
using (MemStorage storage = new MemStorage())
{ //allocate storage for contour approximation
//Contour<Point> contours = gray.FindContours()
Contour<Point> contours = gray.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST,
storage);
for (; contours != null; contours = contours.HNext)
{
Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
//Seq<Point> currentContour = contours.GetConvexHull(Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE);
if (contours.Area > MinRectangleArea) //only consider contours with area greater than 20000
{
if (currentContour.Total == 4) //The contour has 4 vertices.
{
bool isRectangle = true;
Point[] pts = currentContour.ToArray();
LineSegment2D[] edges = PointCollection.PolyLine(pts, true);
for (int i = 0; i < edges.Length; i++)
{
double angle = Math.Abs(edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i]));
if (angle < 90 - RectangleAngleMargin || angle > RectangleAngleMargin + 90)
{
isRectangle = false;
break;
}
}
if (isRectangle)
{
boxList.Add(currentContour.GetMinAreaRect());
}
}
}
}
}
そして、それらの画像に対してそれを実行した結果、次の 2 つの四角形が見つかることがあります。
オレンジ色の四角形は問題ありません。それが必要です。しかし、私は青を望んでいません。4 つの頂点が画像の境界内にある場合もありますが、通常はそのうちの 1 つが外に出ています。
FindContours 関数の RETR_TYPE を CV_RETR_EXTERNAL に変更すると、青い四角形しか得られないので、外部ポイントで輪郭を取得しないオプションがあるかどうか疑問に思います。
実際の画像では、実際にはオレンジ色の内側に小さな四角形が含まれる場合があります (または、四角形を分割する線が表示されます)。そのため、その後、より大きな四角形を選択して必要なものにしますが、その青い四角形ではそのようにすることはできません。