0
0

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&C++の環境構築をMac(Apple Silicon)にて行う

0
Last updated at Posted at 2026-04-17

大学院の授業でOpenCVを使うことになり、推奨がWindowsだったが手元にMacしかなかったのでやった記録を残しておく。

私の環境

私の環境は以下の通りである。

機種 MacBook Air
CPU Apple M4
OS MacOS Tahoe 26.2
Homebrew Homebrew 5.1.6
Editor VSCode
Compiler clang
C++Version C++14

手順

インストール

以下のコマンドを入れてOpenCVをインストールする。

$ brew install opencv

インストール後バージョン確認を行いたい場合は以下のコマンドにて確認できる。

$ brew list --versions | grep opencv

opencv 4.13.0_8

VSCodeの設定

VSCodeではIntelliSenseを使用するためにOpenCVを使いたいプロジェクト直下でc_cpp_properties.jsonを作成(Command+Shift+P でコマンドパレットを開き,「C/Cpp: Edit configurations」を選択すると自動で生成される)し以下の内容の中であなたの画面に無い物を追記する。

c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/homebrew/include/opencv4"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++14",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}

あとはCMakeLists.txtをいい感じに書いて終了!

CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(test)

set(CMAKE_CXX_STANDARD 11)

find_package(OpenCV REQUIRED)

add_executable(Main main.cpp)

target_include_directories(Main PRIVATE ${OpenCV_INCLUDE_DIRS})
target_link_libraries(Main PRIVATE ${OpenCV_LIBS})

テスト用サンプルコード

main.cpp
#include <opencv2/opencv.hpp>

int main() {
    cv::Mat img = cv::Mat::zeros(480, 640, CV_8UC3);

    cv::imshow("test", img);
    cv::waitKey(0);

    return 0;
}

コンパイル

コンパイルにはCMakeLists.txtを実行する必要があるので、以下のコマンドを打ってcmakeをインストールする。

$ brew install cmake

インストールが終わったら以下のコマンドを実行する。

$ mkdir build
cd build
cmake .. .
make
cd ..
./build/Main

参考

https://qiita.com/toshi_machine/items/908e9be57e8afa629159
https://coolware.jp/blog/homebrew_guide/#:~:text=1.Homebrew%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%83%87%E3%82%A3%E3%83%AC%E3%82%AF%E3%83%88%E3%83%AA,usr/local%20%EF%BC%88Intel%20Mac%EF%BC%89

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?