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中都有讲过 相关和卷积工作原理 边界处理 ...
随机推荐
- Object 转换为 BigDecimal
项目中遇到读取Excel文件里面的数据转为金额的情况,为了程序更加的健壮,进行处理如下: import java.math.BigDecimal; import java.math.BigIntege ...
- VB开发ActiveX控件的一些记录
注意,下面很多语句的用法,不能放在Private Sub UserControl_Initialize()里使用,要放在Private Sub UserControl_InitProperties() ...
- Hadoop HDFS分布式文件系统设计要点与架构(转摘)
Hadoop简介:一个分布式系统基础架构,由Apache基金会开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力高速运算和存储.Hadoop实现了一个分布式文件系统(Ha ...
- 【OpenMesh】Training:Getting Started with OpenMesh
内容出自236329 - Digital Geometry Processing, Spring2013 - Announcements上的OpenMesh Assignment感觉比较好,适合刚开始 ...
- 如何高效的将excel导入sqlserver?
大部分人都知道用oledb来读取数据到dataset,但是读取之后怎么处理dataset就千奇百怪了.很多人通过循环来拼接sql,这样做不但容易出错而且效率低下,System.Data.SqlClie ...
- javaScript几个操作数组增减元素的函数
1. shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined 2. unshift:将参数添加到原数组开头,并返回数组的长度 var a = [1,2,3,4,5]; ...
- 无需编码开发快速设计互动式UI - uilang
uilang是一个非常小巧的ui类库,可以帮助不熟悉前端代码的web设计人员快速的开发互动式UI.你只需要使用“语义式”的说明来控制元素的动态效果. 开发中你只需要在<code>标签内部输 ...
- Microjs: 超棒的迷你框架和迷你类库搜罗工具
你可以按条件方便的搜索需要的类库或者框架,以下为条件 基础框架 模板引擎 DOM操作 CSS动画 Javascript动画 数据操作 更多 阅读原文:Microjs: 超棒的迷你框架和迷你类库搜罗工具
- C#应用视频教程1.2 Socket通信客户端实现
接下来我们尝试实现最简单的Socket客户端,为了确保只可能你的代码有问题,服务器要先用别人成熟的代码测试(这也是编程的一个技巧,先不要用自己写的客户端测试自己写的服务器,这样出了问题你也不知道谁有问 ...
- Vijos P1002 过河 (NOIP提高组2005)
链接:https://www.vijos.org/p/1002 解析: 若 p*x+(p+1)*y=Q(採用跳跃距离p和p+1时能够跳至不论什么位置Q),则在Q ≥ P*(P-1)时是一定有解的. 因 ...

