OpenCV2马拉松第24圈——轮廓提取
- 在图片中找到轮廓而且描绘轮廓
- 使用多边形。圆,椭圆来逼近我们的轮廓
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWJjZDE5OTI3MTln/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
原理还是比較简单的。
-
C++: void findContours(InputOutputArray image,
OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
-
C++: void findContours(InputOutputArray image,
OutputArrayOfArrays contours, int mode, int method, Point offset=Point())
-
- image – 灰度图片.非0像素都被觉得是1.会被改动.
- contours – 每个被检測到的轮廓都是points的集合
- hierarchy – 可选输出, 跟轮廓数量大小一样.对每个contours[i]来说 ,hierarchy[i][0] , hiearchy[i][1] 分别表示与contours[i]同level的下一个。前一个轮廓索引hiearchy[i][2] ,
andhiearchy[i][3]分别表示contours[i]的子轮廓和父轮廓索引。假设不存在hierarchy[i] will
be negative. - mode –
- CV_RETR_EXTERNAL 仅仅获得最外面的轮廓,也就是说全部轮廓的hierarchy[i][2]=hierarchy[i][3]=-1 .
- CV_RETR_LIST 获得全部的轮廓,无hierarchy
- CV_RETR_CCOMP retrieves all of the contours and organizes them into a two-level hierarchy. At the top level,
there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level. - CV_RETR_TREE 获得全部的轮廓,并输出hierarchy组织
- method –
- CV_CHAIN_APPROX_NONE 保存全部的点。max(abs(x1-x2),abs(y2-y1))==1.也就是说,不论什么轮廓上的两个点都处在一个九宫格内.
- CV_CHAIN_APPROX_SIMPLE 进行了简单的水平竖直对角线压缩.对于一个矩形,仅仅保留了4个顶点.
- CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS [TehChin89]
- offset – 可选的轮廓位移.当分析图片ROI比較实用.
#include "cv.h"
#include "highgui.h" using namespace cv; int main( int argc, char** argv )
{
Mat src;
// the first command-line parameter must be a filename of the binary
// (black-n-white) image
if( argc != 2 || !(src=imread(argv[1], 0)).data)
return -1; Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3); src = src > 1;
namedWindow( "Source", 1 );
imshow( "Source", src ); vector<vector<Point> > contours;
vector<Vec4i> hierarchy; findContours( src, contours, hierarchy,
CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // iterate through all the top-level contours,
// draw each connected component with its own random color
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
Scalar color( rand()&255, rand()&255, rand()&255 );
drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
} namedWindow( "Components", 1 );
imshow( "Components", dst );
waitKey(0);
}
cv::Rect r0= cv::boundingRect(cv::Mat(contours[0]));
cv::rectangle(result,r0,cv::Scalar(0),2);</span>
float radius;
cv::Point2f center;
cv::minEnclosingCircle(cv::Mat(contours[1]),center,radius);
cv::circle(result,cv::Point(center),static_cast<int>(radius),cv::Scalar(0),2);

也能够用多边形
std::vector<cv::Point> poly;
cv::approxPolyDP(cv::Mat(contours[2]), poly, 5, true);
std::vector<cv::Point> hull;
cv::convexHull(cv::Mat(contours[3]),hull);

也能够用最小外接矩形
vector<RotatedRect> minRect( contours.size() );
for( int i = 0; i < contours.size(); i++ ) {
minRect[i] = minAreaRect( Mat(contours[i]) );
}
也能够用椭圆
vector<RotatedRect> minEllipse( contours.size() );
for( int i = 0; i < contours.size(); i++ )
if( contours[i].size() > 5 )
minEllipse[i] = fitEllipse( Mat(contours[i]) );
|
|
|
cv::contourArea 预计一个轮廓的像素数
cv::pointPolygonTest确定一个点是在轮廓内韩式轮廓外
cv::matchShapes度量两个轮廓的相似度
还有cv::moments用于计算重心等信息
荷枪实弹
參考2-convexhull逼近 http://docs.opencv.org/master/doc/tutorials/imgproc/shapedescriptors/hull/hull.html#hull
OpenCV2马拉松第24圈——轮廓提取的更多相关文章
- OpenCV2马拉松第17圈——边缘检測(Canny边缘检測)
计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g 收入囊中 利用OpenCV Canny函数进行边缘检測 掌握Canny算法基本理论 ...
- OpenCV2马拉松第15圈——边缘检測(Laplace算子,LOG算子)
收入囊中 拉普拉斯算子 LOG算子(高斯拉普拉斯算子) OpenCV Laplacian函数 构建自己的拉普拉斯算子 利用拉普拉斯算子进行图像的锐化 葵花宝典 在OpenCV2马拉松第14圈--边缘检 ...
- OpenCV2马拉松第22圈——Hough变换直线检測原理与实现
计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g/article/details/27220445 收入囊中 Hough变换 概率Ho ...
- OpenCV2马拉松第14圈——边缘检測(Sobel,prewitt,roberts)
收入囊中 差分在边缘检測的角色 Sobel算子 OpenCV sobel函数 OpenCV Scharr函数 prewitt算子 Roberts算子 葵花宝典 差分在边缘检測究竟有什么用呢?先看以下的 ...
- OpenCV2马拉松第2圈——读写图片
收入囊中 用imread读取图片 用nameWindow和imshow展示图片 cvtColor彩色图像灰度化 imwrite写图像 Luv色彩空间转换 初识API 图像读取接口 image = im ...
- OpenCV2马拉松第10圈——直方图反向投影(back project)
收入囊中 灰度图像的反向投影 彩色图像的反向投影 利用反向投影做object detect 葵花宝典 什么是反向投影?事实上没有那么高大上! 在上一篇博文学到,图像能够获得自己的灰度直方图. 反向投影 ...
- OpenCV2马拉松第12圈——直方图比較
收入囊中 使用4种不同的方法进行直方图比較 葵花宝典 要比較两个直方图, 首先必需要选择一个衡量直方图相似度的对照标准.也就是先说明要在哪个方面做对照. 我们能够想出非常多办法,OpenCV採用了下面 ...
- openCV2马拉松第19圈——Harris角点检測(自己实现)
计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g/article/details/26824529 收入囊中 使用OpenCV的con ...
- OpenCV2马拉松第5圈——线性滤波
收入囊中 这里的非常多内容事实上在我的Computer Vision: Algorithms and ApplicationsのImage processing中都有讲过 相关和卷积工作原理 边界处理 ...
随机推荐
- 【资料】wod旗帜,纹章
物品 徽章 旗帜 掉落地点 备注 火焰纹章 法师与怪物 火焰魔法.魔法攻防 雄鹰纹章 受诅咒的遗迹 弩系相关 盗贼纹章 捉迷藏 偷袭.匕首.割喉.近攻防 守夜人的纹章 酒馆里平静的一天 钝器.双打.旋 ...
- c#封装三维向量,另外也看了下别人的C++封装
一. c#实现 /* Vector3 Definition Created by taotao man on 2016-4-12 brief:封装三位向量类Vector3 // 修改记录: date: ...
- 【转载】惠新宸:PHP在百度的应用现状及展望
http://blog.sina.com.cn/s/blog_645f8e970100qvd8.html 惠新宸,百度PHP高级顾问,年二十有八,好追根究底,有不良嗜好, 幸性本善.乙酉年识互联网,丁 ...
- DICOM中的入门概念
DICOM标准是医学影像界技术人员逃不掉的标准.本系列专题是JATI对DICOM标准的阐述,力图使PACS管理员和软件工程师都能理解. DICOM标准的提出者DICOM标准委员会是ISO组织的合作者. ...
- Android面试,BroadCastReceiver的两种注册方式的异同
在Android手机应用程序中开发中,需要用到BroadcastReceiver来监听广播的消息.在自定义好BroadcastReceiver ,需要对其进行注册,注册有两种方法: 一种是在代码当中注 ...
- 利用kettle中的JS来完成ETL数据校验
最近参与了一个信托行业的BI项目,由于信托业务系统设计的问题,很多都是用户手工录入的数据,也有一些是需要分析的但是用户没有录入的数据,针对这样的数据质量,我们就要在ETL抽取的过程中来对数据流进行校验 ...
- spring autowired还需要在xml中申明bean ?
如果未自动扫描spring管理的类,则需要在xml中申明.如果自动扫描包下的类,则不需要 (如果配置了自动扫描,还是不行还需要进行手动在xml中声明,则就是工程建立的有问题,包的路径等问题)
- DevExpressComponents-14.2.5 破解过程,正在编写,未完
首先,批评一下cnblogs,在win10的环境下,需要加入到兼容视图列表方可正常编辑内容, 声明,本人在14.2.3 的源代码环境下,编译未通过,相关功能报错,索性选择破解,在此,只是学习测试使用, ...
- [AngularJS] Angular1.3 ngAria - 1
Accessibility is an often overlooked essential feature of a web application. a11y Is a critical comp ...
- 无需编码开发快速设计互动式UI - uilang
uilang是一个非常小巧的ui类库,可以帮助不熟悉前端代码的web设计人员快速的开发互动式UI.你只需要使用“语义式”的说明来控制元素的动态效果. 开发中你只需要在<code>标签内部输 ...

