OpenCV-Python实战(10)——详解 OpenCV 轮廓检测

AI摘要

本文系统讲解OpenCV-Python中轮廓检测的核心技术,包括轮廓提取、绘制、压缩,以及基于图像矩的几何特征计算(如质心、面积、圆度、偏心率、纵横比)和Hu不变矩的原理与应用。重点阐述cv2.findContours、cv2.approxPolyDP、cv2.moments及cv2.HuMoments等关键函数的使用方法与数学内涵,强调其在形状分析、目标识别中的平移、缩放、旋转不变性优势。

0. 前言

在计算机视觉领域,轮廓通常指图像中对象边界的一系列点。因此,轮廓通常描述了对象边界的关键信息,包含了有关对象形状的主要信息,该信息可用于形状分析与对象检测和识别。在本文中,将首先通过简单示例了解轮廓的基本概念,然后通过实际示例来了解如何检测和压缩轮廓,最后介绍如何利用图像矩描述检测到的轮廓属性。

1. 轮廓介绍

轮廓视为对象边界曲线包含的所有点,通过对这些点的分析可以进行形状判断以及对象检测和识别等计算机视觉过程。OpenCV 提供了许多函数来检测和处理轮廓,在深入了解这些函数之前,我们首先通过函数模拟观察轮廓的基本结构:

def get_test_contour():
    cnts = [np.array(
       
python opencv轮廓检测 0.边缘和轮廓 边缘检测能够检测出边缘,但是边缘是不连续的。 将边缘连接成一个整体,构成轮廓。 1.图像轮廓检测中注意的问题 对象是二值图像,所以需要进行闽值分割。 在Opencv中,背景必须是黑色的,对象必须是白色的。 因为自动从黑色背景中找白色的轮廓。 2.cv2.findContours()函数 (1)函数接口 cv.findContours( image, ... 阅读详情

相关推荐

OpenCV-Python实战(11)——OpenCV轮廓检测相关应用

在计算机视觉领域,轮廓通常指图像中对象边界的一系列点。因此,轮廓通常描述了对象边界的关键信息,包含了有关对象形状的主要信息,该信息可用于形状分析与对象检测和识别。在本文中,我们将学习如何利用获取到的轮廓,进行形状分析以及对象检测和识别。

盼小辉丶的博客 1万+

Python与OpenCV:轮廓检测

轮廓检测是计算机视觉中的一项重要任务,它用于检测图像中的对象边界。通过以上代码,我们完成了使用Python和OpenCV进行轮廓检测的过程。首先,我们加载待处理的图像,并对其进行预处理,包括灰度化、高斯模糊和二值化。在这个例子中,我们将图像转换为灰度图像,然后应用了高斯模糊来减少噪声。然后,我们使用阈值化操作将图像转换为二值图像,其中二值化阈值为127。最后,我们将检测到的轮廓绘制在原始图像上,并显示或保存结果图像。函数的第一个参数是二值图像,第二个参数是轮廓检测的模式,第三个参数是轮廓的逼近方法。

UixnContext的博客 287

Python,OpenCV轮廓属性、轮廓检测及绘制

这篇博客将介绍OpenCV中的轮廓,轮廓的特征及属性(质心,面积,轮廓,近似轮廓,周长,最小外接圆,面积最小外接矩形,直角矩形,凸包); 计算轮廓属性提取物体的一些常用特性,如坚固性、等效直径、掩模图像、宽高比、平均强度、等效直径、极值点、拟合椭圆等;...

qq_40985985的博客 3789

Python Opencv实现图像轮廓识别功能

主要为大家详细介绍了Python Opencv实现图像轮廓识别功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

DIY nA级源表:从架构到校准的完整实战指南

源表(SMU)是一种在输出或吸收电流的同时精确测量电压,或在输出或吸收电压的同时精确测量电流的仪器。其核心原理基于高精度DAC、ADC及低噪声IV转换电路,技术价值在于实现微电流的高分辨率测量。在传感器暗电流、MOS管亚阈值漏电等测试场景中,nA级源表不可或缺。然而,原厂设备价格高昂,DIY成为可行替代。本文聚焦低电流区间,详细拆解跨阻放大器设计、保护电路、PCB布局及固件校准,分享从选型到实测的完整经验,助你打造一台满足10nA-10mA测试需求的源表。

weixin_29165465的博客 304

直流有刷电机H桥控制实战:从原理图到PWM调速(附DR70x驱动芯片详解)

本文详细解析了直流有刷电机H桥控制的核心技术,从原理图设计到PWM调速实现,特别针对DR70x驱动芯片的应用进行了深入探讨。文章涵盖了H桥电路的电流路径分析、PWM调速的工程细节、DR70x驱动芯片的工作原理及典型应用电路设计,并提供了实测波形分析和故障排查方法,为工程师提供了一套完整的解决方案。

weixin_29215815的博客 149

thinkphp5.0.24反序列化漏洞分析

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!因此收集整理了一份《Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

m0_67392182的博客 1872

SAP S/4HANA Cloud 管理员业务角色创建实战:基于 SAP_BR_ADMINISTRATOR 模板的一站式指南

摘要:SAP S/4HANA Cloud新系统初始登录时常见Fiori Launchpad无磁贴的问题,这是由于缺乏合适的业务角色分配。本文介绍了通过预交付的SAP_BR_ADMINISTRATOR模板创建管理员角色的完整流程,包括业务用户、业务角色和业务目录的核心概念解析。重点说明了直接使用模板的风险,并提供了从模板创建自定义管理员角色的详细步骤(4.1-4.3),以及权限限制设置的注意事项。文中还分享了一个实际项目案例,强调正确配置管理员角色对后续权限体系建设的关键作用。(149字)

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 213

基于FPGA的UDP 通信(一)

手头的FPGA开发板上有一个千兆网口,最近准备做一下以太网通信的内容。本文先介绍基本的理论知识。 FPGA芯片型号:xc7a35tfgg484-2 网口芯片:RTL8211 网络接口:RJ45

记录所学,分享知识,结识挚友 8362

GitLab CI/CD .gitlab-ci.yml 关键词(十一):发布版本 release

本篇文章介绍使用GitLab CI/CD 发布一个版本,对外发布。并填写一些发布的信息。

知无涯 4725

通过实例讲解瀑布模型

在电子商城开发中,瀑布模型如同 “工业时代的流水线”—— 能通过标准化流程交付高质量、高合规性的系统,但也可能因缺乏灵活性而错失市场机会。其优势与缺点本质上源于同一核心特征:严格的阶段性与文档驱动。决策建议若开发传统型、长周期、需求固定的电子商城(如企业内部采购平台),可优先选择瀑布模型,并通过 “需求预验证 + 模块化设计” 降低风险;若开发创新型、短周期、需快速迭代的电商平台(如社交电商、跨境电商),建议采用敏捷模型或混合模型(如瀑布 + Scrum),平衡规范性与响应速度。

跟佟格湾一起学习计算机知识吧~ 274

2024 大型集团人力资源数字化转型业务蓝图规划及实施路径规划方案【附全文阅读】

摘要:本方案针对大型企业人力资源数字化转型需求,重点解决组织协同弱、薪酬绩效管控难等痛点。通过构建"6项能力+8大流程+4大系统"框架,分三阶段(2024-2026年)实施7大项目,包括e-HR系统更换、员工服务平台搭建等核心场景。方案优先推进基础系统改造(2024年),再完善绩效管理和数据分析(2025年),最后落地人才发展等进阶模块(2026年),实现与OA、ERP系统的数据集成,全面提升HR运营效率和员工体验。文末提供的优先级清单明确了10个数字化场景的实施顺序及预期价值。

cdfunlove的博客 1348

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

上一篇: scikit-image 中用于图像分割的阈值算法
下一篇: OpenCV-Python实战(11)——OpenCV轮廓检测相关应用
盼小辉丶
盼小辉丶 优质创作者: 人工智能技术领域 优质创作者: 人工智能技术领域 领域专家: 数据科学与机器学习技术领域 领域专家: 数据科学与机器学习技术领域
博客等级 码龄15年 14万+粉丝 904原创
评论 114
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

盼小辉丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值
⌂ 换网址