資源簡介
用opencv2.3.1做的靜態手勢檢測。先濾波去噪-->轉換到HSV空間-->根據皮膚在HSV空間的分布做出閾值判斷,這里用到了inRange函數,然后進行一下形態學的操作,去除噪聲干擾,是手的邊界更加清晰平滑-->得到的2值圖像后用findContours找出手的輪廓,去除偽輪廓后,再用convexHull函數得到凸包絡。

代碼片段和文件信息
/************************************************************************/
/*?
Description: 手勢檢測
先濾波去噪
-->轉換到HSV空間
-->根據皮膚在HSV空間的分布做出閾值判斷,這里用到了inRange函數,
然后進行一下形態學的操作,去除噪聲干擾,是手的邊界更加清晰平滑
-->得到的2值圖像后用findContours找出手的輪廓,去除偽輪廓后,再用convexHull函數得到凸包絡
Author: Yang?Xian
Email: yang_xian521@163.com
Version: 2011-11-2
History:
*/
/************************************************************************/
#include? //?for?standard?I/O
#include????//?for?strings
#include???//?for?controlling?float?print?precision
#include???//?string?to?number?conversion
#include???//?Gaussian?Blur
#include?????????//?Basic?OpenCV?structures?(cv::Mat?Scalar)
#include???//?OpenCV?window?I/O
using?namespace?cv;
using?namespace?std;
int?main(int?argc?char?*argv[])
{
const?std::string?sourceReference?=?“test3.avi“;
int?delay?=?1;
char?c;
int?frameNum?=?-1; //?frame?counter
VideoCapture?captRefrnc(sourceReference);
if?(?!captRefrnc.isOpened())
{
//? cout??<“Could?not?open?reference?“?< return?-1;
}
Size?refS?=?Size(?(int)?captRefrnc.get(CV_CAP_PROP_frame_WIDTH)
(int)?captRefrnc.get(CV_CAP_PROP_frame_HEIGHT)?);
bool?bHandFlag?=?false;
const?char*?WIN_SRC?=?“Source“;
const?char*?WIN_RESULT?=?“Result“;
//?Windows
namedWindow(WIN_SRC?CV_WINDOW_AUTOSIZE?);
namedWindow(WIN_RESULT?CV_WINDOW_AUTOSIZE);
Mat?frame; //?輸入視頻幀序列
Mat?frameHSV; //?hsv空間
Mat?mask(frame.rows?frame.cols?CV_8UC1); //?2值掩膜
Mat?dst(frame); //?輸出圖像
//? Mat?frameSplit[4];
vector?>?contours; //?輪廓
vector?>?filterContours; //?篩選后的輪廓
vector?hierarchy; //?輪廓的結構信息
vector?hull; //?凸包絡的點集
while(true)?//Show?the?image?captured?in?the?window?and?repeat
{
captRefrnc?>>?frame;
if(?frame.empty()?)
{
cout?<“??Game?over!??>?>?>?“;
break;
}
imshow(?WIN_SRC?frame);
//?Begin
//?中值濾波,去除椒鹽噪聲
medianBlur(frame?frame?5);
//? GaussianBlur(?frame?frameHSV?Size(9?9)?2?2?);
//? imshow(“blur2“?frameHSV);
// pyrMeanShiftFiltering(frame?frameHSV?10?10);
// ? imshow(WIN_BLUR?frameHSV);
//?轉換到HSV顏色空間,更容易處理
cvtColor(?frame?frameHSV?CV_BGR2HSV?);
//? split(frameHSV?frameSplit);
//? imshow(WIN_H?frameSplit[0]);
//? imshow(WIN_S?frameSplit[1]);
//? imshow(WIN_V?frameSplit[2]);
Mat?dstTemp1(frame.rows?frame.cols?CV_8UC1);
Mat?dstTemp2(frame.rows?frame.cols?CV_8UC1);
//?對HSV空間進行量化,得到2值圖像,亮的部分為手的形狀
inRange(frameHSV?Scalar(03030)?Scalar(40170256)?dstTemp1);
inRange(frameHSV?Scalar(1563030)?Scalar(180170256)?dstTemp2);
bitwise_or(dstTemp1?dstTemp2?mask);
//? inRange(frameHSV?Scalar(03030)?Scalar(180170256)?dst);
//?形態學操作,去除噪聲,并使手的邊界更加清晰
Mat?element?=?getStructuringElement(MORPH_RE
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????53566??2011-08-12?21:25??handDiscreteTest\Debug\hand.jpg
?????文件?????115200??2011-11-02?14:15??handDiscreteTest\Debug\handDiscreteTest.exe
?????文件?????682620??2011-11-02?14:15??handDiscreteTest\Debug\handDiscreteTest.ilk
?????文件????1182720??2011-11-02?14:15??handDiscreteTest\Debug\handDiscreteTest.pdb
?????文件???????6262??2011-11-02?14:15??handDiscreteTest\handDiscreteTest\Debug\BuildLog.htm
?????文件?????494515??2011-11-02?14:15??handDiscreteTest\handDiscreteTest\Debug\discrete.obj
?????文件????????621??2011-11-02?14:15??handDiscreteTest\handDiscreteTest\Debug\handDiscreteTest.exe.intermediate.manifest
?????文件?????????67??2011-11-02?14:15??handDiscreteTest\handDiscreteTest\Debug\mt.dep
?????文件?????347136??2011-11-02?14:15??handDiscreteTest\handDiscreteTest\Debug\vc90.idb
?????文件?????552960??2011-11-02?14:15??handDiscreteTest\handDiscreteTest\Debug\vc90.pdb
?????文件???????4476??2011-11-02?14:15??handDiscreteTest\handDiscreteTest\discrete.cpp
?????文件???????3758??2011-10-28?15:00??handDiscreteTest\handDiscreteTest\handDiscreteTest.vcproj
?????文件???????1423??2011-11-02?14:15??handDiscreteTest\handDiscreteTest\handDiscreteTest.vcproj.HP98402794308.Administrator.user
?????文件??????24869??2011-10-28?09:46??handDiscreteTest\handDiscreteTest\控制臺版本main.cpp
?????文件???14339072??2011-11-02?14:15??handDiscreteTest\handDiscreteTest.ncb
?????文件????????914??2011-10-27?14:41??handDiscreteTest\handDiscreteTest.sln
????..A..H.?????17408??2011-11-02?14:15??handDiscreteTest\handDiscreteTest.suo
?????目錄??????????0??2011-11-02?14:15??handDiscreteTest\handDiscreteTest\Debug
?????目錄??????????0??2011-11-02?14:15??handDiscreteTest\Debug
?????目錄??????????0??2011-11-02?14:15??handDiscreteTest\handDiscreteTest
?????目錄??????????0??2011-10-27?16:40??handDiscreteTest
-----------?---------??----------?-----??----
?????????????17827587????????????????????21
評論
共有 條評論