一、轮廓的最小外接矩形
第10.2章,已经介绍了BoundingRectangle()函数求轮廓的外接矩形,其实这个矩形是正矩形,也就是矩形的四条边分别是水平、垂直的。同时Emgu.CV中还有一个函数,可以求出最小外接矩形,这个矩形是带有一定旋转角度的,函数是:
public static RotatedRect MinAreaRect
(
IInputArray points // 输入的轮廓
)
函数返回的是一个RotatedRect,它包含三个成员:
- Angle,顺时针方向的旋转角度。
- Center,旋转矩形的质心。
- Size,旋转矩形的宽度和高度。
二、简单应用
1.原始素材
原始素材srcMat如下图:
2.代码
轮廓检索模式要选择RetrType.List,代码如下:
Mat tempMat = srcMat.Clone();
Mat dstMat = srcMat.Clone();
Mat gray = new Mat();
int threshold = 40;
// 转成灰度图再二值化
CvInvoke.CvtColor(tempMat, gray, ColorConversion.Bgr2Gray);
CvInvoke.Threshold(gray, gray, threshold, 255, ThresholdType.Binary);
CvInvoke.Imshow("Gray and threshold", gray);
// 定义轮廓集合
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
VectorOfRect hierarchy = new VectorOfRect();
CvInvoke.FindContours(gray, contours, hierarchy, RetrType.External, ChainApproxMethod.ChainApproxNone);
// 在一张黑色图中画出所有轮廓
Mat allContours = new Mat(new System.Drawing.Size(gray.Cols, gray.Rows), DepthType.Cv8U, 1);
allContours.SetTo(new MCvScalar(0, 0, 0));
CvInvoke.DrawContours(allContours, contours, -1, new MCvScalar(255, 255, 255), 1);
// 按照面积筛选,太小的轮廓不计算
Dictionary<int, double> dict = new Dictionary<int, double>();
if (contours.Size > 0)
{
for (int i = 0; i < contours.Size; i++)
{
double area = CvInvoke.ContourArea(contours[i]);
if (area > 50 && area < 3000000)
{
dict.Add(i, area);
}
}
}
// 开始画轮廓的最小外接矩形
var item = dict.OrderByDescending(v => v.Value); // v.Value就代表面积,是降序排列
int index = 1;
foreach (var it in item)
{
int key = it.Key;
RotatedRect rotatedRect = CvInvoke.MinAreaRect(contours[key]); // 计算最小外接矩形
PointF[] pointFs = rotatedRect.GetVertices(); // 存储最小外接矩形的4个顶点
CvInvoke.PutText(dstMat, "Contour:" + index.ToString() + " ,angle:" + rotatedRect.Angle.ToString("0.#") + " ,center point:" + rotatedRect.Center + " ,size:" + rotatedRect.Size, new System.Drawing.Point((int)pointFs[1].X, (int)pointFs[1].Y), FontFace.HersheyComplex, 0.5, new Bgr(0, 255, 0).MCvScalar, 1, LineType.EightConnected, false);
for (int j = 0; j < 4; j++)
{
CvInvoke.Line(dstMat, new System.Drawing.Point((int)pointFs[j].X, (int)pointFs[j].Y), new System.Drawing.Point((int)pointFs[(j + 1) % 4].X, (int)pointFs[(j + 1) % 4].Y), new MCvScalar(255, 255, 255), 2);
}
index++;
}
CvInvoke.PutText(dstMat, "Contours number:" + item.Count(), new System.Drawing.Point(20, 20), FontFace.HersheyComplex, 0.5, new Bgr(0, 255, 0).MCvScalar, 1, LineType.EightConnected, false);
CvInvoke.DrawContours(dstMat, contours, -1, new MCvScalar(0, 255, 0), 2, LineType.EightConnected, hierarchy);
CvInvoke.Imshow("All contours, " + allContours.Size.ToString(), allContours);
CvInvoke.Imshow("Final result image, " + dstMat.Size.ToString(), dstMat); // 显示最终结果
3.运行结果
如下所示:
- 最小外接矩形是水平方向顺时针旋转37度。
- 质心坐标是(357 ,397)。
- 最小外接矩形长609,宽208。
原创不易,请勿抄袭。共同进步,相互学习。