简体   繁体   English

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

[英]How to get the first frame of a video using opencv?

I have to find the cross correlation of all the frames of a video with the first frame of that video .... 我必须找到视频的所有帧与该视频的第一帧的互相关...。

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;
}

This is the code for finding the correlation. 这是用于找到相关性的代码。 For img1 I need the frame 1 and rest frames will be img 2 in loop ! 对于img1,我需要框架1,其余框架将成为img 2循环!

How should I do that ? 我该怎么办? Please help! 请帮忙!

try this code...it works.. 试试这个代码...它的工作..

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); 
}

You should probably use the more modern VideoCapture::retrieve function and cv::Mat - but essentially rotating_image is correct. 您可能应该使用更现代的VideoCapture :: retrieve函数和cv :: Mat-但本质上rotating_image是正确的。

Simply grab the first frame into a separate image and then continue reusing a new image for the subsequent frames 只需将第一帧抓取到一个单独的图像中,然后继续为后续帧重新使用新图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM