Net AI学习笔记系列第五章 OpenCVSharp实操——图片中物体轮廓查找描绘

.Net AI学习笔记系列

第五章 OpenCVSharp实操——图片中物体轮廓查找描绘



前言

本文主要介绍使用OpenCVSharp中的 FindContours方法识别和提取图像中物体边缘轮廓,并用DrawContours方法描绘轮廓。


一、OpenCVSharp实操——图片中物体轮廓查找描绘

示例展示了一步一步在一张大图中识别查找物体的边缘轮廓,并将轮廓描绘出来。

二、步骤

1.开发工具

	VS2019+.net 4.8+OpenCvSharp4

2.引入库

Install-Package OpenCvSharp4

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;

3.示例代码

代码如下(示例):

//查找轮廓
  private void button1_Click(object sender, EventArgs e)
  {
      //原图像
      Mat src = BitmapConverter.ToMat((Bitmap)this.pictureBox1.Image);

      Mat srcGray = new Mat();
      Cv2.CvtColor(src, srcGray, ColorConversionCodes.BGR2GRAY);

      //二值化
      Mat binaryImg = new Mat();
      double minVal, maxVal;
      Cv2.MinMaxLoc(srcGray, out minVal, out maxVal);
      Cv2.Threshold(srcGray, binaryImg, (minVal + maxVal) / 1.4, 255, ThresholdTypes.Binary);

      // 形态学开操作
      Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(3, 3));
      Mat mb = new Mat();
      Cv2.MorphologyEx(binaryImg, mb, MorphTypes.Open, kernel, new OpenCvSharp.Point(-1, -1), 3, BorderTypes.Default, Scalar.All(0));

      // 边缘检测 + 轮廓
      int threshold = 100;
      Mat cannyOutput = new Mat();
      Cv2.Canny(mb, cannyOutput, threshold, threshold * 2, 3);
      Cv2.FindContours(cannyOutput, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);

      //显示结果
      this.pictureBox2.Image = BitmapConverter.ToBitmap(cannyOutput);
      this.pictureBox2.Refresh();
  }
 //描绘轮廓
   private void button2_Click(object sender, EventArgs e)
   {
       Mat cannyOutput = BitmapConverter.ToMat((Bitmap)this.pictureBox2.Image);
       // 画轮廓
       Mat drawing = new Mat(cannyOutput.Size(), MatType.CV_8UC3, Scalar.Black);
       Random rng = new Random();
       int index = 0;
       foreach (OpenCvSharp.Point[] contour in contours)
       {
           Scalar color = new Scalar(rng.Next(256), rng.Next(256), rng.Next(256));
           Cv2.DrawContours(drawing, contours, index, color, 2, LineTypes.Link8, hierarchy, 0);

           index++;
       }

       //显示结果
       this.pictureBox2.Image = BitmapConverter.ToBitmap(drawing);
       this.pictureBox2.Refresh();
   }

4.运行效果

在这里插入图片描述

总结

在OpenCV中,查找轮廓(Contour Detection)是一种用于识别和提取图像中物体边缘的技术

1.物体识别:
通过查找轮廓,可以识别图像中的物体形状和大小,从而实现对物体的分类和识别。例如,在自动驾驶、机器视觉和图像处理领域,查找轮廓可以帮助识别道路标志、交通信号灯、行人等。
2.图像分割:
查找轮廓可以用于将图像分割成多个区域,每个区域对应一个物体或物体的一部分。这有助于进一步处理和分析图像中的各个区域。
3.几何特征提取:
通过查找轮廓,可以提取物体的几何特征,如面积、周长、质心、凸包等。这些特征可以用于后续的图像处理和分析。
4.形状匹配:
查找轮廓可以用于比较两个或多个物体的形状,以确定它们之间的相似性或差异性。这在模式识别、图像检索和图像分类等领域具有重要应用价值。
5.手势识别:
在计算机视觉和机器学习领域,查找轮廓可以用于识别手势,从而实现基于手势的交互。
6.运动检测:
通过比较连续帧之间的轮廓变化,可以检测图像中的运动物体。这在视频监控、安防监控和运动跟踪等领域具有广泛应用。

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

brooth123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值