1. 程式人生 > >Ubuntu下opencv3.3和opencv_contrib的編譯安裝

Ubuntu下opencv3.3和opencv_contrib的編譯安裝

檔案準備

準備

先安裝以下依賴包

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  

sudo apt-get
install build-essential qt5-default ccache libv4l-dev libavresample-dev libgphoto2-dev libopenblas-base libopenblas-dev doxygen openjdk-8-jdk pylint libvtk6-dev sudo apt-get install pkg-config

編譯

1.解壓下載好的包:

unzip opencv-3.3.1.zip
unzip opencv_contrib-3.3.1.zip

2.解壓完後需要將opencv_contrib.zip提取到opencv目錄下,同時在該目錄下新建一個資料夾build:

cp -r opencv_contrib-3.3.1 opencv-3.3.1  #複製opencv_contrib到opencv目錄下

cd opencv-3.3.1

mkdir build                              #新建資料夾build

3.現在進入到opencv-3.3.1目錄下,檢視檔案結構:

#ls

3rdparty  cmake            data     LICENSE               platforms
apps      CMakeLists.txt   doc      modules               README.md
build CONTRIBUTING.md include opencv_contrib-3.3.1 samples

4.進入build目錄,並且執行cmake生成makefile檔案:


cd build  


cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/files/opencv-3.3.1/opencv_contrib-3.3.1/modules/ .. 

注意:OPENCV_EXTRA_MODULES_PATH就是你 opencv_contrib-3.3.1下面的modules目錄,請按照自己的實際目錄修改地址。還有後面的兩點不可省略!!

接下來就是漫長的等待了…

生成完畢提示:

--   Install path:                  /usr/local
-- 
--   cvconfig.h is in:              /home/files/opencv-3.3.1/build
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/files/opencv-3.3.1/build

5.在cmake成功之後,就可以在build檔案下make了:

make -j8        #8執行緒編譯
make install

接下來就是更漫長的等待了……具體時間因人而異,我的電腦跑了20分鐘。如果看到下圖,說明成功了,再make install就ok了!

這裡寫圖片描述

6.連結庫共享
編譯安裝完畢之後,為了讓你的連結庫被系統共享,讓編譯器發現,需要執行管理命令ldconfig:

sudo ldconfig -v  

問題

Ubuntu16.04會報錯:

Scanning dependencies of target opencv_test_superres
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `TIFFReadDirectory@LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `[email protected]_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `TIFFNumberOfDirectories@LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `[email protected]_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `TIFFOpen@LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `[email protected]_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `TIFFReadRGBAImage@LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `[email protected]_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `TIFFSetField@LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `[email protected]_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `_TIFFfree@LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `[email protected]_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `TIFFScanlineSize@LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `[email protected]_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `TIFFTileSize@LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `[email protected]_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `TIFFSetErrorHandler@LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `[email protected]_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `TIFFReadScanline@LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `[email protected]_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `TIFFNumberOfTiles@LIBTIFF_4.0'
//usr/lib/x86_64-linux-gnu/libvtkIOImage-6.2.so.6.2: undefined reference to `[email protected]_4.0'
[ 74%] Building CXX object modules/imgcodecs/CMakeFiles/opencv_perf_imgcodecs.dir/perf/perf_main.cpp.o
[ 74%] Building CXX object modules/shape/CMakeFiles/opencv_test_shape.dir/test/test_main.cpp.o
[ 74%] Building CXX object modules/highgui/CMakeFiles/opencv_test_highgui.dir/test/test_main.cpp.o
[ 75%] Building CXX object modules/superres/CMakeFiles/opencv_test_superres.dir/test/test_main.cpp.o
[ 75%] Building CXX object modules/videoio/CMakeFiles/opencv_perf_videoio.dir/perf/perf_main.cpp.o
[ 75%] Building CXX object modules/videoio/CMakeFiles/opencv_test_videoio.dir/test/test_main.cpp.o
collect2: error: ld returned 1 exit status
modules/viz/CMakeFiles/opencv_test_viz.dir/build.make:233: recipe for target 'bin/opencv_test_viz' failed
make[2]: *** [bin/opencv_test_viz] Error 1
CMakeFiles/Makefile2:5627: recipe for target 'modules/viz/CMakeFiles/opencv_test_viz.dir/all' failed
make[1]: *** [modules/viz/CMakeFiles/opencv_test_viz.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs...

解決方法:

sudo apt-get autoremove libtiff5-dev

sudo apt-get install libtiff5-dev

例子

接下來你就可以在Clion開始寫你的Opencv程式了,示例: Clion下第一個opencv3程式 ,記得在Cmakelists.txt上多花點功夫寫對。

下面這個例子能跑,證明你上面環境配置完全正確!



/**
* @概述:採用FAST運算元檢測特徵點,採用SIFT運算元對特徵點進行特徵提取,並使用BruteForce匹配法進行特徵點的匹配
* @類和函式:FastFeatureDetector + SiftDescriptorExtractor + BruteForceMatcher
*/


#include<opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>

using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;

int main(int argc, char** argv)
{
    Mat objImage = imread("1.jpg", IMREAD_COLOR);
    Mat sceneImage = imread("2.jpg", IMREAD_COLOR);
    //-- Step 1: Detect the keypoints using SURF Detector
    int minHessian = 400;
    Ptr<SURF> detector = SURF::create(minHessian);
    std::vector<KeyPoint> obj_keypoint, scene_keypoint;
    detector->detect(objImage, obj_keypoint);
    detector->detect(sceneImage, scene_keypoint);
    //computer the descriptors
    Mat obj_descriptors, scene_descriptors;
    detector->compute(objImage, obj_keypoint, obj_descriptors);
    detector->compute(sceneImage, scene_keypoint, scene_descriptors);
    //use BruteForce to match,and get good_matches
    BFMatcher matcher;
    vector<DMatch> matches;
    matcher.match(obj_descriptors, scene_descriptors, matches);
    sort(matches.begin(), matches.end());  //篩選匹配點
    vector<DMatch> good_matches;
    for (int i = 0; i < min(50, (int)(matches.size()*0.15)); i++) {
        good_matches.push_back(matches[i]);
    }
    //draw matches
    Mat imgMatches;
    drawMatches(objImage, obj_keypoint, sceneImage, scene_keypoint,good_matches, imgMatches);
    //get obj bounding
    vector<Point2f> obj_good_keypoint;
    vector<Point2f> scene_good_keypoint;
    for (int i = 0; i < good_matches.size(); i++) {
        obj_good_keypoint.push_back(obj_keypoint[good_matches[i].queryIdx].pt);
        scene_good_keypoint.push_back(scene_keypoint[good_matches[i].trainIdx].pt);
    }
    vector<Point2f> obj_box(4);
    vector<Point2f> scene_box(4);
    obj_box[0] = Point(0, 0);
    obj_box[1] = Point(objImage.cols, 0);
    obj_box[2] = Point(objImage.cols, objImage.rows);
    obj_box[3] = Point(0, objImage.rows);
    Mat H = findHomography(obj_good_keypoint, scene_good_keypoint, RANSAC); //find the perspective transformation between the source and the destination
    perspectiveTransform(obj_box, scene_box, H);
    line(imgMatches, scene_box[0]+Point2f((float)objImage.cols, 0), scene_box[1] + Point2f((float)objImage.cols, 0), Scalar(0, 255, 0), 2);
    line(imgMatches, scene_box[1] + Point2f((float)objImage.cols, 0), scene_box[2] + Point2f((float)objImage.cols, 0), Scalar(0, 255, 0), 2);
    line(imgMatches, scene_box[2] + Point2f((float)objImage.cols, 0), scene_box[3] + Point2f((float)objImage.cols, 0), Scalar(0, 255, 0), 2);
    line(imgMatches, scene_box[3] + Point2f((float)objImage.cols, 0), scene_box[0] + Point2f((float)objImage.cols, 0), Scalar(0, 255, 0), 2);
    //show the result                                                                   
    imshow("匹配圖", imgMatches);
    //save picture file
    imwrite("final.jpg",imgMatches);
    waitKey(0);
    return 0;
}

上傳1.jpg2.jpg

這裡寫圖片描述

這裡寫圖片描述

再傳一張最後跑出的效果圖:

這裡寫圖片描述

相關推薦

Ubuntuopencv3.3opencv_contrib編譯安裝

檔案準備 準備 先安裝以下依賴包 sudo apt-get install build-essential sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcode

ubuntu16.04opencv3.2opencv_contrib編譯安裝

網上存在很多編譯安裝教程,但是為了讓整篇文章整體化,把編譯安裝opencv3.2的過程還是需要在這裡在贅述一遍,方便和我一樣新入手opencv的小白們只看一篇文章,一次編譯成功。 建議:如果是已經編譯過opencv3.2後來想在其中加入opencv_contrib的朋友,可

opencv3.4.1opencv_contrib編譯安裝出錯合集

系統:win7 VS:2017版 opencv:3.4.1 opencv_contrib:3.4.1 cmake: 3.11 因為官網下的opencv解壓後沒有tracking這個包,所以就自

Windows環境編譯OpenCV3.1OpenCV_contrib

參考 https://segmentfault.com/a/1190000003496009 說明一下,當我編譯OpenCV_contrib時,已經是要配合OpenCV3.1的版本才能編譯通過,所以有要自己編譯OpenCV_contrib的請儘量使用較新的OpenCV版本。

CentOSUbuntu安裝配置Greenplum資料庫叢集(包括安裝原始碼編譯安裝

首先說一下,無論是CentOS/RedHat還是Ubuntu都可以按原始碼方式、安裝包方式編譯安裝。 1.   規劃 192.168.4.93(h93)       1個主master  2個主segment、2個映象segment 192.168.4.94(h94) 

mac10.12.6系統使用cmake安裝opencv3.3.0+opencv_contrib-3.3.0

brew與cmake brew安裝 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew解除安裝 /usr/bin/ruby -e "$(curl

Ubuntu Sublime Text 3編輯器的安裝中文輸入及中文環境的適配

Ubuntu預設的編輯器是gedit,本人不是特別喜歡。而傳說中的vim神器更是覺得無比噁心(請抱有不同意見的程式設計師無視我這句話),因此選來選來去選了Sublime Text 3作為御用編輯器。 首先從官網下載(資源似乎在牆外,但有時牆內也可以下,比較神奇): 得到“

MacOS10.9 ZeroMQ4.0.3Java Binding安裝部署

Mac下,對於安裝了brew的朋友,很簡單了。 首先安裝zeromq brew install zeromq 如果報錯,很可能是因為沒有安裝命令列編譯工具。可以通過xcode命令安裝 xcode-select --install

linux【ubuntu 14.04】的【caffe】編譯安裝(CPU 配置)

原文地址:https://www.zybuluo.com/hanxiaoyang/note/364737 特別鳴謝:七月線上 - 專注資料領域的線上教育:https://www.julyedu.com (有部分修正) caffe 是深度學習在影象領域廣泛使用的框架,其 mo

Ubuntu的sublime text 2的安裝破解

sublime text 是一款程式碼編輯器,因為vim使用起來沒那麼方便,比如複製貼上等等,sublime text 比較小,很輕便,所以選擇了它來代替vim,但是呢,它是一個付費軟體,所以來說一下

ubuntu 16.04 OpenCV3.2.0完全編譯安裝

    Opencv可以直接從庫中安裝,也可以自己手動編譯安裝。1、從庫中安裝是最簡單的方式,直接執行下面命令:    sudo apt-get install libopencv-dev python-opencv    如果安裝出錯,那麼可以更新一下源,或是換一個源。2、

ubuntunvm,node以及npm的安裝與使用

詳細 manage tag 不知道 https desc .org 原因 使用方法 一:安裝nvm 首先下載nvm。這裏我們須要使用git,假設沒有安裝git,能夠使用 sudo apt-get install git來安裝 git clone https://g

CentosSphinx的下載與編譯安裝

onf total osi training art exe .gz clas com 官方下載地址 http://sphinxsearch.com/downloads/release/ 百度雲下載地址 https://pan.baidu.com/s/1gfmPbd5

CentosSphinx中文分詞編譯安裝測試---CoreSeek

orm 創建索引 auxiliary aclocal memcache acl inux mage arch 要支持中文分詞,還需要下載Coreseek,可以去官方搜索下載,這裏我用的4.1 百度雲下載地址: https://pan.baidu.com/s/1sl

Ubuntu用gladeGTK+開發C語言界面程序(一)

命令行 簡單的 暑假 all 位置 相同 write 3.0 面向對象的思想 前言:對於大學中計算機系的每年暑假的課設有太多想說的,能從中學到非常多東西,當然不排除打醬油的,這些能夠掠過哦,凡事都打醬油。人生也是打醬油的吧。2333。 對於大三曾經的課設一般的要求

ubuntu關於profilebashrc中環境變量的理解

ack ready 有效 cut file col 註意 parent nic (0) 寫在前面 有些名詞可能需要解釋一下。(也可以先不看這一節,在後面看到有疑惑再上來看相關解釋) 啟動bash shell:就是啟動一個bash shell進程,通常可以理解為打開一個終端。

[硬件]_ELVE_VS2015opencv3.3的配置問題

編輯 測試 spa 完成 裏的 如果 好用 依賴 右鍵 0x00 引言 最近想搞一下攝像頭,但是我的Windows版本是64位的,opencv3.3貌似也只支持64位系統了,所以就配置一下win10+vs2015+opencv3.3的環境變量,具體下載和安裝以及後期測試就

opencv3.3+TensorFlow+python3.5安裝

tensorflow opencv3.3 python3.5安裝 1、安裝Python環境,推薦使用Anaconda,過程不再贅述。2、下載opencv_python3.3.0cp35cp35mwin_amd64.whl或...win32.whl,opencv_python下載地址,下載好後把文件

Windowsswoole擴展的編譯安裝部署

php.ini cygwin .com 官網下載 -1 文件結尾 png -s 9.png 1. 到cygwin官網下載cygwin。 官網地址:https://www.cygwin.com/ 2. 打開下載好的cygwin安裝包,開始安裝cy

CentOS 7MariaDB源碼包編譯安裝

lib make 保存 使用 dir zlib 數據庫的安裝 RM .org 下載安裝包 到官網下載MariaDB 10.2系列穩定版的源碼包 選擇源碼包 點擊鏈接後頁面會跳到下載頁面,頁面中有個信息表格可以填寫,這裏我們就直接跳過,點擊 No thanks, just t