Linux下OpenCV的環境搭建

來源:互聯網
上載者:User

標籤:

OpenCV is the most popular and advanced code library for Computer Vision related applications today, spanning from many very basic tasks (capture and pre-processing of image data) to high-level algorithms (feature extraction, motion tracking, machine learning). It is free software and provides a rich API in C, C++, Java and Python. Other wrappers are available. The library itself is platform-independent and often used for real-time image processing and computer vision.

1.移除先前安裝的ffmpeg和x264

1 sudo apt-get -qq remove ffmpeg x264 libx264-dev

 

2.安裝依賴項

1 sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg cmake qt5-default checkinstall

 

3.下載並解壓OpenCV

1 wget -O OpenCV-$version.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download2 unzip OpenCV-$version.zip3 cd opencv-$version

 

4.編譯源碼,構建OpenCV

1 mkdir build2 cd build3 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 ..4 make -j25 sudo make install6 sudo sh -c ‘echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf‘7 sudo ldconfig

 

5.測試OpenCV是否安裝成功

轉到opencv的範例代碼目錄(samples/c)下,

1 chmod +x build_all.sh2 ./build_all.sh3 ./facedetect "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" lena.jpg

 運行結果:

 

編譯OpenCV程式:

 1 #!/bin/bash 2 echo "compiling $1" 3 if [[ $1 == *.c ]] 4 then 5     gcc -ggdb `pkg-config --cflags opencv` -o `basename $1 .c` $1 `pkg-config --libs opencv`; 6 elif [[ $1 == *.cpp ]] 7 then 8     g++ -ggdb `pkg-config --cflags opencv` -o `basename $1 .cpp` $1 `pkg-config --libs opencv`; 9 else10     echo "Please compile only .c or .cpp files"11 fi12 echo "Output file => ${1%.*}"

 將上面代碼儲存成shell指令碼,在每次編譯器時調用即可。

如果在編譯過程中出現如下錯誤:

1 [ 14%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o2   /home/test/autobuild/instance-0/output/build/opencv-2.4.10/modules/core/src/system.cpp: In function ‘(static initializers for /home/test/autobuild/instance-0/output/build/opencv-2.4.10/modules/core/src/system.cpp)‘:3   /home/test/autobuild/instance-0/output/build/opencv-2.4.10/modules/core/src/system.cpp:280:10: error: inconsistent operand constraints in an ‘asm‘4   make[3]: *** [modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o] Error 1

可參考:https://github.com/Itseez/opencv/commit/ea50be0529c248961e1b66293f8a9e4b807294a6

 

Linux下OpenCV的環境搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.