学习网址:https://sites.google.com/site/yonasstephenfyp2013/updates/tutorialinstallingopencvonmacosxmountainlion
下载opencv-4.0.1并解压,进入文件夹后:

mkdir opencv_build
cd opencv_build
cmake -G "Unix Makefiles" ../

配置成功:

-- Found JNI: /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/JavaVM.framework
-- Found VTK 8.1.1 (/usr/local/lib/cmake/vtk-8.1/UseVTK.cmake)
-- Looking for dlerror in dl
-- Looking for dlerror in dl - found
-- ADE: Download: v0.1.1d.zip
-- OpenCV Python: during development append to PYTHONPATH: /Users/weiyang/Downloads/opencv-4.0.1/opencv_build/python_loader
-- Registering hook 'INIT_MODULE_SOURCES_opencv_dnn': /Users/weiyang/Downloads/opencv-4.0.1/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
-- opencv_dnn: filter out ocl4dnn source code
-- Performing Test HAVE_OBJCXX_FOBJC_EXCEPTIONS
-- Performing Test HAVE_OBJCXX_FOBJC_EXCEPTIONS - Success
-- Performing Test HAVE_CXX_WNO_OVERLOADED_VIRTUAL
-- Performing Test HAVE_CXX_WNO_OVERLOADED_VIRTUAL - Success
-- Performing Test HAVE_CXX_WNO_UNUSED_PRIVATE_FIELD
-- Performing Test HAVE_CXX_WNO_UNUSED_PRIVATE_FIELD - Success
--
-- General configuration for OpenCV 4.0.1 =====================================
-- Version control: unknown
--
-- Platform:
-- Timestamp: 2019-02-16T08:00:57Z
-- Host: Darwin 18.2.0 x86_64
-- CMake: 3.10.0
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: Release
--
-- CPU/HW features:
-- Baseline: SSE SSE2 SSE3 SSSE3 SSE4_1
-- requested: DETECT
-- Dispatched code generation: SSE4_2 FP16 AVX AVX2 AVX512_SKX
-- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
-- SSE4_2 (2 files): + POPCNT SSE4_2
-- FP16 (1 files): + POPCNT SSE4_2 FP16 AVX
-- AVX (5 files): + POPCNT SSE4_2 AVX
-- AVX2 (13 files): + POPCNT SSE4_2 FP16 FMA3 AVX AVX2
-- AVX512_SKX (1 files): + POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_SKX
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ Compiler: /Library/Developer/CommandLineTools/usr/bin/c++ (ver 10.0.0.10001044)
-- C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release):
-- Linker flags (Debug):
-- ccache: NO
-- Precompiled headers: NO
-- Extra dependencies:
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java_bindings_generator ml objdetect photo python2 python3 python_bindings_generator stitching ts video videoio
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: java js
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: NO
--
-- GUI:
-- Cocoa: YES
-- VTK support: YES (ver 8.1.1)
--
-- Media I/O:
-- ZLib: build (ver 1.2.11)
-- JPEG: build-libjpeg-turbo (ver 1.5.3-62)
-- WEBP: build (ver encoder: 0x020e)
-- PNG: build (ver 1.6.35)
-- TIFF: build (ver 42 - 4.0.9)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 1.7.1)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: NO
-- FFMPEG: YES
-- avcodec: YES (ver 58.18.100)
-- avformat: YES (ver 58.12.100)
-- avutil: YES (ver 56.14.100)
-- swscale: YES (ver 5.1.100)
-- avresample: YES (ver 4.0.0)
-- GStreamer: NO
-- AVFoundation: YES
--
-- Parallel framework: GCD
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Lapack: YES (/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Accelerate.framework /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Accelerate.framework)
-- Eigen: NO
-- Custom HAL: NO
-- Protobuf: build (3.5.1)
--
-- OpenCL: YES (no extra features)
-- Include path: NO
-- Link libraries: -framework OpenCL
--
-- Python 2:
-- Interpreter: /usr/bin/python2.7 (ver 2.7.10)
-- Libraries: /usr/lib/libpython2.7.dylib (ver 2.7.10)
-- numpy: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include (ver 1.8.0rc1)
-- install path: lib/python2.7/site-packages/cv2/python-2.7
--
-- Python 3:
-- Interpreter: /usr/local/bin/python3 (ver 3.6.4)
-- Libraries: /usr/local/Frameworks/Python.framework/Versions/3.6/lib/libpython3.6m.dylib (ver 3.6.4)
-- numpy: /usr/local/lib/python3.6/site-packages/numpy/core/include (ver 1.13.3)
-- install path: lib/python3.6/site-packages/cv2/python-3.6
--
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI: /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/JavaVM.framework/Headers /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/JavaVM.framework/Headers /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/JavaVM.framework/Headers
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/weiyang/Downloads/opencv-4.0.1/opencv_build

make

...
[100%] Linking CXX executable ../../bin/opencv_test_video
[100%] Built target opencv_test_video
[100%] Building CXX object modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/test/test_undistort_badarg.cpp.o
[100%] Building CXX object modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/test/test_undistort_points.cpp.o
[100%] Linking CXX executable ../../bin/opencv_perf_stitching
[100%] Built target opencv_perf_stitching
[100%] Linking CXX executable ../../bin/opencv_test_calib3d
[100%] Built target opencv_test_calib3d
[100%] Linking CXX executable ../../bin/opencv_perf_video
[100%] Built target opencv_perf_video
[100%] Linking CXX shared module ../../lib/cv2.so
[100%] Built target opencv_python2
[100%] Linking CXX shared module ../../lib/python3/cv2.cpython-36m-darwin.so
[100%] Built target opencv_python3

sudo make install

Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/include/opencv4/opencv2/cvconfig.h
-- Installing: /usr/local/include/opencv4/opencv2/opencv_modules.hpp
-- Installing: /usr/local/lib/cmake/opencv4/OpenCVModules.cmake
...
-- Installing: /usr/local/bin/setup_vars_opencv4.sh
-- Installing: /usr/local/share/opencv4/valgrind.supp
...
-- Installing: /usr/local/share/licenses/opencv4/ittnotify-LICENSE.GPL
-- Installing: /usr/local/include/opencv4/opencv2/opencv.hpp
-- Installing: /usr/local/share/licenses/opencv4/ade-LICENSE
-- Installing: /usr/local/lib/libopencv_core.4.0.1.dylib
...
-- Installing: /usr/local/lib/libopencv_core.dylib
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/ocl_defs.hpp
...
-- Installing: /usr/local/include/opencv4/opencv2/core/utils/trace.hpp
-- Installing: /usr/local/share/licenses/opencv4/SoftFloat-COPYING.txt
-- Installing: /usr/local/lib/libopencv_flann.4.0.1.dylib
-- Installing: /usr/local/lib/libopencv_flann.4.0.dylib
-- Installing: /usr/local/lib/libopencv_flann.dylib
-- Installing: /usr/local/include/opencv4/opencv2/flann.hpp
-- Installing: /usr/local/include/opencv4/opencv2/flann/flann.hpp
...
-- Installing: /usr/local/include/opencv4/opencv2/flann/timer.h
-- Installing: /usr/local/lib/libopencv_imgproc.4.0.1.dylib
-- Installing: /usr/local/lib/libopencv_imgproc.4.0.dylib
-- Installing: /usr/local/lib/libopencv_imgproc.dylib
-- Installing: /usr/local/include/opencv4/opencv2/imgproc.hpp
...
-- Installing: /usr/local/include/opencv4/opencv2/imgproc/detail/gcgraph.hpp
-- Installing: /usr/local/lib/libopencv_ml.4.0.1.dylib
-- Installing: /usr/local/lib/libopencv_ml.4.0.dylib
-- Installing: /usr/local/lib/libopencv_ml.dylib
-- Installing: /usr/local/include/opencv4/opencv2/ml.hpp
-- Installing: /usr/local/include/opencv4/opencv2/ml/ml.hpp
-- Installing: /usr/local/include/opencv4/opencv2/ml/ml.inl.hpp
-- Installing: /usr/local/lib/libopencv_photo.4.0.1.dylib
-- Installing: /usr/local/lib/libopencv_photo.4.0.dylib
-- Installing: /usr/local/lib/libopencv_photo.dylib
-- Installing: /usr/local/include/opencv4/opencv2/photo.hpp
-- Installing: /usr/local/include/opencv4/opencv2/photo/cuda.hpp
-- Installing: /usr/local/include/opencv4/opencv2/photo/photo.hpp
-- Installing: /usr/local/include/opencv4/opencv2/photo/legacy/constants_c.h
-- Installing: /usr/local/lib/libopencv_dnn.4.0.1.dylib
-- Installing: /usr/local/lib/libopencv_dnn.4.0.dylib
-- Installing: /usr/local/lib/libopencv_dnn.dylib
-- Installing: /usr/local/include/opencv4/opencv2/dnn.hpp
...
-- Installing: /usr/local/include/opencv4/opencv2/dnn/version.hpp
-- Installing: /usr/local/lib/libopencv_gapi.4.0.1.dylib
...
-- Installing: /usr/local/lib/libopencv_imgcodecs.4.0.1.dylib
-- Installing: /usr/local/lib/libopencv_imgcodecs.4.0.dylib
-- Installing: /usr/local/lib/libopencv_imgcodecs.dylib
-- Installing: /usr/local/include/opencv4/opencv2/imgcodecs.hpp
-- Installing: /usr/local/include/opencv4/opencv2/imgcodecs/imgcodecs.hpp
-- Installing: /usr/local/include/opencv4/opencv2/imgcodecs/imgcodecs_c.h
-- Installing: /usr/local/include/opencv4/opencv2/imgcodecs/ios.h
-- Installing: /usr/local/include/opencv4/opencv2/imgcodecs/legacy/constants_c.h
-- Installing: /usr/local/lib/libopencv_videoio.4.0.1.dylib
-- Installing: /usr/local/lib/libopencv_videoio.4.0.dylib
-- Installing: /usr/local/lib/libopencv_videoio.dylib
-- Installing: /usr/local/include/opencv4/opencv2/videoio.hpp
...
-- Installing: /usr/local/include/opencv4/opencv2/features2d/hal/interface.h
-- Installing: /usr/local/lib/libopencv_calib3d.4.0.1.dylib
-- Installing: /usr/local/lib/libopencv_calib3d.4.0.dylib
-- Installing: /usr/local/lib/libopencv_calib3d.dylib
-- Installing: /usr/local/include/opencv4/opencv2/calib3d.hpp
...
-- Installing: /usr/local/include/opencv4/opencv2/objdetect/detection_based_tracker.hpp
...
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/warpers_inl.hpp
-- Installing: /usr/local/lib/libopencv_video.4.0.1.dylib
-- Installing: /usr/local/lib/libopencv_video.4.0.dylib
-- Installing: /usr/local/lib/libopencv_video.dylib
-- Installing: /usr/local/include/opencv4/opencv2/video.hpp
-- Installing: /usr/local/include/opencv4/opencv2/video/background_segm.hpp
-- Installing: /usr/local/include/opencv4/opencv2/video/tracking.hpp
-- Installing: /usr/local/include/opencv4/opencv2/video/video.hpp
-- Installing: /usr/local/include/opencv4/opencv2/video/legacy/constants_c.h
-- Installing: /usr/local/lib/python2.7/site-packages/cv2/__init__.py
...
-- Installing: /usr/local/lib/python3.6/site-packages/cv2/config-3.6.py
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_eye.xml
...
-- Installing: /usr/local/share/opencv4/lbpcascades/lbpcascade_silverware.xml
-- Installing: /usr/local/bin/opencv_annotation
-- Installing: /usr/local/bin/opencv_visualisation
-- Installing: /usr/local/bin/opencv_interactive-calibration
-- Installing: /usr/local/bin/opencv_version


第一个工程:
使用CMake build project
参照:https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html
CMakeLists.txt

macro(use_cxx11)
  if (CMAKE_VERSION VERSION_LESS "3.1")
    if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
      set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
    endif ()
  else ()
    set (CMAKE_CXX_STANDARD 11)
  endif ()
endmacro(use_cxx11)

cmake_minimum_required(VERSION 2.8)
project( imageIOByOpenCV )
use_cxx11()
find_package( OpenCV REQUIRED )
add_executable( ${PROJECT_NAME} "main.cpp" )
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )

cpp code:

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char** argv )
{
    Mat image;
    image = imread( "/Users/weiyang/Desktop/Untitled.png", 1 );

    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);

    waitKey(0);

    return 0;
}

成功运行。
输入任意key关闭窗口。

分类: C plus plus

发表评论

电子邮件地址不会被公开。 必填项已用*标注