织梦CMS - 轻松建站从此开始!

罗索

在ubuntu上安装OpenCV 2.1

落鹤生 发布于 2012-03-01 09:13 点击:次 
Installing OpenCV 2.1 in Ubuntu
TAG:

OpenCV is an excellent library for Computer Vision. I have been using it for years and it helped me a lot during my master thesis.

OpenCV 1.0 can be easily installed in Ubuntu via the repositories. You can install OpenCV 2.0 by following one of my previous posts http://www.samontab.com/web/2010/03/installing-opencv-2-0-in-ubuntu/

Unfortunately, the newer version of OpenCV, 2.1, which was released on April has a slightly different installation procedure. Since it contains many bug fixes and some nice new additions, I will show you how to install it.

UPDATE: Install OpenCV 2.2 in Ubuntu 11.04 with Python and TBB support here.

Here are the steps that I used to successfully install OpenCV 2.1 in Ubuntu 9.10. I have used this procedure for previous versions of Ubuntu as well with minor modifications (if any).

First, you need to install many dependencies, such as support for reading and writing jpg files, movies, etc… This step is very easy, you only need to write the following command in the Terminal

  1. sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev
  2.  libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev 

The next step is to get the OpenCV 2.1 code:

  1. cd ~ 
  2. wget http://sourceforge.net/projects/opencvlibrary/files
  3. /opencv-unix/2.1/OpenCV-2.1.0.tar.bz2/download 
  4. tar -xvf OpenCV-2.1.0.tar.bz2 
  5. cd OpenCV-2.1.0/ 

In this version of OpenCV, the configure utility has been removed. Therefore you need to use Cmake to generate the makefile. Just execute the following line at the console. 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.

  1. cmake . 

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.

configuring opencv2.1

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

  1. make 
  2. sudo make install 

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

  1. sudo 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:

  1. sudo ldconfig 

Now you have to open another file:

  1. sudo gedit /etc/bash.bashrc 

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

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

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

Now you have OpenCV 2.1 installed in your computer.

Let’s check some demos included in OpenCV:

  1. cd ~ 
  2. mkdir openCV_samples 
  3. cp OpenCV-2.1.0/samples/c/* openCV_samples 
  4. cd openCV_samples/ 
  5. chmod +x build_all.sh 
  6. ./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
  2.  --cascade="/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"
  3.  --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.

In OpenCV 2.1, the grabcut algorithm is provided in the samples. 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. ./grabcut 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. ./bgfg_segm tree.avi 

There are many other samples that you can try.

(Sebastian)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://dev.open365.com.cn/a/201203/15774.html]
本文出处:samontab.com 作者:Sebastian 原文
顶一下
(2)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容