用opencv检测convexity defects

原文:http://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)

 

三.代码

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


四.结果

 

原图

 

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

 

五.参考

[1] Gary BradskiAdrian KaehlerLearning 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

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值