LoginSignup
21
26

More than 5 years have passed since last update.

OpenCV3.1.0でSIFTとかSURFとかBINGを使う(Windows 7, Visual Studio 2015)

Last updated at Posted at 2016-03-02

背景

・背景1
OpenCVの導入にはいくつかの方法がある。
簡単な順に示すと,以下の通り。

1.NuGet利用
2.公式インストーラ利用
3.ソースからCmakeを利用してビルド

・背景2
OpenCV3系統では,SIFTやSURFやBINGなどのOpenCV拡張機能を使いたいという場合,Cmakeから自前でビルドする必要がある。つまり,背景1で述べた方法の中で,最も大変な3.の方法で導入する必要がある。

目的

opencv, opencv_contribを合わせてビルドし,SIFTを用いたサンプルプログラムを動かすまでを記述。

環境

OS: Windows 7 64bit
Compiler: Visual Studio 2015 (14)
Cmake: 3.4.1 (https://cmake.org/)
(VS2015,Cmakeはすでにインストールされているものとして話をします。)
OpenCV: 3.1.0

手順

1.ソースのダウンロード

githubからソースをダウンロードする。この際,opencv_contribのバージョンはopencvと一致させる。(今回は3.1.0)

https://github.com/Itseez/opencv/releases
https://github.com/Itseez/opencv_contrib/releases

これらはダウンロードし解凍する。
そして,解凍し以下のパスに配置する。
(自分の好きなところでかまいませんが,以下ではこのパスにファイルを配置したと仮定して説明します。)

C:\opencv-3.1.0
C:\opencv-3.1.0\opencv_contrib-3.1.0

2.Cmakeでmake

CMakeを起動後,source codeとbuild the binariesに以下のパスを指定。

source code: C:\opencv-3.1.0
build the binaries: C:\opencv-3.1.0\build

Configureを押下。

generatorは"Visual Studio 14 2015 Win64"を指定してFinishを押下。

Entryの中のOPENCV_EXTRA_MODULES_PATHを指定。

OPENCV_EXTRA_MODULES_PATH: C:\opencv-3.1.0\opencv_contrib-3.1.0\modules

Configureを押下。

もしEntryの中にBUILD_opencv_hdfがあればチェックをはずす。
(一緒にビルドするとエラーが出ます。このバグは今後改善されると思います。)
Configureを押下。

Generateを押下。

3.Visual Studioでビルド

1.ソリューションファイルを開く
C:\opencv-3.1.0\build\OpenCV.slnをVisual Studioから開く。

2.スタートアッププロジェクトの変更
ソリューションエクスプローラーにて,CMakeTargetsフォルダの中にあるINSTALLの上で右クリックし,”スタートアッププロジェクトに設定”を押下する。

3.ソリューション構成の変更
もしDebugになっているのであれば,Releaseに変更。

4.ビルド
メニューにあるビルド>ソリューションのビルドを押下。
しばらくすると完了する。

4.環境設定

1.パスを追加

C:\opencv-3.1.0\build\bin\Release;

変更後はPCの再起動が必要です。

5.動作確認

C:\にCMakeSampleフォルダを作成。
CMakeSample内にlenna.pngを配置。(適当な画像でもかまいません。)
CMakeSample内にCMakeLists.txtを作成し,以下のように記述。
CMakeSample内にmain.cppを作成し,以下のように記述。

CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(CMakeSample)
find_package( OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS})
add_executable(CMakeSample main.cpp)
set(CMAKE_BUILD_TYPE Release)
target_link_libraries(CMakeSample ${OpenCV_LIBS})
main.cpp
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/xfeatures2d.hpp>

int main(int argc, char** argv)
{
    cv::Mat img_1 = cv::imread("C:\\CMakeSample\\lenna.png", cv::IMREAD_GRAYSCALE);
    cv::Mat img_2 = img_1.clone();
    if (!img_1.data || !img_2.data) {
        std::cout << "画像がよみこめません" << std::endl; return -1;
    }
    int minHessian = 400;
    cv::Ptr < cv::xfeatures2d::SURF>detectorSURF = cv::xfeatures2d::SURF::create(minHessian);
    cv::Ptr < cv::xfeatures2d::SIFT>detectorSIFT = cv::xfeatures2d::SIFT::create(minHessian);
    std::vector < cv::KeyPoint>keypoints_1, keypoints_2;
    detectorSURF->detect(img_1, keypoints_1);
    detectorSIFT->detect(img_2, keypoints_2);

    cv::Mat img_1_keypoints;
    cv::Mat img_2_keypoints;
    cv::drawKeypoints(img_1, keypoints_1, img_1_keypoints, cv::Scalar::all(-1), cv::DrawMatchesFlags::DEFAULT);
    cv::drawKeypoints(img_2, keypoints_2, img_2_keypoints, cv::Scalar::all(-1), cv::DrawMatchesFlags::DEFAULT);

    cv::imshow("INPUT_IMG", img_1);
    cv::imshow("SURF_IMG", img_1_keypoints);
    cv::imshow("SIFT_IMG", img_2_keypoints);
    cv::waitKey(0);
    return 0;
}

CMakeを起動後,source codeとbuild the binariesに以下のパスを指定。

source code: C:\CMakeSample
build the binaries: C:\CMakeSample\build

Configureを押下。

generatorは"Visual Studio 14 2015 Win64"を指定してFinishを押下。

Errorが生じる場合は,OpenCV_DIRが正しく認識されていないので,手動で設定。

OpenCV_DIR: C:\opencv-3.1.0\build

また,CMAKE_CONFIGURATION_TYPESを変更。

CMAKE_CONFIGURATION_TYPES: Release

Configureを押下。

Generateを押下。

C:\CMakeSample\build\CMakeSample.slnをVisual Studioから開く。

ソリューションエクスプローラーにて,CMakeSampleの上で右クリックし,”スタートアッププロジェクトに設定”を押下する。

プログラムをビルドして実行すると動作するはず!

参考文献

http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/
http://blog.yucchiy.com/2014/10/18/install-opencv3-with-contrib/
http://docs.opencv.org/master/d5/de5/tutorial_py_setup_in_windows.html#gsc.tab=0

追記

http://qiita.com/yizumi1012xxx/items/124520609e0fe2fbef44
こちらも参考に

21
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
26