OpenCV2.4.5 configuration process in Ubuntu 13.04

Source: Internet
Author: User

OpenCV 2.4.5 compilation and Installation Method

X264, ffmpeg, and opencv2.4.5 are required.

X264: ftp://ftp.videolan.org/pub/x264/snapshots/
Ffmpeg: http://www.ffmpeg.org/download.html
Opencv: http://opencv.org/
 

We use the latest Ubuntu 13.04 operating system, so we use a later version of ffmpeg. X264 adopts version 20130503, ffmpeg adopts version 1.2.1, and opencv adopts version 2.4.5.

Ffmpeg is vital to us. ffmpeg is used to decode images while reading Input and Output AVI videos and display them. It is our graphic interface. X264 is used as a supplement, so we have installed it. OpenCV2.4.5 has many new features. The latest feature is the further support for graphics cards, which we cannot use, and configuration is also more troublesome. Therefore, I chose to ignore them directly. For example, the ocl module has not been compiled, for example, cvconfig. the h header file cannot be found. These problems do not affect the compilation and running of opencv programs, especially for our applications. However, for the optical flow method, I found that the sample code contains three folders: c, cpp, and gpu. The three folders contain the demo of the optical flow method optical flow, which is of great research significance. Especially in the gpu folder, there may be some good things on the gpu. If you have a chance, you can look at them.
 

Start preparation. Because it is ubuntu, it is more convenient to install software. Use apt-get to install all the packages that can be installed.
Sudo apt-get-y install autoconf automake build-essential git libass-dev libfaac-dev libgpac-dev \
Libmp3lame-dev libopus-dev libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev \
Libvorbis-dev libvpx-dev libx11-dev libxext-dev libxfixes-dev pkg-config texi2html yasm zlib1g-dev

After downloading the three files, create a new folder and name it. I will use build_sources.
Compile x264 first,
Tar xvf x264-snapshot-20130503-2245-stable.tar.bz2
Cd x264-snapshot-20130503-2245-stable
./Configure -- enable-shared -- enable-static
Make
Sudo make install
 

Note: x264 is statically called by ffmpeg. Therefore, we can place the compiled file in the system repository. Therefore, we can use cp libx264.a/usr/lib \ cp x264.h/usr/include instead of make install.
Reference link:
 

Then compile ffmpeg
Refer to the example/
 

Ffmpeg adopts the practice in the post.
Tar xvf ffmpeg-1.2.1.tar.bz2
Cd ffmpeg-1.2.1
. /Configure -- enable-gpl -- enable-version3 -- enable-nonfree -- enable-postproc -- enable-libfaac -- enable-libopencore-amrnb -- enable-libopencore-amrwb -- enable-libtheora -- enable -- enable-libxvid -- enable-x11grab -- enable-swscale -- enable-shared
Make
Sudo make install
 

In the./configure parameter type, if the -- enable option does not specify the installation path, it is usually installed in/usr/local. For more information, see config. mak in the same directory. Because ffmpeg will compile many libraries, especially libavformat. so, libavcodec. so, libavutil. so, libavfilter. so, make install will copy them to the/usr/local directory, these libraries will be used for links, so ldconfig operations are required.
Vim/etc/ld. so. conf. You can see that the link options are all in the ld. so. conf. d/folder. We can see that there is a file in the ld. so. conf. d folder: libc. conf
Vim/etc/ld. so. conf. d/libc. conf
# Libc default configuration
/Usr/local/lib
It can be seen that the link has been included. We only need to execute the sudo ldconfig command in any location.
 

To check whether ffmpeg is successfully installed, run the example in the ffmpeg installation directory.
Cd/usr/local/share/ffmpeg/examples/
Sudo make
One example is demuxing, which separates the shadows and sounds of a video. Looking for a standard avi image, I use the Optical Flow Input Image, optical_flow_input.avi
Sudo./demuxing optical_flow_input.avi video. avi audio
The first parameter is the input avi video, the second parameter is the output audio, and the third parameter is the output audio.
Output is printed after execution.
Demuxing succeeded.
Play the output video file with the command:
Ffplay-f rawvideo-pix_fmt rgb24-video_size 720x480 video. avi
We can execute the last sentence to output ffplay-f rawvideo-pix_fmt rgb24-video_size 720x480 video. avi to see the image.
This indicates that ffmpeg is successfully installed.
 

Finally, compile openCV
To compile opencv, use cmake. Therefore, ubuntu must first install
Sudo apt-get install cmake
Tar xvf opencv-2.4.5.tar.gz
Cd opencv-2.4.5
Mkdir build
Cd build
Cmake-D WITH_TBB = ON-D BUILD_NEW_PYTHON_SUPPORT = ON-D WITH_V4L = ON-D INSTALL_C_EXAMPLES = ON-D events = ON-D BUILD_EXAMPLES = ON-D WITH_QT = ON-D WITH_OPENGL = ON ..
 

Note that mkdir build is required, which is determined by cmake.
There are some important items in the cmake parameters. v4l is a library that can be used to collect images through cameras in linux. Others may be available, depending on our needs.
Make
Sudo make install
Sudo vim/etc/ld. so. conf. d/opencv. conf
Input/usr/local/lib
After saving, sudo ldconfig
Then sudo vim/etc/bash. bashrc
Last added
PKG_CONFIG_PATH = $ PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
Export PKG_CONFIG_PATH
After saving the file, you need to restart it. It cannot be used if it is not restarted.
 

How to test whether the configuration is correct after restart.
Cd/usr/local/share/OpenCV/samples
Cd c
Sudo chmod + x build_all.sh
Sudo./build_all.sh
In this case
/Tmp/ccLJWE0c. o: In function 'cvround': opencv. c :(. text + 0x19 ):
Undefined reference to 'lrint'/tmp/ccLJWE0c. o: In function
'Cvdecrefdata': opencv. c :(. text + 0xa5c): undefined reference
'Cvfree _ 'opencv. c :(. text + 0 xacd): undefined reference to 'cvfree _'
/Tmp/ccLJWE0c. o: In function 'cvgetrow': opencv. c :(. text + 0xbc3 ):
Undefined reference to 'cvgetrows '/tmp/ccLJWE0c. o: In function
'Cvgetcol': opencv. c :(. text + 0 xbee): undefined reference to 'cvgetcols'
/Tmp/ccLJWE0c. o: In function 'cvreleasematnd': opencv. c :(. text + 0xc01 ):
Undefined reference to 'cvreleasemat'/tmp/ccLJWE0c. o: In function
According to stackoverflow posts
Http://stackoverflow.com/questions/14676018/install-opencv-and-compile-c-program-using-gcc
Solution:
Try adding-lm, to include the math library that provides lrint
Add the-lm option after c and modify build_all.sh.
The modified content is
#! /Bin/sh
 

If [$ #-gt 0]; then
Base = 'basename $1. c'
Echo "compiling $ base"
Gcc-ggdb 'pkg-config opencv -- cflags -- libs' $ base. c-o $ base
Else
For I in *. c; do
Echo "compiling $ I"
Gcc-ggdb 'pkg-config -- cflags opencv '-o 'basename $ I. c' $ I 'pkg-config -- libs opencv'-lm;
Done
For I in *. cpp; do
Echo "compiling $ I"
G ++-ggdb 'pkg-config -- cflags opencv '-o 'basename $ I. cpp' $ I 'pkg-config -- libs opencv ';
Done
Fi
Then, compile the c file and there will be no errors.
This post mentions many useful things.
 

1. Reasons for adding the-lm Option
Http://man7.org/linux/man-pages/man3/lrint.3.html
2. Knowledge about the linker
Http://www.lurklurk.org/linkers/linkers.html
Http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
Http://gcc.gnu.org/onlinedocs/cpp/Invocation.html#Invocation
3. Knowledge about shared libraries
Http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
4 pkg-config knowledge
Http://www.freedesktop.org/wiki/Software/pkg-config/
5. Some extensions of bash shell
Http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html
 

The person who answered this question is really a dictionary.
After the code in the c folder is compiled, there is no error. Execute./UI and you can see the graphic interface. The typical opencv example
If you have a camera and execute./fback_c, you may be able to collect images. This is what my computer can do, but it cannot be guaranteed.

Copy build_all.sh to the cpp folder and the gpu folder. There is no problem under cpp, that is, cvconfig is prompted under gpu. h cannot be found. I have not solved this problem, but it does not affect the execution of Stanford's optical flow source code. Now the opencv2.4.5 configuration is complete.
One thing is: every time you need to pursue such a new version, 2.2 to 2.3.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.