我想在vs2010中用opencv实现一个视频中的各帧图像之间的对比,谁能提供可参考的代码?
发布网友
发布时间:2022-04-29 19:07
我来回答
共1个回答
热心网友
时间:2023-10-05 11:14
我写过类似的,截取一部分你看看
这个是读取视频获得每帧图像的代码并保存
#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 )); //获取当前轮廓面积
希望对你有用