Mac下编译opencv

Mac下编译opencv

使用HomeBrew安装cmake

1
brew install cmake

使用HomeBrew安装opencv

1
brew install opencv

Opencv安装的位置为 :/usr/local/Cellar/opencv

使用源码编译

1
2
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

选择想要编译的opencv版本源码

新建build文件夹

1
2
cd~ / opencv
mkdir build

配置

1
2
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

此处选择安装位置为/usr/local

Make

1
2
make -j4
sudo make install

等待即可完成

编译静态精简库

可参考centos下的《编译opencv精简静态库》

测试

  • DisplayImage.cpp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    #include <stdio.h>
    #include <opencv2/opencv.hpp>
    using namespace cv;
    int main(int argc, char** argv )
    {
    if ( argc != 2 ){
    printf("usage: DisplayImage.out <Image_Path>\n");
    return -1;
    }
    Mat image;
    image = imread( argv[1], 1 );
    if ( !image.data ){
    printf("No image data \n");
    return -1;
    }
    printf("Load image successful!\n");
    printf("image size is: < %d x %d>\n",image.cols,image.rows);
    for (int row = 0; row < 1; row++) {
    uchar* data = image.ptr<uchar>(row);
    for (int col = 0; col < 5; col++) {
    printf("%d \t", data[col]);
    }
    printf("\n");
    }
    return 0;
    }
  • CMakeLists.txt

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
      cmake_minimum_required(VERSION 2.8)
    add_compile_options(-std=c++11)
    project( DisplayImage )
    set(OPENCV_PATH /usr/local/Cellar/opencv/4.1.0_2)
    set(JPEG_PATH /usr/local/Cellar/jpeg/9c)
    set(PNG_PATH /usr/local/Cellar/libpng/1.6.37)
    add_executable( DisplayImage DisplayImage.cpp )
    include_directories(${OPENCV_PATH}/include/opencv4 ${JPEG_PATH}/include ${PNG_PATH}/include)
    # set(OPENCV_LIBS ${OPENCV_PATH}/lib/libopencv_imgproc.a ${OPENCV_PATH}/lib/libopencv_highgui.a ${OPENCV_PATH}/lib/libopencv_core.a ${JPEG_PATH}/lib/liblibjpeg.a ${PNG_PATH}/lib/liblibpng.a)
    #target_link_libraries( DisplayImage ${OPENCV_LIBS} dl m pthread rt)
    target_link_libraries( DisplayImage ${OPENCV_PATH}/lib/libopencv_imgproc.a ${OPENCV_PATH}/lib/libopencv_highgui.a ${OPENCV_PATH}/lib/libopencv_core.a ${JPEG_PATH}/lib/liblibjpeg.a ${PNG_PATH}/lib/liblibpng.a dl m pthread rt)

cmake ,make即可

###cmake注意事项

  • 链接库顺序

    ​ cmake中要注意链接的顺序,libopencv_imgproc.a,libopencv_core.a对libzlib.a存在依赖,应该把libzlib.a放在后面,libopencv_highgui.a读写图片还需要liblibjpeg.a等图像解码库的支持,liblibjpeg.a也要放在后面。

    在项目开发过层中尽量让lib是垂直关系,避免循环依赖;越是底层的库,越是往后面写!例如:

    1
    2
    > g++ ...  obj($?) -l(上层逻辑lib) -l(中间封装lib) -l(基础lib) -l(系统lib)  -o $@
    >
  • 源文件和链接库顺序

    *.cpp文件应该放在链接库前面

    所以 以下方式是编译不通过的

    1
    2
    3
    ...
    link_libraries(${OPENCV_LIBS} dl m pthread rt)
    target_link_libraries( DisplayImage)

    应该改为如下方式

    1
    target_link_libraries( DisplayImage ${OPENCV_LIBS} dl m pthread rt)

    或者直接不设OPENCV_LIBS变量,直接将链接写在后方