首頁 > 軟體

Ubuntu 安裝 OpenCV 2.4.9

2020-06-16 17:26:15

OpenCV即 Open Source Computer Vision Library.主要是提供影象處理和視訊處理的基礎演算法庫,還涉及一些機器學習的演算法。比如你想實現視訊的降噪、運動物體的跟蹤、目標(比如人臉)的識別這些都是CV的領域.

安裝的方法可以參見github上的git安裝指令碼.

如果不想取github上看,直接複製儲存下面的程式碼成opencv2_4_9.sh檔案.當然,通過下面的指令碼,安裝的版本就是2_4_9,只適用於Ubuntu系的Linux.其他版本或者Linux的安裝指令碼在上面的github上都有。

arch=$(uname -m)
if [ "$arch" == "i686" -o "$arch" == "i386" -o "$arch" == "i486" -o "$arch" == "i586" ]; then
flag=1
else
flag=0
fi
echo "Installing OpenCV 2.4.9"
mkdir OpenCV
cd OpenCV
echo "Removing any pre-installed ffmpeg and x264"
sudo apt-get -y remove ffmpeg x264 libx264-dev
echo "Installing Dependenices"
sudo apt-get -y install libopencv-dev
sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm
sudo apt-get -y install libtiff4-dev libjpeg-dev libjasper-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev
sudo apt-get -y install python-dev python-numpy
sudo apt-get -y install libtbb-dev
sudo apt-get -y install libqt4-dev libgtk2.0-dev
sudo apt-get -y install libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev
sudo apt-get -y install x264 v4l-utils ffmpeg
# sudo apt-get -y install libgtk2.0-dev # duplicate
echo "Downloading OpenCV 2.4.9"
wget -O OpenCV-2.4.9.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip/download
echo "Installing OpenCV 2.4.9"
unzip OpenCV-2.4.9.zip
cd opencv-2.4.9
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -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 ..
make -j4
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
cd ../../..
rm -rf OpenCV
echo "OpenCV 2.4.9 ready to be used"

然後,通過下面兩種方式執行即可.

chmod +x opencv.sh
./opencv.sh

或者

sudo sh opencv.sh

會在指令碼所在的目錄新建一個OpenCV的目錄,然後將OpenCV的安裝相關檔案下載到該目錄下.指令碼會執行一段時間,耐心等待執行完成,我的時間大約10分鐘左右.因為要下載和編譯一些程式碼.


看到上面,就說明安裝完成了.安裝完成後,上面的OpenCV目錄會被刪除.
通過,下面的命令可以檢視安裝的OpenCV的版本.

pkg-config --modversion opencv

測試安裝

C語言版本test.c,開啟一張圖片.

#include<opencv2/highgui/highgui.hpp>

int main()
{
    IplImage* img = cvLoadImage("/home/linuxidc/圖片/牆紙.jpg",CV_LOAD_IMAGE_COLOR);
    cvNamedWindow("opencvtest",CV_WINDOW_AUTOSIZE);
    cvShowImage("opencvtest",img);
    cvWaitKey(0);
    cvReleaseImage(&img);
    return 0;
}

編譯.

gcc -ggdb `pkg-config --cflags opencv` -o `basename test.c .c` test.c `pkg-config --libs opencv`

執行.

./test

再放一個Python版本的.test.py

#!/usr/bin/python
# -*- coding: utf-8 -*-  
from cv2.cv import *
img=LoadImage("//home/linuxidc/圖片/牆紙.jpg")
NamedWindow("opencv")
ShowImage("opencv",img)
WaitKey(0)

這樣,基本環境就安裝完成了.

OpenCV官方教學中文版(For Python) PDF  http://www.linuxidc.com/Linux/2015-08/121400.htm

Ubuntu 14.04安裝OpenCV2.4.9  http://www.linuxidc.com/Linux/2016-07/132884.htm

Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 16.04上用CMake圖形介面交叉編譯樹莓派的OpenCV3.0 http://www.linuxidc.com/Linux/2016-10/135914.htm

Ubuntu 16.04中安裝OpenCV 2.4.11  http://www.linuxidc.com/Linux/2016-07/132882.htm

Ubuntu 16.04下Matlab2014a+Anaconda2+OpenCV3.1+Caffe安裝  http://www.linuxidc.com/Linux/2016-07/132860.htm


IT145.com E-mail:sddin#qq.com