#include "opencv2/opencv.hpp"
#include<iostream>
#include <iomanip>
using namespace std;
using namespace cv;
int num=0;
Mat MoveDetect(Mat background, Mat img)
{ //对三个模板进行处理
Mat cloth= imread("cloth.bmp");
cvtColor(cloth, cloth, CV_BGR2GRAY);
threshold(cloth, cloth, 40, 255, CV_THRESH_BINARY);
vector<vector<Point>> contours1;
vector<Vec4i> hierarcy1;
findContours(cloth, contours1, hierarcy1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
Mat scissors= imread("scissors.bmp");
cvtColor(scissors, scissors, CV_BGR2GRAY);
threshold(scissors, scissors, 40, 255, CV_THRESH_BINARY);
vector<vector<Point>> contours2;
vector<Vec4i> hierarcy2;
findContours(scissors, contours2, hierarcy2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
Mat stone= imread("stone.bmp");
cvtColor(stone, stone, CV_BGR2GRAY);
threshold(stone, stone, 40, 255, CV_THRESH_BINARY);
vector<vector<Point>> contours3;
vector<Vec4i> hierarcy3;
findContours(stone, contours3, hierarcy3, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
//得出减去背景后的图像
Mat dst = img.clone();
Mat gray1, gray2;
cvtColor(background, gray1, CV_BGR2GRAY);
cvtColor(img, gray2, CV_BGR2GRAY);
Mat diff;
absdiff(gray1, gray2, diff);
//imshow("absdiss", diff);
threshold(diff, diff, 70, 255, CV_THRESH_BINARY);
//imshow("threshold", diff);
Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
Mat element2 = getStructuringElement(MORPH_RECT, Size(10,10));
medianBlur(diff, diff, 5);//滤波参数根据自己环境调整
//imshow("medianBlur", diff);
/*blur(diff, diff, Size(10, 10));
imshow("blur", diff);*/
erode(diff, diff, element);
//imshow("erode", diff);
dilate(diff, diff, element2);//膨胀参数根据自己环境调整
imshow("dilate", diff);
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(diff, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); //查找轮廓
//drawContours(result, contours, -1, Scalar(0,0,255), 2, 8); //绘制轮廓
for(int i=0;i<contours.size();i++)//与三个模板分别比较匹配度
{
double matchrate1=matchShapes(contours1[0],contours[i],CV_CONTOURS_MATCH_I1, 0.0);
cout<<"index1="<<i<<"---"<<setiosflags(ios::fixed)<<matchrate1<<endl;
double matchrate2=matchShapes(contours2[0],contours[i],CV_CONTOURS_MATCH_I1, 0.0);
cout<<"index2="<<i<<"---"<<setiosflags(ios::fixed)<<matchrate2<<endl;
double matchrate3=matchShapes(contours3[0],contours[i],CV_CONTOURS_MATCH_I1, 0.0);
cout<<"index3="<<i<<"---"<<setiosflags(ios::fixed)<<matchrate3<<endl;
if(matchrate1<matchrate2&&matchrate1<matchrate3)
{
Mat result= imread("cloth1.jpg");
imshow("jieguo", result);
putText(dst, "cloth", Point(30, 70), CV_FONT_HERSHEY_COMPLEX, 2, Scalar(0, 0, 255), 2, 8);
}
if(matchrate2<matchrate1&&matchrate2<matchrate3)
{
Mat result= imread("scissors1.jpg");
imshow("jieguo", result);
putText(dst, "scissors", Point(30, 70), CV_FONT_HERSHEY_COMPLEX, 2, Scalar(0, 0, 255), 2, 8);
}
if(matchrate3<matchrate1&&matchrate3<matchrate1)
{
Mat result= imread("stone1.jpg");
imshow("jieguo", result);
putText(dst, "stone", Point(30, 70), CV_FONT_HERSHEY_COMPLEX, 2, Scalar(0, 0, 255), 2, 8);
}
}
return dst;
}
void main()
{
VideoCapture cap(0);
if(!cap.isOpened()) //检查打开是否成功
return;
Mat frame;
Mat background;
Mat dst;
int count=0;
while(1)
{
cap>>frame;
if(!frame.empty())
{
count++;
if(count==1)
background = frame.clone(); //提取第一帧为背景帧
//imshow("video", frame);
dst = MoveDetect(background, frame);
imshow("result", dst);
if(waitKey(30)==27)
break;
}
else
break;
}
cap.release();
}
没有合适的资源?快使用搜索试试~ 我知道了~
OpenCV手势动作识别--石头剪刀布
共41个文件
tlog:20个
jpg:3个
bmp:3个
4星 · 超过85%的资源 需积分: 48 177 下载量 40 浏览量
2018-04-22
17:23:26
上传
评论 15
收藏 5.42MB ZIP 举报
温馨提示
OPENCV手势动作识别-石头剪刀布,OpenCV3.0版本,VS2012完美运行。
资源推荐
资源详情
资源评论
收起资源包目录
手势动作识别-石头剪刀布.zip (41个子文件)
test2
test2.sln 882B
test2
stone.bmp 67KB
test.cpp 4KB
cloth.bmp 67KB
stone1.jpg 16KB
scissors1.jpg 16KB
test2.vcxproj 4KB
test2.vcxproj.filters 942B
Debug
test.obj 313KB
vc110.idb 531KB
link.9720-mt.write.1.tlog 2B
CL.write.1.tlog 276B
CL.read.1.tlog 23KB
test2.lastbuildstate 58B
test2.Build.CppClean.log 4KB
cl.command.1.tlog 592B
link-cvtres.read.1.tlog 2B
link-mt.read.1.tlog 2B
link.write.1.tlog 350B
link.9720-cvtres.read.1.tlog 2B
link.9720.write.1.tlog 2B
link-rc.write.1.tlog 2B
link-mt.write.1.tlog 2B
link-cvtres.write.1.tlog 2B
link.command.1.tlog 3KB
link-rc.read.1.tlog 2B
link.read.1.tlog 8KB
vc110.pdb 972KB
link.9720-mt.read.1.tlog 2B
link.9720.read.1.tlog 2B
link.9720-rc.write.1.tlog 2B
link.9720-rc.read.1.tlog 2B
test2.log 3KB
link.9720-cvtres.write.1.tlog 2B
cloth1.jpg 16KB
scissors.bmp 67KB
test2.sdf 15.13MB
Debug
test2.ilk 566KB
test2.pdb 1.46MB
test2.exe 94KB
test2.v11.suo 23KB
共 41 条
- 1
资源评论
- vvvv02082020-12-13可以运行,准确度差些
wangshuai610
- 粉丝: 52
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功