版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/lichengyu/article/details/38392473

一 概念:

Convexity hull, Convexity defects

如上图所示,黑色的轮廓线为convexity hull, 而convexity hull与手掌之间的部分为convexity defects. 每个convexity defect区域有四个特征量:起始点(startPoint),结束点(endPoint),距离convexity hull最远点(farPoint),最远点到convexity hull的距离(depth)。

二.OpenCV中的相关函数

void convexityDefects(InputArray contour, InputArray convexhull, OutputArrayconvexityDefects)

参数:

coutour: 输入参数,检测到的轮廓,可以调用findContours函数得到;

convexhull: 输入参数,检测到的凸包,可以调用convexHull函数得到。注意,convexHull函数可以得到vector<vector<Point>>和vector<vector<int>>两种类型结果,这里的convexhull应该为vector<vector<int>>类型,否则通不过ASSERT检查;

convexityDefects:输出参数,检测到的最终结果,应为vector<vector<Vec4i>>类型,Vec4i存储了起始点(startPoint),结束点(endPoint),距离convexity hull最远点(farPoint)以及最远点到convexity hull的距离(depth)

三.代码

  1. //http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/hull/hull.html
  2. //http://www.codeproject.com/Articles/782602/Beginners-guide-to-understand-Fingertips-counting
  3. #include "opencv2/highgui/highgui.hpp"
  4. #include "opencv2/imgproc/imgproc.hpp"
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. using namespace cv;
  9. using namespace std;
  10. Mat src; Mat src_gray;
  11. int thresh = 100;
  12. int max_thresh = 255;
  13. RNG rng(12345);
  14. /// Function header
  15. void thresh_callback(int, void* );
  16. /** @function main */
  17. int main( int argc, char** argv )
  18. {
  19. /// Load source image and convert it to gray
  20. src = imread( argv[1], 1 );
  21. /// Convert image to gray and blur it
  22. cvtColor( src, src_gray, CV_BGR2GRAY );
  23. blur( src_gray, src_gray, Size(3,3) );
  24. /// Create Window
  25. char* source_window = "Source";
  26. namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  27. imshow( source_window, src );
  28. createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
  29. thresh_callback( 0, 0 );
  30. waitKey(0);
  31. return(0);
  32. }
  33. /** @function thresh_callback */
  34. void thresh_callback(int, void* )
  35. {
  36. Mat src_copy = src.clone();
  37. Mat threshold_output;
  38. vector<vector<Point> > contours;
  39. vector<Vec4i> hierarchy;
  40. /// Detect edges using Threshold
  41. threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
  42. /// Find contours
  43. findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
  44. /// Find the convex hull object for each contour
  45. vector<vector<Point> >hull( contours.size() );
  46. // Int type hull
  47. vector<vector<int>> hullsI( contours.size() );
  48. // Convexity defects
  49. vector<vector<Vec4i>> defects( contours.size() );
  50. for( size_t i = 0; i < contours.size(); i++ )
  51. {
  52. convexHull( Mat(contours[i]), hull[i], false );
  53. // find int type hull
  54. convexHull( Mat(contours[i]), hullsI[i], false );
  55. // get convexity defects
  56. convexityDefects(Mat(contours[i]),hullsI[i], defects[i]);
  57. }
  58. /// Draw contours + hull results
  59. Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
  60. for( size_t i = 0; i< contours.size(); i++ )
  61. {
  62. Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
  63. drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
  64. drawContours( drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
  65. // draw defects
  66. size_t count = contours[i].size();
  67. std::cout<<"Count : "<<count<<std::endl;
  68. if( count < 300 )
  69. continue;
  70. vector<Vec4i>::iterator d =defects[i].begin();
  71. while( d!=defects[i].end() ) {
  72. Vec4i& v=(*d);
  73. //if(IndexOfBiggestContour == i)
  74. {
  75. int startidx=v[0];
  76. Point ptStart( contours[i][startidx] ); // point of the contour where the defect begins
  77. int endidx=v[1];
  78. Point ptEnd( contours[i][endidx] ); // point of the contour where the defect ends
  79. int faridx=v[2];
  80. Point ptFar( contours[i][faridx] );// the farthest from the convex hull point within the defect
  81. int depth = v[3] / 256; // distance between the farthest point and the convex hull
  82. if(depth > 20 && depth < 80)
  83. {
  84. line( drawing, ptStart, ptFar, CV_RGB(0,255,0), 2 );
  85. line( drawing, ptEnd, ptFar, CV_RGB(0,255,0), 2 );
  86. circle( drawing, ptStart, 4, Scalar(255,0,100), 2 );
  87. circle( drawing, ptEnd, 4, Scalar(255,0,100), 2 );
  88. circle( drawing, ptFar, 4, Scalar(100,0,255), 2 );
  89. }
  90. /*printf("start(%d,%d) end(%d,%d), far(%d,%d)\n",
  91. ptStart.x, ptStart.y, ptEnd.x, ptEnd.y, ptFar.x, ptFar.y);*/
  92. }
  93. d++;
  94. }
  95. }
  96. /// Show in a window
  97. namedWindow( "Hull demo", CV_WINDOW_AUTOSIZE );
  98. imshow( "Hull demo", drawing );
  99. //imwrite("convexity_defects.jpg", drawing);
  100. }

四.结果

原图

Convexity defects图,蓝色点是convexity defects的起始点和结束点,红色点是最远点。(为什么有的起始点和结束点中间没有最远点呢?因为只画出了depth范围在20到80之间的convexity defects的起始点、结束点和最远点)

五.参考

[1] Gary Bradski, Adrian Kaehler. Learning OpenCV: Computer Vision with the OpenCV Library. Page258~259.

[2] http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/hull/hull.html

[3] http://www.codeproject.com/Articles/782602/Beginners-guide-to-understand-Fingertips-counting

opencv 凹凸性检测 和 缺陷分析相关推荐

  1. 【OpenCV流程+代码分析】Opencv HOG行人检测 源码分析

    [原文:http://blog.csdn.net/ttransposition/article/details/11874285] OpenCV demo演示见本人的另一篇灌水博客 http://bl ...

  2. Opencv HOG行人检测 源码分析(二)

    前一篇博客大体讲了下思路,对比较难理解的关系有些图示 http://blog.csdn.net/soidnhp/article/details/11874285 /*M/// // // IMPORT ...

  3. OpenCV—python 显着性检测二

    这里只是展示一下OpenCV自带的显着性检测函数.效果都不是很好,各位可以测试一下. 需要OpenCV 3.4或更高版本 cv2.saliency.ObjectnessBING_create() cv ...

  4. 毕业设计 - 题目:基于机器视觉opencv的手势检测 手势识别 算法 - 深度学习 卷积神经网络 opencv python

    文章目录 1 简介 2 传统机器视觉的手势检测 2.1 轮廓检测法 2.2 算法结果 2.3 整体代码实现 2.3.1 算法流程 3 深度学习方法做手势识别 3.1 经典的卷积神经网络 3.2 YOL ...

  5. opencv角点检测学习总结

    学习opencv 角点检测 如果一个点在两个正交方向上都有明显的导数,则我们认为此点更倾向于是独一无二的,所以许多可跟踪的特征点都是角点. 一下为角点检测中用到的一些函数 cvGoodFeatures ...

  6. python运动目标检测与跟踪_基于OpenCV的运动目标检测与跟踪

    尹俊超,刘直芳:基于 OpenCV 的运动目标检测与跟踪 2011, V ol.32, No.8 2817 0 引 言 运动目标检测跟踪技术在航空航天遥感. 生物医学. 工业 自动化生产. 军事公安目 ...

  7. 对一款国家级内容过滤系统Dos安全缺陷分析

    对某款国家级内容过滤系统Dos安全缺陷分析 Author: jianxin [80sec] EMail: jianxin#80sec.com Site: http://www.80sec.com Da ...

  8. opencv(人脸检测和识别)

    Opencv的人脸检测函数,定义了具体可跟踪对象类型的数据文件. Haar级联分类器,通过对比分析相邻图像区域来判断给定图像或子图像与已知对象是否匹配. 两个图像的相似程度可以通过它们对应特征的欧式距 ...

  9. 物联网技术西电捷通TRAIS符合性检测系统的应用研究

    引言   随着信息技术与网络的快速融合发展,以射频识别(Radio Frequency Identification,简称RFID)应用为主要内容的物联网逐渐走进人们的生活,使得无处不在的网络应用成为 ...

最新文章

  1. 如何在Anaconda中安装Pytorch
  2. python3 copy_python3 深浅copy对比详解
  3. 农行基于TFS工具的敏捷转型实践
  4. 【渝粤题库】陕西师范大学201571金融法作业(专升本)
  5. java输出1-100内的所有5的倍数,5个一行
  6. 拍雪景得诗一首,记之,以表心绪[有能和者,不妨凑个热闹给大家解闷]
  7. Android SDK Manager 更新代理配置
  8. php类的实例化方法,php中类的定义和实例化方法
  9. body属性文本标记和排版标记
  10. python如何使用 b_python中的b
  11. 我所认识的EXT2(二)
  12. vs2013 .net连接mysql_Visual Studio C#.NET 轻松连接Mysql数据库之组件mysql-connector-net-网络教程与技术 -亦是美网络...
  13. Activiti学习(二)之工作流的入门与流程实列
  14. Pointofix非常好用的一款屏幕书写软件
  15. 李沐动手学深度学习V2-注意力评分函数
  16. Cisco:CCNA专业英文词汇(1)
  17. 负记账与剩余项目清账虚增借贷的问题
  18. python3-多线程
  19. 清华姚班教师劝退文:读博,你真的想好了吗?
  20. TED听后笔记:如何理解并克服拖延症

热门文章

  1. JAVscript对象
  2. NeurIPS TAPE | 用于评估蛋白质表示学习性能的多任务平台
  3. 第二十九课.回声状态网络ESN
  4. 【问题收录】Ubuntu14.04无法进入到tty1-6的解决办法
  5. MonkeyRunner实例及使用说明
  6. cytoscape插件bingo使用
  7. 调控微生物改善土壤,生物制剂能否开启农业新篇章?
  8. QIIME 2用户文档. 14机器学习预测样品元数据分类和回归q2-sample-classifier(2018.11)
  9. 学习全基因组测序数据分析2:FASTA和FASTQ
  10. Error in eval(predvars, data, env) : object ‘**‘ not found