0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OpenCVとONNXRuntimeを用いるためのC++環境構築

Last updated at Posted at 2025-06-15

Xcodeコマンドラインツールのインストール

C++コンパイラ(Clang)やGitなどの基本的な開発ツールをインストール

xcode-select --install

Homebrewのインストール

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Apple Silicon(M1/M2など)では /opt/homebrew、Intel Mac では /usr/local にインストールされる。

CMakeのインストール

brew install cmake

CMakeはクロスプラットフォームなビルドツールでプロジェクト管理に使用

OpenCVのインストール

brew install opencv

ONNX Runtimeのインストール

brew install onnxruntime

これでうまくいかなかった場合はプロジェクトディレクトリに移動し、手動でインストール

# macOS ARM64用ONNX Runtimeをダウンロード(M1/M2/M3 Mac用)
curl -L -o onnxruntime-osx-arm64-1.16.3.tgz \
  https://github.com/microsoft/onnxruntime/releases/download/v1.16.3/onnxruntime-osx-arm64-1.16.3.tgz

# 解凍
tar -xzf onnxruntime-osx-arm64-1.16.3.tgz

# onnxruntimeという名前でシンボリックリンクを作成
ln -sf onnxruntime-osx-arm64-1.16.3 onnxruntime

# 構造を確認
ls -la onnxruntime/include/

Intel Macの場合は以下

# Intel Mac用
curl -L -o onnxruntime-osx-x86_64-1.16.3.tgz \
  https://github.com/microsoft/onnxruntime/releases/download/v1.16.3/onnxruntime-osx-x86_64-1.16.3.tgz

tar -xzf onnxruntime-osx-x86_64-1.16.3.tgz
ln -sf onnxruntime-osx-x86_64-1.16.3 onnxruntime

IDEやエディタのインストール

VSCodeやCLionなど用途に合わせて選択。Xcodeも利用可能。

プロジェクトディレクトリ作成

mkdir MyApp && cd MyApp
mkdir src include build
touch src/main.cpp CMakeLists.txt

CMakeLists.txtに以下の記述をする

cmake_minimum_required(VERSION 3.20)
project(MyApp LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)

# OpenCV
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

# ONNX Runtime - 手動指定
set(ONNXRUNTIME_DIR "/opt/homebrew/opt/onnxruntime")
include_directories(${ONNXRUNTIME_DIR}/include)
link_directories(${ONNXRUNTIME_DIR}/lib)

add_executable(MyApp src/main.cpp)
target_link_libraries(MyApp PRIVATE ${OpenCV_LIBS} onnxruntime)

/opt/homebrew は Apple Silicon Mac のパスです。Intel Mac の場合は /usr/local/opt に置き換えてください。

src/main.cppに以下のテストコードを記載

#include <opencv2/opencv.hpp>

int main() {
    cv::Mat img = cv::imread("test.jpg");
    cv::imshow("表示テスト", img);
    cv::waitKey(0);
    return 0;
}

CMakeビルド実行

cd build
cmake ..
make

vcpkgによる依存関係管理

git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh # macOS/Linux
./vcpkg install onnxruntime opencv4

CMakeにvcpkgのパスを教える

cmake .. -DCMAKE_TOOLCHAIN_FILE=./vcpkg/scripts/buildsystems/vcpkg.cmake

cmake_minimum_required(VERSION 3.14)
project(MyApp)

# vcpkg経由でOpenCVを探す
find_package(OpenCV REQUIRED)

# ONNX Runtimeは手動でインクルードとリンクパスを指定
set(ONNX_DIR "/path/to/onnxruntime")

include_directories(${ONNX_DIR}/include)
link_directories(${ONNX_DIR}/lib)

add_executable(MyApp main.cpp)
target_link_libraries(MyApp PRIVATE ${OpenCV_LIBS} onnxruntime)

vcpkgがパスを通してくれるため手動のOpenCV_DIRなどの設定が不要になる

今後

CVPKG_ROOTを環境変数に設定しておくことでCMakeのオプションを書く必要がなくなる

export VCPKG_ROOT=/path/to/vcpkg

~/.zshrcなどに記載しておくと便利

プロジェクトルートにvcpkg.jsonを置くとチーム開発がしやすくなる。

{
  "name": "myapp",
  "version-string": "0.1.0",
  "dependencies": [
    "opencv",
    "onnxruntime"
  ]
}

この後に

vcpkg install
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?