引自: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_depthfixpt_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;
}

  

opencv使用convexityDefects计算轮廓凸缺陷的更多相关文章

  1. opencv学习之路(27)、轮廓查找与绘制(六)——外接圆、椭圆拟合、逼近多边形曲线、计算轮廓面积及长度、提取不规则轮廓

    一.最小外接圆 #include "opencv2/opencv.hpp" #include<iostream> using namespace std; using ...

  2. opencv —— contourArea、arcLength 计算轮廓面积与长度

    计算轮廓面积:contourArea 函数 double contourArea(InputArray contour, bool oriented = false); contour,输入的二维点集 ...

  3. Opencv笔记(十八)——轮廓的更多函数及其层次结构

    凸缺陷 前面我们已经学习了轮廓的凸包,对象上的任何凹陷都被成为凸缺陷.OpenCV 中有一个函数 cv.convexityDefect() 可以帮助我们找到凸缺陷.函数调用如下: hull = cv2 ...

  4. OpenCV函数:提取轮廓相关函数使用方法

    opencv中提供findContours()函数来寻找图像中物体的轮廓,并结合drawContours()函数将找到的轮廓绘制出.首先看一下findContours(),opencv中提供了两种定义 ...

  5. OpenCV学习(34) 点到轮廓的距离

    在OpenCV中,可以很方便的计算一个像素点到轮廓的距离,计算距离的函数为: double pointPolygonTest(InputArray contour, Point2f pt, bool ...

  6. opencv 图像仿射变换 计算仿射变换后对应特征点的新坐标 图像旋转、缩放、平移

    常常需要最图像进行仿射变换,仿射变换后,我们可能需要将原来图像中的特征点坐标进行重新计算,获得原来图像中例如眼睛瞳孔坐标的新的位置,用于在新得到图像中继续利用瞳孔位置坐标. 仿射变换在:http:// ...

  7. OpenCV——积分图计算

    #include <opencv2/opencv.hpp> #include <iostream> #include "math.h" using name ...

  8. OpenCV——查找、绘制轮廓

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  9. OpenCV学习代码记录——轮廓(contour)检测

    很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...

随机推荐

  1. Base64encode

    一直以来Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Decoder类.这个类是 ...

  2. delphi变量作用域

    完全摘自网络(一件飘雪),供参考: 很多初学者对delphi单元的变量和函数访问权限不理解,在此我举例说明,希望初学者看此文后能茅塞顿开. delphi单元的变量和函数访问权限问题如下两个单元描述: ...

  3. MySql 使用教程(摘自网络)

    一.连接 mysql    格式:mysql-h 主机地址 -u 用户名 p 用户密码    1 例 1 连接到本机上的 mysql    首先在打开 DOS 窗口,然后进入目录 mysql\bin ...

  4. python 主机宝

    需求:开发一个主机批量管理系统,要求按saltstack方式执行命令 #!/usr/bin/env python3. # -*- coding:utf8 -*- import os,sys,pickl ...

  5. iOS开发之视差滚动视图

    首先声明一点,由于自己iOS开发经验有限,这里给下面将要实现的效果起名叫视差滚动视图,自己也不知道是否严谨,等以后有经验了,再来更新吧. 一.需求 有的时候我们可能会有这样一种需求,在一个UITabl ...

  6. 7. Shell 函数

    1. 格式 [ function ] funname [()] { action; [return int;] } 可以带function fun() 定义,也可以直接fun() 定义,不带任何参数 ...

  7. incomplete type is not allowed

    keil环境下,报错#70: incomplete type is not allowed,解决 mqtt_conf.h 定义了一个结构体 mqtt_buffer.h #include <std ...

  8. Microsoft Visual Studio 2012正式版官方下载

    Microsoft Visual Studio 2012正式版官方下载 首先声明,看到园子里还没有类似的新闻,所以斗胆发首页,如有不妥之处,请移除并谅解. 虽然之前已经又用到泄露的MSDN正式版,但今 ...

  9. cocoaPods使用

    1更新版本:pod repo update 有时太老的gem版本,搜索不到最新SDK库 特殊情况下,由于网络或者别的原因,通过 CocoaPods 下载的文件可能会有问题. 这时候您可以删除 Coco ...

  10. phantomjs处理alert、confirm弹窗

    一:phantomjs处理alert弹窗 脚本实现功能为:点击click me按钮弹出弹窗消息为cheese,点击确定按钮,弹窗关闭 脚本代码为:注意的是phantomjs处理alert弹窗需要将ph ...