ブロガーは昨夜、OpenCV の SURF インターフェースを使用する必要がありましたが、ヘッダー ファイルがないために呼び出すことができませんでした。そこで情報を確認したところ、これらのライブラリは米国が特許を取得しており、有料ライブラリになっていることがわかりました。OpenCV を既にインストールしている場合、または OpenCV をインストールしていない場合は、このチュートリアルに従って opencv_contrib をインストールできます。
1. OpenCV をインストールする
OpenCV をインストールしていない場合は、最初に読むことができます: Ubuntu は OpenCV3.4.5 をインストールします (2 つの方法 && グラフィックとテキストによる説明)。
2. opencv_contrib をダウンロード
opencv_contribをダウンロードし、[タグ] で対応するバージョンを見つけてダウンロードします。次に、opencv_contrib を opencv ディレクトリに解凍します。
3、cmake解析
ターミナルを起動し、cd でビルド ディレクトリに移動し、次のコマンドを入力します。その後に、Path はモジュールのディレクトリです。
cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_EXTRA_MODULES_PATH=~/Software/opencv-3.4.5/opencv_contrib-3.4.5/modules ..
次のインストール以外の問題が発生するはずです。
-- xfeatures2d/vgg: Download: vgg_generated_64.i
CMake Warning at cmake/OpenCVDownload.cmake:193 (message):
xfeatures2d/vgg: Download failed: 28;"Timeout was reached"
For details please refer to the download log file:
/home/dzh/Software/opencv-3.4.5/build/CMakeDownloadLog.txt
Call Stack (most recent call first):
opencv_contrib-3.4.5/modules/xfeatures2d/cmake/download_vgg.cmake:16 (ocv_download)
opencv_contrib-3.4.5/modules/xfeatures2d/CMakeLists.txt:9 (download_vgg_descriptors)
-- xfeatures2d/vgg: Download: vgg_generated_80.i
--
=======================================================================
Couldn't connect to server from the Internet.
Perhaps direct connections are not allowed in the current network.
To use proxy please check/specify these environment variables:
- http_proxy/https_proxy
- and/or HTTP_PROXY/HTTPS_PROXY
=======================================================================
CMake Warning at cmake/OpenCVDownload.cmake:193 (message):
xfeatures2d/vgg: Download failed: 7;"Couldn't connect to server"
For details please refer to the download log file:
/home/dzh/Software/opencv-3.4.5/build/CMakeDownloadLog.txt
Call Stack (most recent call first):
opencv_contrib-3.4.5/modules/xfeatures2d/cmake/download_vgg.cmake:16 (ocv_download)
opencv_contrib-3.4.5/modules/xfeatures2d/CMakeLists.txt:9 (download_vgg_descriptors)
-- data: Download: face_landmark_model.dat
--
=======================================================================
Couldn't connect to server from the Internet.
Perhaps direct connections are not allowed in the current network.
To use proxy please check/specify these environment variables:
- http_proxy/https_proxy
- and/or HTTP_PROXY/HTTPS_PROXY
=======================================================================
CMake Warning at cmake/OpenCVDownload.cmake:193 (message):
data: Download failed: 7;"Couldn't connect to server"
For details please refer to the download log file:
/home/dzh/Software/opencv-3.4.5/build/CMakeDownloadLog.txt
Call Stack (most recent call first):
opencv_contrib-3.4.5/modules/face/CMakeLists.txt:13 (ocv_download)
CMake Warning at opencv_contrib-3.4.5/modules/face/CMakeLists.txt:26 (message):
Face: Can't get model file for face alignment.
-- No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
-- Found installed version of gflags: /usr/lib/x86_64-linux-gnu/cmake/gflags
-- Detected gflags version: 2.2.1
-- Found installed version of Eigen: /usr/local/lib/cmake/eigen3
-- Found required Ceres dependency: Eigen version 3.2.92 in /usr/local/include/eigen3
-- Found required Ceres dependency: glog
-- Found installed version of gflags: /usr/lib/x86_64-linux-gnu/cmake/gflags
-- Detected gflags version: 2.2.1
-- Found required Ceres dependency: gflags
-- Found Ceres version: 1.14.0 installed in: /usr/local with components: [EigenSparse, SparseLinearAlgebraLibrary, LAPACK, SuiteSparse, CXSparse, SchurSpecializations, OpenMP, Multithreading]
-- Checking SFM deps... TRUE
実際には、ネットワーク接続の問題でダウンロードに失敗し、ダウンロード アドレスに接続できませんでした。インターネット上のすべてのチュートリアルは次のように要約できます: 1. ホットスポットを変更して数回接続する; 2. ファイルをインターネットからローカルにダウンロードする; 3. ダウンロードアドレスを変更する,
ここでは科学インターネット アクセスを使用し、github にアクセスしてファイルをローカルにダウンロードし、ダウンロードしたboostdesc_bgm.i
シリーズとvgg_generated_48.i
シリーズopencv-3.4.5/opencv_contrib-3.4.5/modules/xfeatures2d/src/
をディレクトリに配置します。ダウンロードしたものface_landmark_model.dat
を入れるopencv-3.4.5/opencv_contrib-3.4.5/modules/face/src/
分析のために再度 cmake コマンドを入力すると、上記のファイルにエラーが報告されますが、必要なファイルは既に用意されているため、心配する必要はありません。
4. コンパイルする
入力sudo make -j4
してコンパイルします。-j4 は 4 つのスレッドで実行することを意味します。
エラー 1 |
---|
In file included from /home/dzh/Software/opencv-3.4.5/modules/stitching/include/opencv2/stitching.hpp:49:0,
from /home/dzh/Software/opencv-3.4.5/modules/stitching/src/precomp.hpp:59,
from /home/dzh/Software/opencv-3.4.5/build/modules/stitching/opencv_stitching_pch_dephelp.cxx:1:
/home/dzh/Software/opencv-3.4.5/modules/stitching/include/opencv2/stitching/detail/matchers.hpp:52:12: fatal error: opencv2/xfeatures2d/cuda.hpp: 没有那个文件或目录
# include "opencv2/xfeatures2d/cuda.hpp"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/build.make:62: recipe for target 'modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/opencv_stitching_pch_dephelp.cxx.o' failed
make[2]: *** [modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/opencv_stitching_pch_dephelp.cxx.o] Error 1
CMakeFiles/Makefile2:15804: recipe for target 'modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/all' failed
make[1]: *** [modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/all] Error 2
make[1]: *** 正在等待未完成的任务....
インクルードのcuda.hppヘッダーファイルパスが見つからず、絶対パスに変更する必要があるため、上記のERRORは必ず表示されると思います。プロンプトが表示されたファイルを開き、matchers.hpp
以下に示すように cuda.hpp ディレクトリを見つけます。
include "opencv2/xfeatures2d/cuda.hpp"
パスをコピーして、私のように置き換えます。/home/dzh/Software/opencv-3.4.5/opencv_contrib-3.4.5/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp
エラー 2 |
---|
In file included from /home/dzh/Software/opencv-3.4.5/build/modules/stitching/opencv_stitching_pch_dephelp.cxx:1:0:
/home/dzh/Software/opencv-3.4.5/modules/stitching/src/precomp.hpp:91:12: fatal error: opencv2/xfeatures2d/cuda.hpp: 没有那个文件或目录
# include "opencv2/xfeatures2d/cuda.hpp"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Error1の経験上、間違ったファイルを見つけるエラーメッセージに従って、パスを上記の絶対パスに変更します。
エラー 3 |
---|
[ 90%] Building CXX object modules/stitching/CMakeFiles/opencv_stitching.dir/src/matchers.cpp.o
/home/dzh/Software/opencv-3.4.5/modules/stitching/src/matchers.cpp:52:10: fatal error: opencv2/xfeatures2d.hpp: 没有那个文件或目录
#include "opencv2/xfeatures2d.hpp"
^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
modules/stitching/CMakeFiles/opencv_stitching.dir/build.make:164: recipe for target 'modules/stitching/CMakeFiles/opencv_stitching.dir/src/matchers.cpp.o' failed
make[2]: *** [modules/stitching/CMakeFiles/opencv_stitching.dir/src/matchers.cpp.o] Error 1
CMakeFiles/Makefile2:15854: recipe for target 'modules/stitching/CMakeFiles/opencv_stitching.dir/all' failed
make[1]: *** [modules/stitching/CMakeFiles/opencv_stitching.dir/all] Error 2
make[1]: *** 正在等待未完成的任务....
[ 90%] Building CXX object modules/sfm/CMakeFiles/opencv_sfm.dir/src/simple_pipeline.cpp.o
/home/dzh/Software/opencv-3.4.5/opencv_contrib-3.4.5/modules/sfm/src/simple_pipeline.cpp:44:10: fatal error: opencv2/xfeatures2d.hpp: 没有那个文件或目录
#include <opencv2/xfeatures2d.hpp>
^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
modules/sfm/CMakeFiles/opencv_sfm.dir/build.make:230: recipe for target 'modules/sfm/CMakeFiles/opencv_sfm.dir/src/simple_pipeline.cpp.o' failed
make[2]: *** [modules/sfm/CMakeFiles/opencv_sfm.dir/src/simple_pipeline.cpp.o] Error 1
make[2]: *** 正在等待未完成的任务....
The error report shows that it cannot be found xfeatures2d.hpp
. 同様に、エラーが報告された場所が見つかり、パスが絶対パスに変更されます。
/home/dzh/Software/opencv-3.4.5/opencv_contrib-3.4.5/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp
成功 |
---|
いろいろ失敗した末、最終メイク成功!古いことわざのように、世界に難しいことは何もなく、心を持っている人だけです。
5、インストールインストールを行う
インストール プロセスは非常に高速で、約 15 秒でインストールできます。
sudo make install
6. インストールのテスト
#include <QApplication>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/xfeatures2d/nonfree.hpp"
#include "opencv2/xfeatures2d.hpp"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
cv::Mat image = cv::imread("/home/dzh/QtOpenCV/image.png", 0); //以灰度图像读取
vector<cv::KeyPoint> keypoints;//特征点数组
// cv::FAST(image, keypoints, 40, true); //Fast特征点
// cv::drawKeypoints(image, keypoints, image, cv::Scalar(255 ,255, 255), cv::DrawMatchesFlags::DRAW_OVER_OUTIMG);
cv::Ptr<cv::xfeatures2d::SURF> detector = cv::xfeatures2d::SURF::create(2500.);//初始化检测器对象
detector->detect(image, keypoints);//检测SURF特征点
cv::drawKeypoints(image, keypoints, image, cv::Scalar::all(-1), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);//绘制特征点
cv::imshow("image", image);
cv::waitKey(0);
return a.exec();
}