用opencv檢測convexity defects

一 概念:

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)

 

三.代碼

//http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/hull/hull.html
//http://www.codeproject.com/Articles/782602/Beginners-guide-to-understand-Fingertips-counting
 
#include "opencv2/highgui/highgui.hpp"
 #include "opencv2/imgproc/imgproc.hpp"
 #include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
 
 using namespace cv;
 using namespace std;
 
 Mat src; Mat src_gray;
 int thresh = 100;
 int max_thresh = 255;
 RNG rng(12345);
 
 /// Function header
 void thresh_callback(int, void* );
 
/** @function main */
int main( int argc, char** argv )
 {
   /// Load source image and convert it to gray
   src = imread( argv[1], 1 );
 
   /// Convert image to gray and blur it
   cvtColor( src, src_gray, CV_BGR2GRAY );
   blur( src_gray, src_gray, Size(3,3) );
 
   /// Create Window
   char* source_window = "Source";
   namedWindow( source_window, CV_WINDOW_AUTOSIZE );
   imshow( source_window, src );
 
   createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
   thresh_callback( 0, 0 );
 
   waitKey(0);
   return(0);
 }
 
 /** @function thresh_callback */
 void thresh_callback(int, void* )
 {
   Mat src_copy = src.clone();
   Mat threshold_output;
   vector<vector<Point> > contours;
   vector<Vec4i> hierarchy;
 
   /// Detect edges using Threshold
   threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
 
   /// Find contours
   findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
 
   /// Find the convex hull object for each contour
   vector<vector<Point> >hull( contours.size() );
   // Int type hull
   vector<vector<int>> hullsI( contours.size() );
   // Convexity defects
   vector<vector<Vec4i>> defects( contours.size() );
 
   for( size_t i = 0; i < contours.size(); i++ )
   {  
       convexHull( Mat(contours[i]), hull[i], false ); 
       // find int type hull
       convexHull( Mat(contours[i]), hullsI[i], false ); 
       // get convexity defects
       convexityDefects(Mat(contours[i]),hullsI[i], defects[i]);
   
   }
 
   /// Draw contours + hull results
   Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
   for( size_t i = 0; i< contours.size(); i++ )
      {
        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
        drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
        drawContours( drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
 
        // draw defects
        size_t count = contours[i].size();
        std::cout<<"Count : "<<count<<std::endl;
        if( count < 300 )
            continue;
 
        vector<Vec4i>::iterator d =defects[i].begin();
 
        while( d!=defects[i].end() ) {
            Vec4i& v=(*d);
            //if(IndexOfBiggestContour == i)
            {
 
                int startidx=v[0]; 
                Point ptStart( contours[i][startidx] ); // point of the contour where the defect begins
                int endidx=v[1]; 
                Point ptEnd( contours[i][endidx] ); // point of the contour where the defect ends
                int faridx=v[2]; 
                Point ptFar( contours[i][faridx] );// the farthest from the convex hull point within the defect
                int depth = v[3] / 256; // distance between the farthest point and the convex hull
 
                if(depth > 20 && depth < 80)
                {
                line( drawing, ptStart, ptFar, CV_RGB(0,255,0), 2 );
                line( drawing, ptEnd, ptFar, CV_RGB(0,255,0), 2 );
                circle( drawing, ptStart,   4, Scalar(255,0,100), 2 );
                circle( drawing, ptEnd,   4, Scalar(255,0,100), 2 );
                circle( drawing, ptFar,   4, Scalar(100,0,255), 2 );
                }
 
                /*printf("start(%d,%d) end(%d,%d), far(%d,%d)\n",
                    ptStart.x, ptStart.y, ptEnd.x, ptEnd.y, ptFar.x, ptFar.y);*/
            }
            d++;
        }
 
 
      }
 
   /// Show in a window
   namedWindow( "Hull demo", CV_WINDOW_AUTOSIZE );
   imshow( "Hull demo", drawing );
   //imwrite("convexity_defects.jpg", drawing);
 }


四.結果

原圖

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
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章