LoginSignup
5
4

More than 1 year has passed since last update.

macOS+homebrew : C++でopencv4

Last updated at Posted at 2019-03-07

ビルドできなくてえらい苦労したのでメモ

導入

brew install opencv
> brew info opencv
opencv: stable 4.0.1 (bottled)
Open source computer vision library
https://opencv.org/
/usr/local/Cellar/opencv/4.0.1 (679 files, 226MB) *
  Poured from bottle on 2019-03-07 at 12:11:25
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/opencv.rb
==> Dependencies
Build: cmake ✔, pkg-config ✔
Required: eigen ✔, ffmpeg ✔, jpeg ✔, libpng ✔, libtiff ✔, numpy ✔, openexr ✔, python ✔, python@2 ✔, tbb ✔
==> Analytics
install: 13,231 (30 days), 53,429 (90 days), 231,530 (365 days)
install_on_request: 12,353 (30 days), 48,865 (90 days), 212,007 (365 days)
build_error: 0 (30 days)

ソース

sample1.cc
#include <opencv2/opencv.hpp>

int main(int argc, char* argv[])
{
  //cv::Mat image = cv::Mat::zeros(100, 100, CV_8UC3);
  cv::Mat image = cv::imread("sample.jpg");
  cv::imshow("sample1", image);
  cv::waitKey(0);
  return 0;
}

ビルドオプションに -std=c++11を付けること。

CXX=g++
CXXFLAGS= -Wall -std=c++11 `pkg-config opencv4 --cflags`
OPENCV_LIBS= `pkg-config opencv4 --libs`

all: sample1

sample1: sample1.o
    $(CXX) -g -o $@ $^ $(OPENCV_LIBS)

clean:
    rm -f sample1 *.o *~

.PHONY: all clean

cmakeで書くならこう

CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
find_package(PkgConfig)

project(SampleProject)

pkg_check_modules(OPENCV4 REQUIRED opencv4)

#
# sample1
#

add_executable(sample1
  sample1.cc)

target_include_directories(sample1
  PUBLIC
  ${OPENCV4_INCLUDE_DIRS})

target_link_libraries(sample1
  PUBLIC
  ${OPENCV4_LDFLAGS}
)

target_compile_features(sample1
  PUBLIC
  cxx_std_11)

target_compile_options(sample1
  PUBLIC
  -Wall
  )
5
4
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
5
4