计算机视觉讨论群162501053


收入囊中
  • 在图片中找到轮廓而且描绘轮廓
  • 使用多边形。圆,椭圆来逼近我们的轮廓

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWJjZDE5OTI3MTln/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">


葵花宝典

关于轮廓提取,几乎相同是一个连通域的推断。

原理还是比較简单的。




初识API

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比較实用.



找到轮廓后。就能够用drawContours画出来
  1. #include "cv.h"
  2. #include "highgui.h"
  3.  
  4. using namespace cv;
  5.  
  6. int main( int argc, char** argv )
  7. {
  8. Mat src;
  9. // the first command-line parameter must be a filename of the binary
  10. // (black-n-white) image
  11. if( argc != 2 || !(src=imread(argv[1], 0)).data)
  12. return -1;
  13.  
  14. Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3);
  15.  
  16. src = src > 1;
  17. namedWindow( "Source", 1 );
  18. imshow( "Source", src );
  19.  
  20. vector<vector<Point> > contours;
  21. vector<Vec4i> hierarchy;
  22.  
  23. findContours( src, contours, hierarchy,
  24. CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
  25.  
  26. // iterate through all the top-level contours,
  27. // draw each connected component with its own random color
  28. int idx = 0;
  29. for( ; idx >= 0; idx = hierarchy[idx][0] )
  30. {
  31. Scalar color( rand()&255, rand()&255, rand()&255 );
  32. drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
  33. }
  34.  
  35. namedWindow( "Components", 1 );
  36. imshow( "Components", dst );
  37. waitKey(0);
  38. }

我们能够用矩形包围轮廓
  1. cv::Rect r0= cv::boundingRect(cv::Mat(contours[0]));
  2. cv::rectangle(result,r0,cv::Scalar(0),2);</span>

也能够用圆
  1. float radius;
  2. cv::Point2f center;
  3. cv::minEnclosingCircle(cv::Mat(contours[1]),center,radius);
  4. cv::circle(result,cv::Point(center),static_cast<int>(radius),cv::Scalar(0),2);

     

也能够用多边形

  1. std::vector<cv::Point> poly;
  2. cv::approxPolyDP(cv::Mat(contours[2]), poly, 5, true);
最左边那个

也能够用还有一种多边形模拟convex hull
  1. std::vector<cv::Point> hull;
  2. cv::convexHull(cv::Mat(contours[3]),hull);

       

也能够用最小外接矩形

  1. vector<RotatedRect> minRect( contours.size() );
  2. for( int i = 0; i < contours.size(); i++ ) {
  3. minRect[i] = minAreaRect( Mat(contours[i]) );
  4. }


也能够用椭圆

  1. vector<RotatedRect> minEllipse( contours.size() );
  2. for( int i = 0; i < contours.size(); i++ )
  3. if( contours[i].size() > 5 )
  4. minEllipse[i] = fitEllipse( Mat(contours[i]) );


还有非常多好用的函数

cv::contourArea  预计一个轮廓的像素数

cv::pointPolygonTest确定一个点是在轮廓内韩式轮廓外

cv::matchShapes度量两个轮廓的相似度

还有cv::moments用于计算重心等信息

荷枪实弹

參考1-轮廓提取 http://docs.opencv.org/master/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html#find-contours

參考2-convexhull逼近 http://docs.opencv.org/master/doc/tutorials/imgproc/shapedescriptors/hull/hull.html#hull

參考3-圆与矩形逼近 http://docs.opencv.org/master/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html

參考4-最小外接矩形与椭圆逼近 http://docs.opencv.org/master/doc/tutorials/imgproc/shapedescriptors/bounding_rotated_ellipses/bounding_rotated_ellipses.html#bounding-rotated-ellipses

OpenCV2马拉松第24圈——轮廓提取的更多相关文章

  1. OpenCV2马拉松第17圈——边缘检測(Canny边缘检測)

    计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g 收入囊中 利用OpenCV Canny函数进行边缘检測 掌握Canny算法基本理论 ...

  2. OpenCV2马拉松第15圈——边缘检測(Laplace算子,LOG算子)

    收入囊中 拉普拉斯算子 LOG算子(高斯拉普拉斯算子) OpenCV Laplacian函数 构建自己的拉普拉斯算子 利用拉普拉斯算子进行图像的锐化 葵花宝典 在OpenCV2马拉松第14圈--边缘检 ...

  3. OpenCV2马拉松第22圈——Hough变换直线检測原理与实现

    计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g/article/details/27220445 收入囊中 Hough变换 概率Ho ...

  4. OpenCV2马拉松第14圈——边缘检測(Sobel,prewitt,roberts)

    收入囊中 差分在边缘检測的角色 Sobel算子 OpenCV sobel函数 OpenCV Scharr函数 prewitt算子 Roberts算子 葵花宝典 差分在边缘检測究竟有什么用呢?先看以下的 ...

  5. OpenCV2马拉松第2圈——读写图片

    收入囊中 用imread读取图片 用nameWindow和imshow展示图片 cvtColor彩色图像灰度化 imwrite写图像 Luv色彩空间转换 初识API 图像读取接口 image = im ...

  6. OpenCV2马拉松第10圈——直方图反向投影(back project)

    收入囊中 灰度图像的反向投影 彩色图像的反向投影 利用反向投影做object detect 葵花宝典 什么是反向投影?事实上没有那么高大上! 在上一篇博文学到,图像能够获得自己的灰度直方图. 反向投影 ...

  7. OpenCV2马拉松第12圈——直方图比較

    收入囊中 使用4种不同的方法进行直方图比較 葵花宝典 要比較两个直方图, 首先必需要选择一个衡量直方图相似度的对照标准.也就是先说明要在哪个方面做对照. 我们能够想出非常多办法,OpenCV採用了下面 ...

  8. openCV2马拉松第19圈——Harris角点检測(自己实现)

    计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g/article/details/26824529 收入囊中 使用OpenCV的con ...

  9. OpenCV2马拉松第5圈——线性滤波

    收入囊中 这里的非常多内容事实上在我的Computer Vision: Algorithms and ApplicationsのImage processing中都有讲过 相关和卷积工作原理 边界处理 ...

随机推荐

  1. Eclipse里web的依赖工程部署的简便方法

    用Eclipse开发项目,曾经为依赖工程的部署问题头疼过,用了MyEclipse之后就没有仔细去研究,最近研究了下,还真找到了比较简便的方法,之前都是采用Ant打jar包,copy到web工程,或者通 ...

  2. Redis主从集群以及Sentinel的配置

    安装完redis后,修改几个redis从节点的配置文件redis.conf,主要是加入主节点位置 slaveof 另外需要修改的地方包括,这样允许其他的从节点连入 bind 0.0.0.0 prote ...

  3. Python学习(六)模块 —— 标准模块

    Python 标准模块 Python 带有一个标准模块库,并发布有独立的文档(库参考手册).对于程序员而言,标准库与语言本身同样重要,它好比一个百宝箱,能为各种常见的任务提供完美的解决方案. 这边简单 ...

  4. Django admin管理

    admin的配置 admin是django强大功能之一,它能共从数据库中读取数据,呈现在页面中,进行管理.默认情况下,它的功能已经非常强大,如果你不需要复杂的功能,它已经够用,但是有时候,一些特殊的功 ...

  5. python with和上下文管理工具

    对于系统资源如文件.数据库连接.socket 而言,应用程序打开这些资源并执行完业务逻辑之后,必须做的一件事就是要关闭(断开)该资源. 比如 Python 程序打开一个文件,往文件中写内容,写完之后, ...

  6. 《java 语言程序设计》第2章编程练习

    2.1 public class test { public static void main(String[] args) { Scanner input = new Scanner(System. ...

  7. UITextView 添加 pleaceholder

    UITextView 默认没有 pleaceholder属性: 我们可以通过多种方式添加 在UITextView的代理方法中写 - (void)textViewDidBeginEditing:(UIT ...

  8. spring错误汇总

    在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结.希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with name 'helloServi ...

  9. 算法笔记_139:二分图的最大权匹配(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 何为二分图的最大权匹配问题? 最大权二分匹配问题就是给二分图的每条边一个权值,选择若干不相交的边,得到的总权值最大. 2 解决方案 对于此问题的讲解 ...

  10. Reimplementing event handler

    Events in PyQt4 are processed often by reimplementing event handlers. #!/usr/bin/python # -*- coding ...