OpenCv提供了函数 findContours()用于对物体轮廓进行检测,该函数实现算法是由S.suzuki K.Abe于1985年发表的。OpenCVSharp封装了这个函数,有2个参数(contours,hierarchy)要做特别的说明。
public static void FindContours(InputOutputArray image, out Point[][] contours,
out HierarchyIndex[] hierarchy, RetrievalModes mode,
ContourApproximationModes method, Point? offset = null);
解析:contours 的类型是Point[][],它相当于OpenCV中的Vector<Vector<Point>> contours
,存储多个轮廓,每个轮廓是由若干个点组成,可以在该函数前声明Point[][] contours;
,在C#中没有赋值的变量在用的时候是不允许的,因为它是输出的结果,可以不需要给它new空间,但必须在函数的参数中声明是out;参数hierarchy为包含图像拓扑结构的信息,它是HierarchyIndex[]类型,这是输入的结果,同样要在函数的参数中声明为out。具体代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
using OpenCvSharp.Extensions;
namespace OpenCvSharp_03
{
class Prog