not good but great

プログラミング、アート、映画・本の感想について書きます。

MacでC++からOpenCV4を使う

$brew install opencv

これでopencvが入る。

$brew list --versions | grep opencv
opencv 2.4.13.2

バージョンが古かった

$brew upgrade opencv

upgradeするがいろいろエラーが起こる。

$brew rm opencv && brew install opencv
Error: suite-sparse is already installed from homebrew/science!
Please `brew uninstall suite-sparse` first."

一度消してinstallし直し。

$brew uninstall suite-sparse                                                                                                                    1 ↵
Error: Refusing to uninstall /usr/local/Cellar/suite-sparse/4.5.3
because it is required by octave, which is currently installed.
You can override this and force removal with:
  brew uninstall --ignore-dependencies suite-sparse

エラーメッセージに従う。

$brew uninstall --ignore-dependencies suite-sparse
brew install opencv
$brew list --versions | grep opencv                                                                                                             1 ↵
opencv 4.3.0

インストールできた。

Cppの実行

$g++ main.cpp -o main.out `pkg-config --cflags opencv` `pkg-config --libs opencv` && ./main.out
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
main.cpp:7:10: fatal error: 'opencv2/opencv.hpp' file not found
#include "opencv2/opencv.hpp"
         ^~~~~~~~~~~~~~~~~~~~
1 error generated.

実行できない。

$sudo cp /usr/local/Cellar/opencv/4.3.0/lib/pkgconfig/opencv4.pc /usr/local/lib/pkgconfig/opencv.pc

pkgconfigにopencvをcopyする。

$echo `pkg-config --cflags opencv`
-I/usr/local/Cellar/opencv/4.3.0/include/opencv4/opencv -I/usr/local/Cellar/opencv/4.3.0/include/opencv4
$ g++ -std=c++11 main.cpp -o main.out `pkg-config --cflags opencv` `pkg-config --libs opencv` && ./main.out

-std=c++11が必要。

参考

C++ - OpenCVコンパイルエラー|teratail https://teratail.com/questions/167878