ffmpeg安装
opencv安装
请提前先安装好python3,可参考 python3安装入口>>
yum安装依赖包
$ yum install cmake gcc gcc-c++ gtk+-devel gimp-devel gimp-devel-tools gimp-help-browser zlib-devel libtiff-devel libjpeg-devel libpng-devel gstreamer-devel libavc1394-devel libraw1394-devel libdc1394-devel jasper-devel jasper-utils swig Python libtool nasm
$ yum install python-devel python-nose python-setuptools gcc gcc-gfortran gcc-c++ blas-devel lapack-devel atlas-devel
$ yum -y install libjpeg libpng libtiff jasper-libs ilmbase OpenEXR-libs gtk2 gstreamer gstreamer-plugins-base
- numpy安装
$ easy_install pip
$ sudo pip install numpy==1.6.1
- opencv下载安装
$ git clone https://github.com/opencv/opencv.git
$ unzip opencv-master.zip
$ cd opencv-master
$ make dir build
$cd build
$ cmake .. \
-DBUILD_opencv_python2=OFF \
-DBUILD_opencv_python3=ON \
-DPYTHON3_LIBRARY=/usr/local/python34/lib/libpython3.4m.a \
-DPYTHON3_INCLUDE_DIR=/usr/local/python34/include/python3.4m \
-DPYTHON3_PACKAGES_PATH=/usr/local/python34/lib/python3.4/site-packages \
-DPYTHON3_EXECUTABLE=/usr/local/python34/bin/python3 \
-DPYTHON_DEFAULT_EXECUTABLE=/data/majian/software/python_zyq/bin/python
$ make
$ make install
- 导出对应的库
$ vim ~/.bashrc
添加下面两句:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
$source ~/.bashrc
错误及解决
- 错误
- 原因
2017年5月15日,centos更新了jaspe-devel包为jasper-devel.x86_64 0:1.900.1-30.el7_3(旧版本为jasper-devel-1.900.1-29.el7.x86_64),这将导致该问题。 - 解决
$ vi /usr/include/jasper/jas_math.h
在 #include <stdint.h> 后添加
# if ! defined SIZE_MAX
# define SIZE_MAX (4294967295U)
# endif
小经验
- 编译需要注意
$ gcc test.cpp -o test `pkg-config --cflags --libs opencv`
cvQueryFrame()内存问题
使用cvQueryFrame()取出CvCapture*每帧图像,只需在最后释放CvCapture*,不需要释放IplImage*。
抽取视频帧的例子
#include <stdio.h>
#include <stdlib.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <string>
#include <iostream>
#include <unistd.h>
#include <algorithm>
#include <time.h>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
#define cvQueryHistValue_2D( hist, idx0, idx1 ) cvGetReal2D( (hist)->bins, (idx0), (idx1) )
const int MAX_LINE_LEN = 1024;
void PrintUsage(const char* name){
cerr<<"===Frame Extractor==="<<endl;
cerr<<"USAGE: "<<name<<" [-f <video_file>] [-s <frame_save_dir>]"<<endl;
}
int frame_extract(const char* video_file,const char* save_dir){
time_t cur_t;
time(&cur_t);
struct tm * timeinfo;
timeinfo = localtime (&cur_t);
char buf[80];
strftime(buf,80,"%Y-%m-%d %H:%M:%S",timeinfo);
string vf_tmp = video_file;
string sd_tmp = save_dir;
size_t pos1 = vf_tmp.find_last_of("/");
size_t pos2 = vf_tmp.find_last_of(".");
if(pos2==vf_tmp.npos)
return -3;
if(pos1==vf_tmp.npos)
pos1=-1;
string prefix = vf_tmp.substr(pos1+1,pos2-pos1-1);
cerr<<buf<<"\tprocess_file:"<<vf_tmp.substr(pos1+1)<<"\t";
CvCapture* pCapture = NULL;
IplImage* pFrame=NULL;
if(!(pCapture = cvCaptureFromFile(video_file))){
fprintf(stderr, "Error: Can not open video file %s\n", video_file);
return -2;
}
int numFrames = (int) cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
int fps = (int) cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS);
float tts = numFrames*1.0/fps;
int preFrame = min(numFrames,fps*120);
int avgFrame = preFrame/10;
int frameId = 0;
int frame_num = 0;
for(int i=10; i>0; i--){
frameId = avgFrame * i;
if(frameId ==0)break;
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES,frameId);
pFrame = cvQueryFrame(pCapture);
if(pFrame==NULL)continue;
if(i>frame_num)frame_num = i;
string picName = sd_tmp+"/"+prefix
+"_"+lexical_cast<string>(numFrames)
+"_"+lexical_cast<string>(fps)
+"_"+lexical_cast<string>(frame_num)
+"_"+lexical_cast<string>(i-1)
+"_"+lexical_cast<string>(frameId)
+".jpg";
cvSaveImage(picName.c_str(),pFrame);
cout<<picName<<endl;
}
cerr<<"[===frame_number:"<<numFrames<<"\tFPS:"<<fps<<"\tduration:"<<tts<<"s\textract_frame_num:"<<frame_num<<"===]"<<endl;
cvReleaseCapture(&pCapture);
return 0;
}
int main(int argc, char** argv){
char ch;
string save_dir = "./";
string video_file = "";
while((ch = getopt(argc, argv, "f:s:h")) != -1){
switch(ch){
case 's':
save_dir = optarg;
break;
case 'f':
video_file = optarg;
break;
case 'h':
PrintUsage(argv[0]);
return 0;
default:
PrintUsage(argv[0]);
return -1;
}
}
//单个视频处理
if(video_file!=""){
frame_extract(video_file.c_str(),save_dir.c_str());
}
else{//从标准输入读入视频文件
char* buf=new char[MAX_LINE_LEN];
while (fgets(buf, MAX_LINE_LEN, stdin)){
int n = strlen(buf);
buf[n-1]='\0';
video_file=buf;
frame_extract(video_file.c_str(),save_dir.c_str());
}
delete [] buf;
}
return 0;
}
- 编译Makefile
CXX = g++
CFLAGS =`pkg-config --cflags --libs opencv`
INCLUDES = -I./
FRAME_EXTRACT=frame_extract
all : $(FRAME_EXTRACT)
frame_extract: frame_extract.cpp
g++ $(CFLAGS) -o $@ $^ -g
clean:
rm -f *.o $(FRAME_EXTRACT)