TWL's Algorithms

The video in example 2 of OReilly Learning OpenCV refuses to be played after I have compiled the code successfully. This is in spite of me installing the latest versions of both FFMPEG and OpenCV. Hence, I retried by installing the old versions of FFMPEG and OpenCV, and I managed to get the video to play finally!

Here are the steps to get OpenCV playing a video using a fresh installation of CentOS 6.5.

Get the required packages

su
yum -y update
yum groupinstall "Development Tools"
yum install gtk+-devel gimp-devel gimp-devel-tools gimp-help-browser libjpeg-devel gstreamer-devel libavc1394-devel libraw1394-devel libdc1394-devel

Installing a Desktop and VNC

yum groupinstall "Desktop" "Desktop Platforms" "Fonts" "X Window System"
yum install tigervnc tigervnc-server
useradd vncuser
passwd vncuser
vim /etc/sysconfig/vncservers

Append the following lines to /etc/sysconfig/vncservers:
VNCSERVERS="1:vncuser"
VNCSERVERARGS[1]="-geometry 1280x900"

Go back to command line complete configuration.
su vncuser
vncpasswd
exit
service vncserver start
chkconfig vncserver on

Download putty and VNCViewer.
Under putty, on its left, go to Connection > SSH > Tunnels.
Fill in
Source Port: 9000
Destination: localhost:5901
and click Add.
Go back to Session and click Open.
Login with your vnc username and password.
Open VNC Viewer and fill in
Server: localhost:9000
and click OK.
Fill in the Password field with the password set through vncpasswd
At the login window inside VNC, use the password set through passwd for your vnc user.

Change directory to /opt where you will install FFMPEG and OpenCV2.0.0
cd /opt

Installing FFMPEG

wget http://www.ffmpeg.org/releases/ffmpeg-0.5.1.tar.gz
tar -zxvf ffmpeg-0.5.1.tar.gz
cd ffmpeg-0.5.1
./configure --prefix=/usr/local --enable-gpl --enable-swscale --enable-shared --enable-postproc --enable-avfilter-lavf
make all
make install all

Find the folder of avcodec.h. If its at /usr/local/include/libavcodec, change directory to /usr/local/include

updatedb
locate avcodec.h
cd /usr/local/include
ln -s libavcodec/avcodec.h avcodec.h
ln -s libavformat/avformat.h avformat.h
ln -s libavformat/avio.h avio.h
ln -s libavutil/avutil.h avutil.h
ln -s libswscale/swscale.h swscale.h

Installing OpenCV
cd /opt
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.0/OpenCV-2.0.0.tar.bz2
tar -jxvf OpenCV-2.0.0.tar.bz2
cd OpenCV-2.0.0
./configure --enable-shared
make
make install

Become a normal user to input the paths.
exit
echo 'export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH' >> ~/.bash_profile
. ~/.bash_profile
echo $LD_LIBRARY_PATH

You should see /usr/local/lib/:

Save the following contents to video.c
#include "highgui.h"
int main( int argc, char** argv ) {
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( argv[1] );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
}

Try to compile with the command:
gcc -I/usr/local/include/opencv -L/usr/local/lib -lhighgui -lcv -lcxcore -lml -lcvaux video.c -o video

Upload an avi file to your CentOS, and try to play it with the command ./video animation.avi, assuming that your video is named animation.avi.

Leave a comment