Installing OpenCV 2.2 in Ubuntu 11.04

本文详细介绍了如何在Ubuntu 11.04系统上安装OpenCV 2.2,包括依赖安装、获取并编译FFmpeg源代码、下载并编译OpenCV源代码、配置OpenCV、设置环境变量、安装OpenCV等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/


First, you need to install many dependencies, such as support for reading and writing image files, drawing on the screen, some needed tools, etc… This step is very easy, you only need to write the following command in the Terminal

1sudo apt-get install build-essential libgtk2.0-dev libjpeg62-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy libtbb-dev libeigen2-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev

Now we need to get and compile the ffmpeg source code so that video files work properly with OpenCV. This section is partially based on the method discussed here.

1cd ~
3tar -xvzf ffmpeg-0.7-rc1.tar.gz
4cd ffmpeg-0.7-rc1
5./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libxvid --enable-x11grab --enable-swscale --enable-shared
6make
7sudo make install

The next step is to get the OpenCV 2.2 code:

1cd ~
3tar -xvf OpenCV-2.2.0.tar.bz2
4cd OpenCV-2.2.0/

Now we have to generate the Makefile by using cmake. In here we can define which parts of OpenCV we want to compile. Since we want to use Python and TBB with OpenCV, here is where we set that. Just execute the following line at the console to create the appropriate Makefile. Note that there is a dot at the end of the line, it is an argument for the cmake program and it means current directory.

1cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON .

Check that the above command produces no error and that in particular it reports FFMPEG as 1. If this is not the case you will not be able to read or write videos. Also, check that Python reports ON and Python numpy reports YES. Also, check that under Use TBB it says YES. If anything is wrong, go back, correct the errors by maybe installing extra packages and then run cmake again. You should see something similar to this:


Now, you are ready to compile and install OpenCV 2.2:

1make
2sudo make install

这里MAKE时可能会报如下错:

/root/OpenCV-2.2.0/modules/core/include/opencv2/core/core.hpp:354:13: error: ‘ptrdiff_t’ does not name a type


解决办法:  

在文件 /root/OpenCV-2.2.0/modules/core/include/opencv2/core/core.hpp 头包含 using std::ptrdiff_t;.




Now you have to configure OpenCV. First, open the opencv.conf file with the following code:

1sudo gedit /etc/ld.so.conf.d/opencv.conf

Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:

1/usr/local/lib

Run the following code to configure the library:

1sudo ldconfig

Now you have to open another file:

1sudo gedit /etc/bash.bashrc

Add these two lines at the end of the file and save it:

1PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
2export PKG_CONFIG_PATH

Finally, close the console and open a new one, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.

There is a final step to configure Python with OpenCV. You need to copy the file cv.so into the correct place. You can do that by just executing the following command:

1sudo cp /usr/local/lib/python2.7/site-packages/cv.so /usr/local/lib/python2.7/dist-packages/cv.so

Now you have OpenCV 2.2 installed in your computer with Python and TBB support.

Let’s check some demos included in OpenCV.
First, let’s see some C demos:

1cd ~/OpenCV-2.2.0/samples/c
2chmod +x build_all.sh
3./build_all.sh

Some of the training data for object detection is stored in /usr/local/share/opencv/haarcascades. You need to tell OpenCV which training data to use. I will use one of the frontal face detectors available. Let’s find a face:

1./facedetect --cascade="/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5 lena.jpg

Note the scale parameter. It allows you to increase or decrease the size of the smallest object found in the image (faces in this case). Smaller numbers allows OpenCV to find smaller faces, which may lead to increasing the number of false detections. Also, the computation time needed gets larger when searching for smaller objects.

You can also detect smaller objects that are inside larger ones. For example you can search for eyes inside any detected face. You can do that with the nested-cascade parameter:

1./facedetect --cascade="/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade="/usr/local/share/opencv/haarcascades/haarcascade_eye.xml" --scale=1.5 lena.jpg


Feel free to experiment with other features like mouth or nose for example using the corresponding cascades provided in the haarcascades directory.

Now let’s check some C++ demos:

1cd ~/OpenCV-2.2.0/samples/cpp
2make

Now all the C++ demos are built in ~/OpenCV-2.2.0/bin. Let’s see a couple of them. For example, a simulated chessboard calibration:

1~/OpenCV-2.2.0/bin/calibration_artificial


In OpenCV 2.2, the grabcut algorithm is provided as a C++ sample. This is a very nice segmentation algorithm that needs very little user input to segment the objects in the image. For using the demo, you need to select a rectangle of the area you want to segment. Then, hold the Control key and left click to select the background (in Blue). After that, hold the Shift key and left click to select the foreground (in Red). Then press the n key to generate the segmentation. You can press n again to continue to the next iteration of the algorithm.

1~/OpenCV-2.2.0/bin/grabcut ~/OpenCV-2.2.0/samples/cpp/lena.jpg

This image shows the initial rectangle for defining the object that I want to segment.

Now I roughly set the foreground (red) and background (blue).

When you are ready, press the n key to run the grabcut algorithm. This image shows the result of the first iteration of the algorithm.

Now let’s see some background subtraction from a video. The original video shows a hand moving in front of some trees. OpenCV allows you to separate the foreground (hand) from the background (trees).

1~/OpenCV-2.2.0/bin/bgfg_segm ~/OpenCV-2.2.0/samples/c/tree.avi

And finally, let’s see Python working with OpenCV:

1cd ~/OpenCV-2.2.0/samples/python/

Let’s run the kmeans.py example. This script starts with randomly generated 2D points and then uses a clustering method called k-means. Each cluster is presented in a different color.

1python kmeans.py

Now let’s see the convexhull.py demo. This algorithm basically calculates the smallest convex polygon that encompasses the data points.

1python convexhull.py

Python scripts can also be executed directly like the following example. This script reads a video file (../c/tree.avi) within pyhton and shows the first frame on screen:

1./minidemo.py


Have fun with OpenCV in C, C++ or Python…


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值