c# – 在位图中查找blob
发布时间:2020-12-15 08:37:30 所属栏目:百科 来源:网络整理
导读:我在位图中使用 AForge.Net查找blob,我的位图如下: 我的问题是AForge.Net只检测到一个blob,实际上细线上有两个连接的blob. 我的问题是有一个算法可以确定它们之间有两个连接很薄的大blob?我如何在C#或VB中实现此算法? 样品图片: 解决方法 正如其他人所建
我在位图中使用
AForge.Net查找blob,我的位图如下:
我的问题是AForge.Net只检测到一个blob,实际上细线上有两个连接的blob. 我的问题是有一个算法可以确定它们之间有两个连接很薄的大blob?我如何在C#或VB中实现此算法? 样品图片: 解决方法
正如其他人所建议的,我会使用OpenCv而不是AForge(似乎AForge已经有一段时间没有更新,OpenCv有很多可用的样本).
使用C#,我建议使用 OpenCvSharp nuget包.它很容易使用,因为代码看起来像C或python代码,就像大多数样本一样. 所以,OpenCv有一个blob探测器,但是它会检测blob中心,所以在你的情况下,你看起来比轮廓更多(通常是这种情况). 幸运的是,使用OpenCv和你的样本图像,它只是没有做任何花哨的事情(我们甚至不必先侵蚀图像),我们可以使用findContours,过滤一些毛刺,并获得convexHull.这是一个示例代码,演示了: using (var src = new Mat(filePath)) using (var gray = new Mat()) { using (var bw = src.CvtColor(ColorConversionCodes.BGR2GRAY)) // convert to grayscale { // invert b&w (specific to your white on black image) Cv2.BitwiseNot(bw,gray); } // find all contours var contours = gray.FindContoursAsArray(RetrievalModes.List,ContourApproximationModes.ApproxSimple); using (var dst = src.Clone()) { foreach (var contour in contours) { // filter small contours by their area var area = Cv2.ContourArea(contour); if (area < 15 * 15) // a rect of 15x15,or whatever you see fit continue; // also filter the whole image contour (by 1% close to the real area),there may be smarter ways... if (Math.Abs((area - (src.Width * src.Height)) / area) < 0.01f) continue; var hull = Cv2.ConvexHull(contour); Cv2.Polylines(dst,new[] { hull },true,Scalar.Red,2); } using (new Window("src image",src)) using (new Window("dst image",dst)) { Cv2.WaitKey(); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |