如何使用opencv获取视频的第一帧?

时间:2012-09-01 18:44:10

标签: visual-studio-2010 opencv

我必须找到视频的所有帧与该视频的第一帧的互相关....

double crossCorrelation( IplImage* img1, IplImage* img2 ) {
double corr;
int M = img1->width;
int N = img1->height;

BwImage img_1( img1 );
BwImage img_2( img2 );

CvScalar img1_avg = cvAvg( img1, NULL );
CvScalar img2_avg = cvAvg( img2, NULL );

double sum_img1_img2 = 0;
double sum_img1_2 = 0;
double sum_img2_2 = 0;

for( int m=0; m<M; ++m ) {
    for( int n=0; n<N; ++n ) {
        sum_img1_img2   = sum_img1_img2 + (img_1[m][n]-img1_avg.val[0])*(img_2[m][n]-img2_avg.val[0]);  
        sum_img1_2      = sum_img1_2 + (img_1[m][n]-img1_avg.val[0])*(img_1[m][n]-img1_avg.val[0]);
        sum_img2_2      = sum_img2_2 + (img_2[m][n]-img2_avg.val[0])*(img_2[m][n]-img2_avg.val[0]);
    }
}

corr = sum_img1_img2/sqrt(sum_img1_2*sum_img2_2);
return corr;
}

这是查找相关性的代码。对于img1我需要帧1和其余帧将是循环img 2!

我该怎么做? 请帮忙!

2 个答案:

答案 0 :(得分:1)

试试这段代码......它有效..

CvCapture *video = cvCaptureFromFile("C:\\path_to_video.avi");

IplImage *firstFrame = cvQueryFrame(video);//this is the first frame
IplImage *nextFrame;

while(nextFrame!=NULL)
{
   nextFrame = cvQueryFrame(video);

   if(nextFrame!=NULL)
     double CrossCorrValue = crossCorrelation(firstFrame,nextFrame); 
}

答案 1 :(得分:0)

您应该使用更现代的VideoCapture :: retrieve函数和cv :: Mat - 但基本上rotating_image是正确的。

只需将第一帧抓取到单独的图像中,然后继续为后续帧重新使用新图像