Ubuntu14.04 下 OpenCV3 安裝
一 按照官網安裝Opencv步驟:
1.需要的包:
GCC 4.4.x 或更高
CMake 2.8.7 或更高
Git
GTK+2.x 或更高,包括頭(libgtk2.0-dev)
pkg-config
Python 2.6 或以後,Numpy 1.5或以後,和開發包(python-dev,python-numpy)
ffmpeg 或者libav開發包:libavcodec-dev, libavformat-dev, libswscale-dev
可選包:libtbb2 ,libtbb-dev
可選包:libdc1394 2.x
可選包:libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
可以用一下程式碼進行安裝:
基本:
sudo apt-get install build-essential
必須:sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
可選:
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
解壓安裝包到你想要的地方,本文以存放主檔案加下。
cd ~/opencv-3.0.0
4. 新建build資料夾存放opencv的編譯檔案,進入build資料夾程式碼如下
mkdir build
cd build
5. 配置opencv檔案,程式碼如下:
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
或者使用cmake-gui用圖形介面編譯。
6. 進行編譯,程式碼如下:
make
7. 安裝opencv庫到系統,程式碼如下:這樣OpenCV就可以使用了。sudo make install
二 測試
新建一個HelloWorld.cpp,程式碼如下:
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
int main( int argc, char** argv )
{
if( argc != 2 )//判斷引數是否是兩個
{
printf( " No image data \n " );
return -1;
}
char* imageName = argv[1];
Mat image;
image = imread( imageName, 1 );//讀入圖片
if( !image.data)//判斷是否有資料
{
printf( " No image data \n " );
return -1;
}
namedWindow( imageName, CV_WINDOW_AUTOSIZE );
imshow( imageName, image );//顯示圖片
waitKey(0);
return 0;
}
新建CMakeLists.txt,程式碼如下:
cmake_minimum_required(VERSION 2.8)
project(helloworld)
add_executable(helloworld HelloWorld.cpp)
find_package(OpenCV REQUIRED)
target_link_libraries(helloworld ${OpenCV_LIBS})
在兩個檔案所在目錄新建build資料夾存放編譯程式碼,並進行編譯,程式碼如下
mkdir build
cd build
cmkae ..
make
之後可以看見build資料夾裡生成一個helloworld的可執行檔案,執行程式碼:
./helloworld pitcure.jpg
picture.jpg為傳入的圖片,作為引數,放在build資料夾裡,也可以自己編寫路徑。
三 問題
以上就是OpenCV安裝全過程,安裝過程中可能碰到一些問題,例舉一些。
在編譯的過程中可能會碰到找不到Qt5檔案的情況,只是一個警告可以不理,如果有強迫症的同學可以安裝一下Qt5
CMake Warning at cmake/OpenCVFindLibsGUI.cmake:18 (find_package):
By not providing "FindQt5Core.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Qt5Core", but
CMake did not find one.
另一個是ICV hash校驗值不符的問題
CMake Warning at 3rdparty/ippicv/downloader.cmake:54 (message):
ICV: Local copy of ICV package has invalid MD5 hash:
0103b909e19ca9c6497a7ae696c16480 (expected:
8b449a536a2157bcad08a2b9f266828b)
Call Stack (most recent call first):
3rdparty/ippicv/downloader.cmake:108 (_icv_downloader)
cmake/OpenCVFindIPP.cmake:235 (include)
cmake/OpenCVFindLibsPerf.cmake:12 (include)
CMakeLists.txt:526 (include)
-- ICV: Downloading ippicv_linux_20141027.tgz...
CMake Error at 3rdparty/ippicv/downloader.cmake:71 (file):
file DOWNLOAD HASH mismatch
for file: [/home/jason/program/opencv-3.0.0/3rdparty/ippicv/downloads/linux-8b449a536a2157bcad08a2b9f266828b/ippicv_linux_20141027.tgz]
expected hash: [8b449a536a2157bcad08a2b9f266828b]
actual hash: [0103b909e19ca9c6497a7ae696c16480]
Call Stack (most recent call first):
3rdparty/ippicv/downloader.cmake:108 (_icv_downloader)
cmake/OpenCVFindIPP.cmake:235 (include)
cmake/OpenCVFindLibsPerf.cmake:12 (include)
CMakeLists.txt:526 (include)
之後查了很多資料也沒有發現也不知道該怎麼解決,後來去查了一下程式碼,發現這個是下載ippicv_linux_20141027.tgz時的校驗和不符,查看了配置檔案,發現可以自行從http://sourceforge.net/projects/opencvlibrary/files/3rdparty/ippicv這裡下載,奇怪的是編譯時從這裡下載的檔案和自己下載的檔案校驗值居然不一樣,也真是神奇,將下載的檔案
~/opencv-3.0.0/3rdparty/ippicv/downloads/linux-8b449a536a2157bcad08a2b9f266828b ,代替原來的同名檔案,ok問題解決。
配置的時候出現的問題:
ICV: Downloading ippicv_linux_20151201.tgz...