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 Program
{
static void Main(string[] args)
{
Mat srcImage = Cv2.ImRead(@"D:\MyData\circle.

本文介绍了如何使用OpenCVSharp库在C#中进行物体轮廓检测。重点讲解了findContours()函数的使用,包括参数contours(存储轮廓的Point[][]类型)和hierarchy(存储图像拓扑结构的HierarchyIndex[]类型)。提供了MyFindContours()函数的封装示例,并展示了测试结果。
最低0.47元/天 解锁文章
2067

被折叠的 条评论
为什么被折叠?



