Ubuntu安装Opencv2.4.9

本文详细介绍了如何在Ubuntu 14.04 LTS系统中安装最新版本的OpenCV 2.4.9,包括更新系统、安装依赖包、下载源代码、配置、编译与安装等步骤,以及如何使用viz模块进行3D可视化、Python、Java、TBB、OpenGL、视频和Qt支持的功能。

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



(转自)http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/

  我按照配置成功安装了opencv2.4.9,是可以用的!

Installing OpenCV 2.4.9 in Ubuntu 14.04 LTS

The latest Long Term Support version of Ubuntu(14.04 LTS) is out and a new version of OpenCV was recently released as well. This means that now is a great opportunity to update my OpenCV installation guide to the latest versions, Ubuntu 14.04 LTS and OpenCV 2.4.9.

viz

One of the great additions in this new release of OpenCV is the new viz module, which offers a nice 3D visualizer. This module is based on the great Visualization Toolkit library (VTK) and nicely prepares the users for the upcoming OpenCV 3.

In this guide, I will show you how to install OpenCV with a lot of the features it provides. Here are some of the things that are going to be enabled when you are finished following through with this installation tutorial:

  • viz module (3D visualization)
  • Qt version of the HighGUI module (Better 2D window interface with zoom, image saving capabilities, etc)
  • OpenGL support
  • C++ interface and examples
  • C interface and examples
  • Python interface and examples
  • Java interface and examples
  • Intel Threading Building Blocks (TBB)

Note: I have noticed some copies of my posts elsewhere, so make sure that you are reading this from the original source, at samontab dot com, accessible from here so that you don’t miss the comments.

OK, so the first step is to make sure that everything in the system is updated and upgraded. Open the terminal and write this:

1 sudo apt-get update
2 sudo apt-get upgrade

Now, you need to install many dependencies, such as support for reading and writing image files, drawing on the screen, some needed tools, other libraries, 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 libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen3-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev default-jdk ant libvtk5-qt4-dev

Time to get the OpenCV 2.4.9 source code:

1 cd ~
3 unzip opencv-2.4.9.zip
4 cd opencv-2.4.9

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 the viz module, Python, Java, TBB, OpenGL, Qt, work with videos, etc, here is where we need to set that. Just execute the following line at the terminal to create the appropriate Makefile. Note that there are two dots at the end of the line, it is an argument for the cmake program and it means the parent directory (because we are inside the build directory, and we want to refer to the OpenCV directory, which is its parent).

1 mkdir build
2 cd build
3 cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON ..

OpenCVConfig

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. Check that Java, Python, TBB, OpenGL, V4L, OpenGL and Qt are all detected correctly.

OpenCVmodulesToBeBuilt

Make sure you scroll up and check that the modules that are going to be built are these:
core flann imgproc highgui features2d calib3d ml video legacy objdetect photo gpu ocl nonfree contrib java python stitching superres ts videostab viz.

If anything is wrong, go back, correct the errors by maybe installing extra packages and then run cmake again.

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

1 make
2 sudo make install

Now you have to configure OpenCV. 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, 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.

Now you have OpenCV 2.4.9 installed in your computer with 3D visualization, Python, Java, TBB, OpenGL, video, and Qt support.

Check out the cool Qt interface which provides image viewing capabilities with zoom, as well as the ability to save the current image with just one click.

If you zoom in enough, you can see the RGB (or intensity) values for each pixel.

Now let’s build some samples included in OpenCV:

1 cd ~/opencv-2.4.9/samples/c
2 chmod +x build_all.sh
3 ./build_all.sh
These examples use the old C interface:
1 ./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml"--scale=1.5 lena.jpg

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

The following examples use the new C++ interface:

1 ~/opencv-2.4.9/build/bin/cpp-example-grabcut ~/opencv-2.4.9/samples/cpp/lena.jpg

1 ~/opencv-2.4.9/build/bin/cpp-example-calibration_artificial

Now let’s run some Python code:

1 python ~/opencv-2.4.9/samples/python2/turing.py

Now you can build a Java sample using ant for example. (Make sure that you change/home/samontab/ with your actual home directory):

1 cd ~/opencv-2.4.9/samples/java/ant
2 ant -DocvJarDir=/home/samontab/opencv-2.4.9/build/bin -DocvLibDir=/home/samontab/opencv-2.4.9/build/lib

java_ant

Now let’s read a video and use OpenGL with Qt through this great sample that detects the features from the video, then estimates the 3D location of the structure using POSIT, and finally uses OpenGL to draw in 3D (great sample Javier):

1 cd ~/opencv-2.4.9/samples/cpp/Qt_sample
2 mkdir build
3 cd build
4 cmake ..
5 make
6 ./OpenGL_Qt_Binding

posit

And finally, let’s build a sample using the 3D visualization module viz:

1 cd ~/opencv-2.4.9/samples/cpp/tutorial_code/viz
2 g++ -o widget_pose `pkg-config opencv --cflags` widget_pose.cpp `pkg-config opencv --libs`
3 ./widget_pose

viz

As you can see, now you can use OpenCV with C++, C, Python, and Java. The Qt enhanced 2D interface is enabled, 3D data can be displayed using OpenGL directly, or using the new viz module. Multi threading functionality is enabled using TBB. Also, video support is enabled as well.

If you correctly finished this tutorial, you are now ready to do some nice computer vision work.

### 安装 OpenCV 2.4.9 的指导 #### 准备工作 在 Ubuntu 20 上安装 OpenCV 2.4.9 需要先完成一些准备工作。这包括下载源码并确保系统中有必要的开发工具和库支持。 可以使用 `wget` 命令从 SourceForge 下载 OpenCV 2.4.9 源码包[^1]: ```bash wget https://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip/download -O opencv-2.4.9.zip ``` 接着解压缩该文件到主目录下,可以看到一个名为 `opencv-2.4.9` 的文件夹: ```bash unzip opencv-2.4.9.zip -d ~/ ``` #### 安装依赖项 为了成功构建 OpenCV,需要安装一系列依赖软件包。这些依赖项可以通过 APT 包管理器来获取: 运行以下命令以安装基本的编译环境以及常用的图像处理库[^3][^5]: ```bash sudo apt-get update sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config \ python-dev python-numpy libavcodec-dev libavformat-dev libswscale-dev ``` 如果目标环境中还需要其他功能模块的支持(比如视频 I/O 或者 Python 绑定),可以根据需求扩展上述列表中的项目。 #### 编译配置 进入刚刚提取出来的 OpenCV 源代码根目录,并创建一个新的子目录用于存放生成的目标文件: ```bash cd ~/opencv-2.4.9 mkdir release && cd release ``` 在此处执行 CMake 来初始化项目的构建过程。CMake 是用来简化跨平台应用程序构建流程的一个工具,在此阶段它会检测系统的状态并将结果写入 Makefile 中以便后续操作[^4]: ```bash cmake -D CMAKE_BUILD_TYPE=RELEASE .. ``` #### 构建与安装 一旦 CMake 成功完成了它的任务,则可以直接调用 make 开始实际的编译工作: ```bash make -j$(nproc) ``` 参数 `-j$(nproc)` 表示利用当前 CPU 所有的核心数加速编译速度。 最后一步就是正式将编译后的产物部署至全局路径中去: ```bash sudo make install ``` 至此,整个安装流程结束。现在应该可以在系统范围内正常使用 OpenCV 库了。 --- ### 示例验证程序 下面给出一段简单的测试代码片段,帮助确认安装是否正确无误: ```cpp #include <cv.h> #include <highgui.h> int main() { cv::Mat image = cv::imread("test.jpg"); if (image.empty()) return -1; cv::imshow("Test Image", image); cv::waitKey(0); return 0; } ``` 保存以上内容为 `.cpp` 文件形式之后通过 g++ 加上链接选项 `${OpenCV_LIBS}` 即可顺利完成编译。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值