Copyright Notice: All original articles in this blog can be used for personal, educational, or non-commercial purposes, however, it is important to ensure the integrity of the article and to indicate the original author, source and this statement in the form of a hyperlink without any modification.
Blog address: Http://blog.csdn.net/shuxiao9058
Original Author: Ji ya
All those who have read my blog know that I am still favored by the Red Hat system. Currently, Linux 6.2 and fedora 15 are commonly used, after all, the red hat is so stable and convenient, but the resources are not easy to find. When compiling the second video example of learning opencv, I cannot run it. I guess it may be a decoder problem. I heard from my friends that Ubuntu is more convenient, So I downloaded and installed it, and then re-compile opencv. It turns out that there is no problem at all.
Also, in Linux, the source code is compiled successfully only in Ubuntu 11.10/12.04 and RHEL 6.2, and there is no problem, but it is difficult to play the video in RHEL, unlike Ubuntu, you can directly update the plug-in. Note: This article is a OpenCV-2.3.1 to introduce the compilation configuration method, the opencv version of the local needs to be modified.
First, make sure that GTK + 2.xhas been installed in Ubuntu (you can run the "PKG-config -- modversion GTK +-2.0" command to view GTK + 2. version X, as shown in Figure 1) or carbon. Otherwise, the program may not run properly (as shown in Figure 2. The information shown here is after the compilation of ubuntu 11.10, error message displayed when running the program [install GTK + 2. after X, the program runs normally], but below is what I compiled and configured under Ubuntu 12.04 ). Figure 1 no installation of GTK + 2.x Figure 2 program running error (because GTK + 2.xis not installed when compiling opencv)
It can be seen that GTK + 2.xis not installed on my computer, so now we need to install GTK + 2.x. to install the GTK environment, we only need to install a gnome-core-devel, which integrates many other packages.
Run the following command to configure the network before installing the software. For more information about network configuration under the VM, see my blog: Three Network Connection Methods of VMware Workstation ):
$ sudo apt-get install gnome-core-devel
You can also use the following command to install only GTK + 2.x:
$ sudo apt-get install libgtk2.0-dev
Or
$ sudo apt-get install libgtk2.0*
Run the following command to automatically find the header file and library file location when compiling the GTK program:
$ sudo apt-get install pkg-config
Finally, run the following command to view the GTK + 2. x version:
$ pkg-config --modversion gtk+-2.0
As shown in volumes, 3, I have put these files (compressed packages) on the desktop. Figure 3 place pre-used files on the desktop
1. Install cmake
Step 1:
$ tar zxvf cmake-2.8.8.tar.gz
…
$ cd cmake-2.8.8
…
$ ./bootstrap
…
If the following error occurs after running the preceding command, run the command "sudo apt-Get install g ++" to install the C ++ compiler.
$ ./bootstrap
---------------------------------------------
CMake 2.8.8, Copyright 2000-2011 Kitware, Inc.
C compiler on this system is: cc
---------------------------------------------
Error when bootstrapping CMake:
Cannot find appropriate C++ compiler on this system.
Please specify one using environment variable CXX.
See cmake_bootstrap.log for compilers attempted.
---------------------------------------------
Log of errors: /home/jiya/Desktop/cmake-2.8.8/Bootstrap.cmk/cmake_bootstrap.log
---------------------------------------------
This operation takes a long time. Here, you can take a cup of tea to continue.
After completing step, you will see the prompt "cmake has bootstrapped. Now run make." And then execute the command:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jiya/Desktop/cmake-2.8.8
---------------------------------------------
CMake has bootstrapped. Now run make.
$
…
$ make
…
Wait a moment after the same operation is completed.
Step1.3 then run the following command to install it to the system (if you do not have the permission, use root to perform the operation. You can use the "sudo Su" command to switch to the root user ):
$ sudo make install…
Step: run the "cmake-version" command to check whether the client has any version information about cmake. If not, you need to modify the user's path and add the cmake path to the user's environment variable. Run the following command:
$ echo "export PATH=$PATH:/usr/local/bin" >> ~/.bashrc
Note: here we use the modified by the root user, so it only takes effect for the root user! If other users also need to use the cmake command, then use another user to log on to the terminal and modify the user environment variable.
You can also modify it ~ /. Bashrc file, add the above Code at the end of the file as follows.
Note that, unlike the environment variables in windows, environment variables are separated by colons (:) instead of semicolons (;). Restart the terminal to make the user variables take effect.
Test: log on to the system again (you do not need to restart the machine, start-> logout or re-open the terminal) to make the above settings take effect. Enter cmake -- version on the command line and the following information appears, this indicates that the cmake environment has been successfully configured and can be used properly.
$ cmake --version
cmake version 2.8.8
$
Ii. Compile opencv
Step2.1 return to the desktop and run the following command:
$ tar -jxvf OpenCV-2.3.1a.tar.bz2
$ cd OpenCV-2.3.1
$ mkdir release
$ cd release
$
Step 2.2 go to the release directory and execute the following command (note that this is an entire command and there are two ".. "), where" cmake_install_prefix =/home/OpenCV-2.3.1a "indicates that when we install opencv using the" make install "command, the related files will be copied to the/home/OpenCV-2.3.1a directory.
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/home/OpenCV-2.3.1a -D BUILD_PYTHON_SUPPORT=ON ..
After executing the preceding command, makefile files will be generated under the release directory, as shown in Figure 4. Files And Directories generated under the release directory
Step2.3:
$ make
Waiting for compilation ......
Step2.4 after the compilation is complete, run the "sudo make install" command, will generate opencv related directories and files in the/home/OpenCV-2.3.1a directory, including Bin, Lib, include, etc., 5. Figure 5 related directories and files generated under the/home/OpenCV-2.3.1a directory
3. Finally, let's configure the compiling environment. Here we choose QT ide
After the download is complete, copy the file to the Ubuntu Desktop. Run the following command:
$ chmod +x QtSdk-offline-linux-x86-v1.2.1.run
$ ./QtSdk-offline-linux-x86-v1.2.1.run
Next, follow the Wizard to install QT step by step (always click "Next ).
Below we configure the opencv development environment in QT:
Step3.2 modify the opencv. PC file (this step is not required)
Suppose we want to put opencv-related files under the/home/OpenCV-2.3.1a directory (using "CP/home/OpenCV-2.3.1a/opt-R"), under the/opt/OpenCV-2.3.1a directory, in this case, we need to modify an object. The file is located in the "lib/pkgconfig" directory, that is, "lib/pkgconfig/opencv. PC file, open this file with gedit, and change "prefix =/opt/OpenCV-2.3.1a" to "prefix =/opt/OpenCV-2.3.1a" to save it (6 ). Figure 6 Modify/opt/OpenCV-2.3.1a/lib/pkgconfig/opencv. PC file and save
Then, convert opencv. copy the file from the PC to the/usr/share/pkgconfig directory. You can use "PKG-config opencv -- cflags -- Libs" to test opencv. whether the PC configuration takes effect (7 ). 7. test whether the opencv. PC configuration takes effect.
Step use the following command to modify the/etc/lD. So. conf file and add the opencv Link Library. (If you do not have the permission, use root to perform the operation. You can use the "sudo Su" command to switch to the root user ).
$ echo "/opt/OpenCV-2.3.1a/lib " >> /etc/ld.so.conf
Step create a project using qt ide, select "new project-other projects-empty QT Project", click "select", browse to the specified directory, and click "Next.
Add the following content to the project file (*. Pro, if the name of my new project is opencv, You need to modify opencv. Pro:
INCLUDEPATH += /opt/OpenCV-2.3.1a/include/LIBS += `pkg-config opencv --cflags --libs`
Here, "libs + = 'pkg-config opencv -- cflags -- libs'" can also be used in the following form:
LIBS += /opt/OpenCV-2.3.1a/lib/libopencv_calib3d.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_contrib.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_core.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_features2d.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_flann.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_gpu.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_highgui.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_imgproc.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_legacy.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_ml.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_objdetect.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_ts.so.2.3.1 \
/opt/OpenCV-2.3.1a/lib/libopencv_video.so.2.3.1 \
Create a source file (*. CPP. For example, if the file name of the newly created source file is opencv. CPP), write code, Build-build all projects (CTRL + Shift + B), build-run (CTRL + r.
Test example (source code ):
#include <opencv/highgui.h>
int main( int argc, char** argv ) {
IplImage* img = cvLoadImage( argv[1] );
cvNamedWindow( "OpenCV", CV_WINDOW_AUTOSIZE );
cvShowImage( "OpenCV", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "OpenCV" );
return ( 0 );
}
After compilation, run./opencv Lena \ (1 \). Tiff in the directory where the executable file is located. effect 8 is shown in figure 8.
Note: If you use functions such as cvcapturefromfile () and cvcapturefromavi in Linux, the problem is caused by the decoder. We only need to install the FFMPEG decoder. the "-enable-shared" parameter must be added to the/configure command. Otherwise, the FFMPEG library cannot be found in opencv. For example, the command used by myself is ". /configure -- enable-shared -- enable-GPL "). You can also open any video file in Ubuntu (or in the Ubuntu Software Center) and install gstreamer plug-ins. I would like to explain the specific method. I will not repeat it here. The video playback effect of opencv in Ubuntu is shown in Figure 7. The video playback effect of opencv in Ubuntu is as follows:
The related code is as follows:
#include <opencv/cv.h>
#include <opencv/highgui.h>
int
main( int argc, char **argv )
{
cvNamedWindow( "Video", CV_WINDOW_FULLSCREEN );
CvCapture *capture = cvCreateFileCapture( argv[ 1 ] );
IplImage *frame;
while (1) {
frame = cvQueryFrame( capture );
if ( !frame )
break;
cvShowImage( "Video", frame );
char c = cvWaitKey( 33 );
if ( c == 27 )
break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Video" );
return ( 0 );
}
Appendix:
1. How to open the terminal in Ubuntu:
Method 1. (Of course, it is a stupid method), open (Application-attachment-terminal) in turn, if you are idle, you can directly add it to the Panel, this is also convenient.
Method 2. press Alt first, then press F2, and a running box is displayed. Enter gnome-terminal to run the command. In this dialog box, you can execute a lot of commands to [run] (Windows + r key) windows, but it is hard to remember.
Method 3. You can right-click the terminal window and right-click it in the blank area of the desktop or folder. The shortcut "open from terminal" appears. That requires a software. The installation is simple. Open the terminal and enter the following command in it:
$ sudo apt-get install nautilus-open-terminal
Log out and you will see it!
2. If the software cannot be updated or installed, run the following command:
$ sudo dpkg --configure -a
Reference link:
Http://opencv-extension-library.googlecode.com/svn/trunk/QtOpenCV/
Http://code.google.com/p/qt-opencv-multithreaded/wiki/Documentation
Http://qt.nokia.com/
Http://ffmpeg.org/
Http://www.cnblogs.com/niocai/archive/2011/07/15/2107472.html
Http://blog.163.com/kingore@126/blog/static/8540871920113382233164/
Attachment download:
Ubuntu system CD:
Http://115.com/file/an0mpuer# ubuntu-11.10-desktop-i386.iso
Http://115.com/file/e78j6ym2# ubuntu-12.04-desktop-i386.iso
Compiled files:
Http://115.com/file/an0m24z1# QtSdk-offline-linux-x86-v1.2.1.run
Http://115.com/file/c2ap6pm8# opencv-2.3.1a(ubuntu-11.102.16.tar.bz2
Http://115.com/file/e78jirjm# opencv-2.3.1a(ubuntu-12.042.16.tar.bz2
Http://115.com/file/e78bxqn2# opencv-2.3.1a(rhel-6.22.16.tar.gz
Http://115.com/file/e7jwbi6p# opencv-2.4.0(ubuntu-12.042.16.tar.bz2
Source code:
Http://115.com/file/becmjqxa# OpenCV-2.3.1a.tar.bz2
Http://115.com/file/anc42qnb# OpenCV-2.4.0.tar.bz2
Http://115.com/file/becmju89# cmake-2.8.8.tar.gz