我想在vs2010中用opencv实现一个视频中的各帧图像之间的对比,谁能提供可参考的代码?
具体思路是这样的,读入视频后,提取人物的特征,得到人物的轮廓,检测轮廓面积的变化剧烈程度,大于某个阈值后,提示报警即可。如果我试了之后确实有效,必定悬赏高分!...
具体思路是这样的,读入视频后,提取人物的特征,得到人物的轮廓,检测轮廓面积的变化剧烈程度,大于某个阈值后,提示报警即可。如果我试了之后确实有效,必定悬赏高分!
展开
1个回答
展开全部
我写过类似的,截取一部分你看看
这个是读取视频获得每帧图像的代码并保存
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
//#pragma comment(lib, "ml.lib")
//#pragma comment(lib, "cv.lib")
//#pragma comment(lib, "cvaux.lib")
//#pragma comment(lib, "cvcam.lib")
//#pragma comment(lib, "cxcore.lib")
//#pragma comment(lib, "cxts.lib")
//#pragma comment(lib, "highgui.lib")
//#pragma comment(lib, "cvhaartraining.lib")
int main()
{
CvCapture *capture = NULL;
IplImage *frame = NULL;
char *AviFileName = "e:\\Wildlife.wmv";//视频的目录
char *AviSavePath = "e:\\截图\\";//图片保存的位置
const int jiange = 4;//间隔两帧保存一次图片
capture = cvCaptureFromAVI(AviFileName);
cvNamedWindow("AVI player",1);
int count_tmp = 0;//计数总帧数
char tmpfile[100]={'\0'};
while(true)
{
if(cvGrabFrame(capture))
{
if (count_tmp % jiange == 0)
{
frame=cvRetrieveFrame(capture);
cvShowImage("AVI player",frame);//显示当前帧
sprintf(tmpfile,"%s//%d.jpg",AviSavePath,count_tmp);//使用帧号作为图片名
cvSaveImage(tmpfile,frame);
}
if(cvWaitKey(10)>=0) //延时
break;
++count_tmp;
}
else
{
break;
}
}
cvReleaseCapture(&capture);
cvDestroyWindow("AVI player");
std::cout<<"总帧数" << count_tmp << std::endl;
return 0;
}
获取轮廓代码
m_p_iplContourImg = cvCreateImage(cvGetSize(p_iplSrcImg),IPL_DEPTH_8U,3);
cvZero(m_p_iplContourImg);
CvMemStorage *storage = cvCreateMemStorage(0);
CvSeq * contour = 0;
CvSeq * LocaContour = 0;
double maxaera = 3000, minaera = 800, Maxaera = 0, LocaAera = 0;
IplImage *p_iplDestImgClone = cvCreateImage(cvGetSize(p_iplDestImg),8,1);
p_iplDestImgClone = cvCloneImage(p_iplDestImg) ;
//只找外部轮廓 CV_RETR_EXTERNAL
cvFindContours( p_iplDestImgClone, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
获取轮廓面积
double area = fabs(cvContourArea( contour, CV_WHOLE_SEQ )); //获取当前轮廓面积
希望对你有用
这个是读取视频获得每帧图像的代码并保存
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
//#pragma comment(lib, "ml.lib")
//#pragma comment(lib, "cv.lib")
//#pragma comment(lib, "cvaux.lib")
//#pragma comment(lib, "cvcam.lib")
//#pragma comment(lib, "cxcore.lib")
//#pragma comment(lib, "cxts.lib")
//#pragma comment(lib, "highgui.lib")
//#pragma comment(lib, "cvhaartraining.lib")
int main()
{
CvCapture *capture = NULL;
IplImage *frame = NULL;
char *AviFileName = "e:\\Wildlife.wmv";//视频的目录
char *AviSavePath = "e:\\截图\\";//图片保存的位置
const int jiange = 4;//间隔两帧保存一次图片
capture = cvCaptureFromAVI(AviFileName);
cvNamedWindow("AVI player",1);
int count_tmp = 0;//计数总帧数
char tmpfile[100]={'\0'};
while(true)
{
if(cvGrabFrame(capture))
{
if (count_tmp % jiange == 0)
{
frame=cvRetrieveFrame(capture);
cvShowImage("AVI player",frame);//显示当前帧
sprintf(tmpfile,"%s//%d.jpg",AviSavePath,count_tmp);//使用帧号作为图片名
cvSaveImage(tmpfile,frame);
}
if(cvWaitKey(10)>=0) //延时
break;
++count_tmp;
}
else
{
break;
}
}
cvReleaseCapture(&capture);
cvDestroyWindow("AVI player");
std::cout<<"总帧数" << count_tmp << std::endl;
return 0;
}
获取轮廓代码
m_p_iplContourImg = cvCreateImage(cvGetSize(p_iplSrcImg),IPL_DEPTH_8U,3);
cvZero(m_p_iplContourImg);
CvMemStorage *storage = cvCreateMemStorage(0);
CvSeq * contour = 0;
CvSeq * LocaContour = 0;
double maxaera = 3000, minaera = 800, Maxaera = 0, LocaAera = 0;
IplImage *p_iplDestImgClone = cvCreateImage(cvGetSize(p_iplDestImg),8,1);
p_iplDestImgClone = cvCloneImage(p_iplDestImg) ;
//只找外部轮廓 CV_RETR_EXTERNAL
cvFindContours( p_iplDestImgClone, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
获取轮廓面积
double area = fabs(cvContourArea( contour, CV_WHOLE_SEQ )); //获取当前轮廓面积
希望对你有用
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |