• OpenCvSharp 学习三:查找边缘 FindContours


    使用 FindContours() 方法 来查找图像的边缘信息.是一个基本的联系.

    在OpenCvSharp中所有的 FindXXX() 方法 . 都需要开辟一开内存来储存结果使用 CvMesStorage storage = new CvMemStorage() 来开辟. 返回的都是一个CvSeq 序列. CvSeq<CvPoint> contours = new CvSeq<CvPoint>(SeqType.Contour,storage); 也就是这个内存块中装的是这个结果序列.

    最后. 我们使用 DrawContours(CvSeq contours,color 外框颜色 ,Color 内框颜色,int 线条宽). 来将边缘画出.

    View Code
     1 using System;
    2 using OpenCvSharp;
    3
    4 namespace CVS231
    5 {
    6 // 练习 使用FindContours 方法来查找轮廓
    7 public class Program
    8 {
    9 static void Main()
    10 {
    11 using(CvCapture cap = CvCapture.FromCamera(1))
    12 using(CvWindow win = new CvWindow("CVS231"))
    13 {
    14 while (Cv.WaitKey(10) < 0) {
    15 // 得到视频截图
    16 using (IplImage src = cap.QueryFrame())
    17 // 创建一个同等大小的 Canny图
    18 using(IplImage cannyImg = new IplImage(src.Size,BitDepth.U8,1))
    19 // ***所有FindXXX() 方法.都需要开辟一块内存来存储查找到的轮廓信息
    20 using (CvMemStorage storage = new CvMemStorage())
    21 {
    22 // 将视频延Y轴反转.这样,视频里的图像不会是反的. 效果与照镜子一样.
    23 src.Flip(src,FlipMode.Y);
    24 // 用原始图生成Canny图像
    25 src.CvtColor(cannyImg, ColorConversion.BgrToGray);
    26 Cv.Canny(cannyImg,cannyImg,50,120);
    27
    28 // 创建一个序列来存放所找到的轮廓
    29 CvSeq<CvPoint> contours = new CvSeq<CvPoint>(SeqType.Contour,storage);
    30 // ** 使用 FindContours 方法 来在Canny图像中查找轮廓. 返回一个Int值 .是找到的轮廓个数
    31 int num = Cv.FindContours(cannyImg,storage,out contours);
    32 // 使用图像的 DrawContours方法来把这些轮廓画出来,
    33 // 在画之前需要判断下是否查找到轮廓. 找到再画. 没找到就画会出错的.
    34 if (num > 0) {
    35 src.DrawContours(contours,new CvColor(0,255,0),new CvColor(0,0,255),1);
    36 // 这个 DrawContours 方法 在Cv下面也有.跟Flip一个 Cv.DrawContours() 不过他的第一个参数
    37 // 需要告诉画在哪个IplImage上.
    38 }
    39 // 将结果显示到 窗口上.
    40 win.Image = src;
    41 }
    42 }
    43 }
    44 }
    45 }
    46 }

    运行结果:

  • 相关阅读:
    Core Data 入门
    iOS布局之Auto Layout
    iOS 布局之 Springs and Struts
    Soul 学习笔记---使用 nacos 实现数据同步上篇(七)
    Soul 学习笔记---使用 zookeeper 实现数据同步(六)
    Soul 学习笔记---数据同步 websocket 连接建立过程分析(五)
    Soul 学习笔记---soul 数据同步的浅显分析(四)
    Soul学习笔记---运行 soul-examples-dubbo(三)
    Soul学习笔记---运行 soul-examples-http(二)
    windows下安装zookeeper 及 遇到的问题---打开zkServer.cmd闪退,此时不应有 Javajdk1.8.0_144
  • 原文地址:https://www.cnblogs.com/easyfrog/p/2360871.html
Copyright © 2020-2023  润新知