#include <stdio.h>
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/video/video.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include "cv.h"
#include "highgui.h"
using namespace std;
int g_slider_position = 0;
CvCapture* g_capture = NULL;
int frames;
bool isPreImage = false;
int lastFrame = 0;
void onTrackbarSlide(int pos) {
cvSetCaptureProperty(
g_capture,
CV_CAP_PROP_POS_FRAMES,
pos
);
frames = pos;
//printf("onTrackbarSlide frames = %d, isPreImage = %d\n", pos, isPreImage);
}
int getAVIFrames(char * fname) {
char tempSize[4];
// Trying to open the video file
ifstream videoFile( fname , ios::in | ios::binary );
// Checking the availablity of the file
if ( !videoFile ) {
cout << "Couldn’t open the input file " << fname << endl;
exit( 1 );
}
// get the number of frames
videoFile.seekg( 0x30 , ios::beg );
videoFile.read( tempSize , 4 );
int frames = (unsigned char ) tempSize[0] + 0x100*(unsigned char ) tempSize[1] + 0x10000*(unsigned char ) tempSize[2] + 0x1000000*(unsigned char ) tempSize[3];
videoFile.close( );
return frames;
}
/************************************************************************/
/* E(x,y,t) = E(x+dx,y+dy,t+dt) —— 光流约束方程
将上式右边做泰勒展开,并令dt->0,则得到:Exu+Eyv+Et = 0,其中:
Ex = dE/dx Ey = dE/dy Et = dE/dt u = dx/dt v = dy/dt
/************************************************************************/
void calculate(const IplImage *pre, const IplImage *cur, IplImage *result, double *opticalflow)
{
static int Ex,Ey,Et;
static int gray1,gray2;
static double u;
static int i,j;
static const int width = cur->width;
static const int height = cur->height;
static char *prePtr;
static char *curPtr;
static char *rePtr;
memset(opticalflow, 0, width * height);
memset(result->imageData, 0, result->imageSize);
for (i = 2; i < height - 2; i++)
{
prePtr = pre->imageData + i * pre->widthStep;
curPtr = cur->imageData + i * cur->widthStep;
rePtr = result->imageData + i * result->widthStep;
for(j = 2; j < width - 2; j++)
{
int base = j * cur->nChannels;
gray1 = (curPtr[base] + curPtr[base + 1] + curPtr[base + 2]) / 3;
gray2 = (prePtr[base] + prePtr[base + 1] + prePtr[base + 2]) / 3;
Et = gray1 - gray2;
gray2 = (curPtr[base + 3] + curPtr[base + 3 + 1] + curPtr[base + 3 + 2]) / 3;
Ex = gray2 - gray1;
base += cur->widthStep;
gray2 = (curPtr[base] + curPtr[base + 1] + curPtr[base + 2]) / 3;
Ey = gray2 - gray1;
/*Ex = ((int)(Ex/10))*10;
Ey = ((int)(Ey/10))*10;
Et = ((int)(Et/10))*10;*/
if(Et < 0) Et = - Et;
u = (Et * 10.0) / (sqrt((Ex * Ex + Ey * Ey) * 1.0) + 0.1);
opticalflow[i * width + j] = u;
if (u < 0) u = -u;
if (u > 640.0)
//if (u > 1.0)
{
base = j * cur->nChannels;
rePtr[base] = 255;
rePtr[base + 255] = 0;
rePtr[base + 255] = 0;
}
}
}
}
void ImgOpticalFlow(IplImage *pre_grey, IplImage *grey)
{
IplImage *velx = cvCreateImage( cvSize(grey->width ,grey->height),IPL_DEPTH_32F, 1 );
IplImage *vely = cvCreateImage( cvSize(grey->width ,grey->height),IPL_DEPTH_32F, 1 );
velx->origin = vely->origin = grey->origin;
CvSize winSize = cvSize(5,5);
//calcOpticalFlowPyrLK( pre_grey, grey, winSize, velx, vely );
//cvAbsDiff( grey, prev_grey, abs_img );
//cvThreshold( abs_img, abs_img, 29, 255, CV_THRESH_BINARY);
}
int main( int argc, char** argv )
{
IplImage* frame;
IplImage* preImage, *resultImage;
char* videoFileName = "D:/FarnebackInGPU/myProject/myProject/data/car/MAH00154.MP4";
char* imageWinName = "orignalImage";
char* effectWinName = "effectImage";
g_capture = cvCreateFileCapture( videoFileName );
if (NULL == g_capture)
{
printf("Load vedio %s failed!\n", videoFileName);
return 0;
}
cvNamedWindow(imageWinName, CV_WINDOW_AUTOSIZE);
cvNamedWindow(effectWinName, CV_WINDOW_AUTOSIZE);
frames = (int) cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT
);
int tmpw = (int) cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_WIDTH
);
int tmph = (int) cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_HEIGHT
);
printf("frames %d w %d h %d\n",frames,tmpw,tmph);
cvCreateTrackbar(
"Position",
imageWinName,
&g_slider_position,
frames - 1,
onTrackbarSlide
);
double* opticalflow = new double[tmph * tmpw];
resultImage = cvCreateImage(CvSize(tmpw, tmph), 8, 1);
frames = 0;
while(1) {
frame = cvQueryFrame( g_capture );
if( !frame ) break;
cvShowImage(imageWinName, frame); //显示当前帧
//看是否有先前帧,当有了第一帧后,判断是否有先前帧就是判断
//上一帧和当前帧的帧号是否连续的了,即lastFrame + 1 == frames?
if (isPreImage && lastFrame + 1 == frames)
{
printf("frames:%d, opticalflow\n", frames);
printf("compute opticalflow and show flow image\n");
calculate(preImage, frame, resultImage, opticalflow);
cvShowImage(effectWinName, resultImage);
}
else
{
//表明没有先前帧
}
preImage = cvCloneImage(frame); //preImage为先前帧
printf("\nFrame number=%d\n",frames);
cvSetTrackbarPos("Position",imageWinName,frames);
isPreImage = true; //表明已经有了第一帧
lastFrame = frames;
frames++;
char c = (char)cvWaitKey(2000);
if( c == 27 ) break;
}
delete[] opticalflow;
cvReleaseCapture( &g_capture );
cvDestroyWindow( imageWinName );
cvDestroyWindow(effectWinName);
return(0);
}
opencv视频播放和进度条
最新推荐文章于 2022-10-03 23:14:33 发布