Mac源码安装使用OpenCV

0.编译安装之前需要下载依赖环境,请参考: Mac安装OpenCV环境及demo
1.下载最新OpenCV源码
2.编译安装
# mkdir -p /usr/local/Cellar/opencv/3.4.0 //指定库安装目录
# midir build
# cd build
# cmake -j8 -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/Cellar/opencv/3.4.0 ../
# make -j8
# sudo make install
4.配置OpenCV环境变量
# emacs /etc/profile 
添加:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/Cellar/opencv/3.4.0/lib/pkgconfig
export PKG_CONFIG_PATH
export LD_LIBRARY_PATH=/usr/local/Cellar/opencv/3.4.0/bin:SLD_LIBRARY_PATH
export PATH=${PATH}:/usr/local/Cellar/opencv/3.4.0/lib
# source /etc/profile //重启电脑

5.测试demo,打印当前版本号
<1>test_version.cpp
#include                                                                                
#include
int main(int argc, const char* argv[]){
  std::cout << "Welcome to OpenCV " << CV_VERSION << std::endl; 
  return 0;
}
<2>.Makefile
CXX ?= g++
CXXFLAGS += -c -Wall $(shell pkg-config --cflags opencv)
LDFLAGS += $(shell pkg-config --libs --static opencv)
all: test_version
opencv_example: test_version.o; $(CXX) $< -o $@ $(LDFLAGS)
%.o: %.cpp; $(CXX) $< -o $@ $(CXXFLAGS)
clean: ; rm -f test_version.o test_version

你可能感兴趣的:(OpenCV,学习笔记)