C# OpenCvSharp 轮廓检测
效果
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
namespace OpenCvSharp_轮廓检测
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Mat srcImage = Cv2.ImRead("test.jpg");
Mat dstImage = ToolsFindContours(srcImage);
Bitmap Bitmap1 = BitmapConverter.ToBitmap(srcImage);
Bitmap Bitmap2 = BitmapConverter.ToBitmap(dstImage);
pictureBox1.Image = Bitmap1;
pictureBox2.Image = Bitmap2;
}
/// <summary>
/// 查找轮廓
/// </summary>
/// <param name="srcImage"></param>
/// <returns></returns>
public static Mat ToolsFindContours(Mat srcImage)
{
// 转化为灰度图
Mat src_gray = new Mat();
Cv2.CvtColor(srcImage, src_gray, ColorConversionCodes.RGB2GRAY);
// 滤波
Cv2.Blur(src_gray, src_gray, new OpenCvSharp.Size(3, 3));
// Canny边缘检测
Mat canny_Image = new Mat();
// 输入、输出、最小阀值、最大阀值
Cv2.Canny(src_gray, canny_Image, 100, 200);
// 获得轮廓
OpenCvSharp.Point[][] contours;
HierarchyIndex[] hierarchly;
/*
1.寻找轮廓的图像
2.返回轮廓数组
3.层次结构索引
4.轮廓的检索模式(External只检测外轮廓,List检测所有轮廓,CComp检测所有轮廓并建立两个等级,Tree检测所有轮廓并建立等级树
5.轮廓近似模式(ApproxNone保存物体边界上所有连续的轮廓点, ApproxSimple仅保存轮廓的拐点信息。CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用Teh-Chin chain 近似算法)
6.Point偏移量,所有的轮廓信息相对于原始图像对应点的偏移量
*/
Cv2.FindContours(canny_Image, out contours, out hierarchly, RetrievalModes.Tree, ContourApproximationModes.ApproxNone, new OpenCvSharp.Point(0, 0));
// 将结果画出并返回结果
Mat dst_Image = Mat.Zeros(canny_Image.Size(), srcImage.Type());
for (int i = 0; i < contours.Length; i++)
{
// 轮廓的颜色为绿色
Scalar color = new Scalar(0, 255, 0);
/*
1.输入图
2.表示输入的轮廓组
3.指明画第几个轮廓
4.颜色
5.thickness为轮廓的线宽,如果为负值或CV_FILLED表示填充轮廓内部
6.线形
7.轮廓结构信息
*/
Cv2.DrawContours(dst_Image, contours, i, color, 2, LineTypes.Link8, hierarchly);
}
return dst_Image;
}
}
}
C# OpenCvSharp 轮廓检测的更多相关文章
- OpenCV图像轮廓检测
轮廓检测: 轮廓检测的原理通俗的说就是掏空内部点,比如原图中有3*3的矩形点.那么就可以将中间的那一点去掉. 一.关键函数1.1 cvFindContours函数功能:对图像进行轮廓检测,这个函数将 ...
- opencv--图像轮廓检测
//图像的轮廓检测上 //By MoreWindows (http://blog.csdn.net/MoreWindows) #include <opencv2/opencv.hpp> u ...
- [转载+原创]Emgu CV on C# (七) —— Emgu CV on 轮廓检测
轮廓检测 对于查找轮廓我们一般要对图像Canny检测.但是对于很特殊的场合其实我们还可以直接对二值化的图像进行轮廓的提取. 关键函数 1. cvFindContours Retrieves conto ...
- 第十七节,OpenCV(学习六)图像轮廓检测
1.检测轮廓 轮廓检测是图像处理中经常用到的,OpenCV-Python接口中使用cv2.findContours()函数查找检测物体的轮廓. cv2.findContours(image, mode ...
- 机器学习进阶-图像金字塔与轮廓检测-轮廓检测 1.cv2.cvtColor(图像颜色转换) 2.cv2.findContours(找出图像的轮廓) 3.cv2.drawContours(画出图像轮廓) 4.cv2.contourArea(轮廓面积) 5.cv2.arcLength(轮廓周长) 6.cv2.aprroxPloyDP(获得轮廓近似) 7.cv2.boudingrect(外接圆)..
1. cv2.cvtcolor(img, cv2.COLOR_BGR2GRAY) # 将彩色图转换为灰度图 参数说明: img表示输入的图片, cv2.COLOR_BGR2GRAY表示颜色的变换形式 ...
- OpenCV—Python 轮廓检测 绘出矩形框(findContours\ boundingRect\rectangle
千万注意opencv的轮廓检测和边缘检测是两码事 本文链接:https://blog.csdn.net/wsp_1138886114/article/details/82945328 1 获取轮廓 O ...
- 3. OpenCV-Python——图像梯度算法、边缘检测、图像金字塔与轮廓检测、直方图与傅里叶变换
一.图像梯度算法 1.图像梯度-Sobel算子 dst = cv2.Sobel(src, ddepth, dx, dy, ksize) ddepth:图像的深度 dx和dy分别表示水平和竖直方向 ks ...
- 轮廓检测论文解读 | 整体嵌套边缘检测HED | CVPR | 2015
主题列表:juejin, github, smartblue, cyanosis, channing-cyan, fancy, hydrogen, condensed-night-purple, gr ...
- 轮廓检测论文解读 | Richer Convolutional Features for Edge Detection | CVPR | 2017
有什么问题可以加作者微信讨论,cyx645016617 上千人的粉丝群已经成立,氛围超好.为大家提供一个遇到问题有可能得到答案的平台. 0 概述 论文名称:"Richer Convoluti ...
- (opencv5)轮廓检测函数
(opencv5)轮廓检测函数 contours, hierarchy = cv2.findContours(img, mode, method,[offset) 注意 : 1.输入为二值图像,黑色为 ...
随机推荐
- STC系列8位MCU在Windows下的开发
STC系列 MCS-51 8位MCU 简介 STC的8位MCU有89/90/10/11/12/15/8(A/F/G/H)这几个大系列, 都是8051衍生的8位单片机, 每个系列的特点如下 STC89系 ...
- 【Android】使用Socket实现跨设备通讯
1 Socket 简介 Socket(套接字)是应用层与 TCP/IP 协议通信的中间软件抽象层,它是一组接口,用户只需面向 Socket 编程,即可实现跨设备(网络)通讯. Socket 是 ...
- Java设计模式-中介者模式Mediator
介绍 中介者模式(Mediator Pattern),用一个中介对象来封装一系列的对象交互.中介者使各个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 中介者模式属于行 ...
- junit自定义测试方法执行顺序
平常我们写单元测试类,多个方法的执行顺序其实是没有特定顺序的.例如下面代码: package com.laoxu.gamedog; import org.junit.FixMethodOrder; i ...
- "explicit" 的使用
今天在编译项目时,代码审查提示 "Single-parameter constructors should be marked explicit" 于是就在构造函数前加上 expl ...
- win32-创建透明的图片按钮
// Test_CustomButton.cpp : Defines the entry point for the application. // #include "framework. ...
- 安装Standalone模式HBase
所谓Standalone模式HBase,就是只启动一个JVM进程,在这个进程中同时启动了多个后台角色,如:HMaster,单个HRegionServer,以及ZooKeeper服务. 下载安装 最新版 ...
- 图书管理系统---基于form组件和modelform改造添加和编辑
添加 基于form组件改造 步骤1 1.为了区分自己写的form类和视图逻辑,所以工作中需要区分开来,那么就可以在应用下创建一个叫utils的文件夹,专门存放我们写的form类,py文件名随便起 2. ...
- Execl常用快捷操作
常用的操作 Ctrl+A 全选 Ctrl+Z 撤销 Ctrl+X 剪切 Ctrl+C 复制 Ctrl+V 粘贴 Ctrl+B 加粗 Ctrl+S 保存 Ctrl+F 查找 Ctrl+H 替换 Alt+ ...
- 【Azure Fabric Service】怎样关闭 Azure Service Fabric?
问题描述 怎样关闭Azure Service Fabric服务呢?在Azure门户上没有找到 Stop 按钮. 问题回答 Azure Service Fabric 默认是无法停止的,可以删除. 虽然可 ...