Ubuntu 安装 opencv 2.0

Installing OpenCV 2.0 in Ubuntu

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. Unfortunately, the newer version of OpenCV, 2.0, which is full of new features is not yet available in the repositories of Ubuntu.

Here are the steps that I used to successfully install OpenCV 2.0 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 libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev

The next step is to get the OpenCV 2.0 code:

1 cd ~
3 tar -xvf OpenCV-2.0.0.tar.bz2
4 cd OpenCV-2.0.0

Now, you need to configure how you want to compile OpenCV 2.0. In my case I used /usr/local as the installation directory and I also activated the video support for reading and writing videos using OpenCV (via ffmpeg).

1 ./configure --prefix=/usr/local --enable-apps --enable-shared --with-ffmpeg --with-gnu-ld --with-x --without-quicktime CXXFLAGS=-fno-strict-aliasing

Check that the above command produces no error and that in particular it reports ffmpeg as yes. If this is not the case you will not be able to read or write videos.

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

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.0 installed in your computer.

Let’s check some demos included in OpenCV:

1 cd ~
2 mkdir openCV_samples
3 cp /usr/local/share/opencv/samples/c/* openCV_samples
4 cd openCV_samples/
5 chmod +x build_all.sh
6 ./build_all.sh

All 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.

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

### 卸载 OpenCV 2.0 为了在 Ubuntu 18.04 上完全卸载现有的 OpenCV 2.0 版本,可以使用以下命令来移除所有与 OpenCV 相关的库和依赖项: ```bash sudo apt-get purge libopencv* sudo apt-get autoremove ``` 这些命令会删除系统中已有的 OpenCV 库及其相关组件[^1]。 ### 下载并准备 OpenCV 和 contrib 模块 前往官方发布的页面下载所需的 OpenCVOpenCV_contrib 的压缩包。具体链接如下: - OpenCV: https://opencv.org/releases.html - OpenCV_contrib: https://github.com/opencv/opencv_contrib/releases 下载完成后,将其解压至指定位置,并将 `opencv_contrib-3.4.1` 文件夹复制到 `opencv-3.4.1` 中: ```bash unzip opencv-3.4.1.zip unzip opencv_contrib-3.4.1.zip cp -r opencv_contrib-3.4.1 opencv-3.4.1/ ``` 此操作确保了 contrib 模块能够被正确识别和编译[^2]。 ### 配置编译环境 进入 OpenCV 解压后的根目录创建一个新的构建文件夹,并初始化 CMake 工具链以完成配置工作: ```bash cd opencv-3.4.1 mkdir build && cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.4.1/modules .. ``` 上述命令中的 `-D OPENCV_EXTRA_MODULES_PATH` 参数指定了额外模块的位置,从而支持更多功能特性[^4]。 接着执行实际的编译过程以及最终安装步骤: ```bash make -j$(nproc) sudo make install sudo ldconfig ``` 最后一步通过编辑 `/etc/ld.so.conf.d/opencv.conf` 来注册新安装好的动态链接库地址,以便程序运行时能自动加载它们[^3]: ```bash echo "/usr/local/lib" | sudo tee -a /etc/ld.so.conf.d/opencv.conf sudo ldconfig ``` 验证安装成功与否可以通过 Python 或者终端输入查看版本号确认: ```python import cv2 print(cv2.__version__) ``` 如果显示为 `3.4.1` 则表明一切正常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值