引自:http://www.xuebuyuan.com/1684976.html
http://blog.csdn.net/lichengyu/article/details/38392473
http://www.cnblogs.com/yemeishu/archive/2013/01/19/2867286.html談談NITE 2與OpenCV結合提取指尖座標
一 概念:
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);
}
另一個版本的說法
首先介紹今天主角:void convexityDefects(InputArray contour, InputArray、convexhull, OutputArray convexityDefects)
使用時注意,最後一個引數 convexityDefects 是儲存 Vec4i 的向量(vector<varname>),函式計算成功後向量的大小是輪廓凸缺陷的數量,向量每個元素Vec4i儲存了4個整型資料,因為Vec4i對[]實現了過載,所以可以使用 _vectername[i][0] 來訪問向量 _vactername的第i個元素的第一個分量。再說 Vec4i 中儲存的四個整形資料,
Opencv 使用這四個元素表示凸缺陷,
第一個名字叫做
start_index,表示缺陷在輪廓上的開始處,他的值是開始點在函式第一個引數 contour 中的下標索引;
Vec4i 第二個元素的名字叫
end_index, 顧名思義其對應的值就是缺陷結束處在 contour 中的下標索引;
Vec4i 第三個元素
farthest_pt_index 是缺陷上距離 輪廓凸包(convexhull)最遠的點;
Vec4i最後的元素叫
fixpt_depth,fixpt_depth/256 表示了
輪廓上以 farthest_pt_index 為下標的點到 輪廓凸包的(convexhull)的距離,以畫素為單位。
All is so easy!下面就是簡單的程式碼示例(首先計算兩個輪廓的凸包,然後計算兩個輪廓的凸缺陷):
// 計算凸缺陷 convexityDefect
//
#include "stdafx.h"
#include <opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
Mat *img_01 = new Mat(400, 400, CV_8UC3);
Mat *img_02 = new Mat(400, 400, CV_8UC3);
*img_01 = Scalar::all(0);
*img_02 = Scalar::all(0);
// 輪廓點組成的陣列
vector<Point> points_01,points_02;
// 給輪廓組賦值
points_01.push_back(Point(10, 10));points_01.push_back(Point(10,390));
points_01.push_back(Point(390, 390));points_01.push_back(Point(150, 250));
points_02.push_back(Point(10, 10));points_02.push_back(Point(10,390));
points_02.push_back(Point(390, 390));points_02.push_back(Point(250, 150));
vector<int> hull_01,hull_02;
// 計算凸包
convexHull(points_01, hull_01, true);
convexHull(points_02, hull_02, true);
// 繪製輪廓
for(int i=0;i < 4;++i)
{
circle(*img_01, points_01[i], 3, Scalar(0,255,255), CV_FILLED, CV_AA);
circle(*img_02, points_02[i], 3, Scalar(0,255,255), CV_FILLED, CV_AA);
}
// 繪製凸包輪廓
CvPoint poi_01 = points_01[hull_01[hull_01.size()-1]];
for(int i=0;i < hull_01.size();++i)
{
line(*img_01, poi_01, points_01[i], Scalar(255,255,0), 1, CV_AA);
poi_01 = points_01[i];
}
CvPoint poi_02 = points_02[hull_02[hull_02.size()-1]];
for(int i=0;i < hull_02.size();++i)
{
line(*img_02, poi_02, points_02[i], Scalar(255,255,0), 1, CV_AA);
poi_02 = points_02[i];
}
vector<Vec4i> defects;
// 如果有凸缺陷就把它畫出來
if( isContourConvex(points_01) )
{
cout<<"img_01的輪廓是凸包"<<endl;
}else{
cout<<"img_01的輪廓不是凸包"<<endl;
convexityDefects(
points_01,
Mat(hull_01),
defects
);
// 繪製缺陷
cout<<"共"<<defects.size()<<"處缺陷"<<endl;
for(int i=0;i < defects.size();++i)
{
circle(*img_01, points_01[defects[i][0]], 6, Scalar(255,0,0), 2, CV_AA);
circle(*img_01, points_01[defects[i][1]], 6, Scalar(255,0,0), 2, CV_AA);
circle(*img_01, points_01[defects[i][2]], 6, Scalar(255,0,0), 2, CV_AA);
line(*img_01, points_01[defects[i][0]], points_01[defects[i][1]], Scalar(255,0,0), 1, CV_AA);
line(*img_01, points_01[defects[i][1]], points_01[defects[i][2]], Scalar(255,0,0), 1, CV_AA);
line(*img_01, points_01[defects[i][2]], points_01[defects[i][0]], Scalar(255,0,0), 1, CV_AA);
cout<<"第"<<i<<"缺陷<"<<points_01[defects[i][0]].x<<","<<points_01[defects[i][0]].y
<<">,<"<<points_01[defects[i][1]].x<<","<<points_01[defects[i][1]].y
<<">,<"<<points_01[defects[i][2]].x<<","<<points_01[defects[i][2]].y<<">到輪廓的距離為:"<<defects[i][3]/256<<"px"<<endl;
}
defects.clear();
}
if( isContourConvex( points_02 ) )
{
cout<<"img_02的輪廓是凸包"<<endl;
}else{
cout<<"img_02的輪廓不是凸包"<<endl;
vector<Vec4i> defects;
convexityDefects(
points_01,
Mat(hull_01),
defects
);
// 繪製出缺陷的輪廓
for(int i=0;i < defects.size();++i)
{
circle(*img_02, points_01[defects[i][0]], 6, Scalar(255,0,0), 2, CV_AA);
circle(*img_02, points_01[defects[i][1]], 6, Scalar(255,0,0), 2, CV_AA);
circle(*img_02, points_01[defects[i][2]], 6, Scalar(255,0,0), 2, CV_AA);
line(*img_02, points_01[defects[i][0]], points_01[defects[i][1]], Scalar(255,0,0), 1, CV_AA);
line(*img_02, points_01[defects[i][1]], points_01[defects[i][2]], Scalar(255,0,0), 1, CV_AA);
line(*img_02, points_01[defects[i][2]], points_01[defects[i][0]], Scalar(255,0,0), 1, CV_AA);
// 因為 img_02 沒有缺陷所以就懶的寫那些輸出程式碼了
}
defects.clear();
}
imshow("img_01 的輪廓和凸包:", *img_01);
imshow("img_02 的輪廓和凸包:", *img_02);
cvWaitKey();
return 0;
}