Skip to content

Instantly share code, notes, and snippets.

@demid5111
Last active February 9, 2021 16:35
Show Gist options
  • Save demid5111/b5d3a93bdb05554312b161bdedba497e to your computer and use it in GitHub Desktop.
Save demid5111/b5d3a93bdb05554312b161bdedba497e to your computer and use it in GitHub Desktop.
Instructions for installing opencv on macOS

Install OpenCV 4.5 on macOS

Pre-configured OpenCV for Python from PyPi

  1. open the terminal
  2. check Python3 installation: python3 --version
  3. go to official website to learn details: https://pypi.org/project/opencv-python/
  4. choose the most complete package and run: pip install opencv-contrib-python
  5. check installation by entering the Python REPL: python3
  6. import tha library: import cv2

Pre-configured OpenCV for Python and C++ from homebrew

  1. brew install opencv

Build OpenCV for Python and C++ from sources

  1. Create a tmp folder for all archives: mkdir ~/opencv4.5-tmp && cd ~/opencv4.5-tmp
  2. Download opencv sources: wget https://github.com/opencv/opencv/archive/4.5.1.zip -O opencv.zip
  3. Download opencv-contrib sources: wget https://github.com/opencv/opencv_contrib/archive/4.5.1.zip -O opencv_contrib.zip
  4. Unzip the opencv files: unzip opencv.zip
  5. Unzip the opencv-contrib files: unzip opencv_contrib.zip
  6. Move the files to simple directories: mv opencv-4.5.1/ opencv
  7. Move opencv-contrib files to simple directories: mv opencv_contrib-4.5.1/ opencv_contrib
  8. Make build directory: cd opencv && mkdir build && cd build
  9. Copy and run the following command. Install cmake if it is not available on the system.
    cmake -D CMAKE_BUILD_TYPE=DEBUG \
          -D CMAKE_INSTALL_PREFIX=~/opencv4.5-custom \
          -D OPENCV_EXTRA_MODULES_PATH=~/opencv4.5-tmp/opencv_contrib/modules \
          -D BUILD_EXAMPLES=ON ..
  10. Make the project:
    make -j4
    
  11. Install opencv:
    sudo make install
    

Configure Xcode project to work with opencv

  1. Create a new macOS project for Commandline

  2. Paste the following code to the main.cpp

  3. Select the project, go to 'Build settings' and then to Header Search Paths add include directory of opencv installation (for example for Debug if you have built or use opencv for Debug),

    1. brew package: /usr/local/Cellar/opencv/4.5.1_2/include
    2. custom build: /Users/<YOUR USER>/opencv4.5-custom/include Do not forget to mark the path as recursive.
  4. Select the project, go to 'Build settings' and then to Library Search Paths add lib directory of opencv installation (for example for Debug if you have built or use opencv for Debug),

    1. brew package: /usr/local/Cellar/opencv/4.5.1_2/lib
    2. custom build: /Users/<YOUR USER>/opencv4.5-custom/lib Do not forget to mark the path as recursive.
  5. Select the project, go to 'Build settings' and then to Other Linker Flags add all the necessary flags - all libraies that we use in our application. In our application it is:

    -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_core
    

    To name the few more available libraries that can be included with the following linker flags:

    -lopencv_gapi -lopencv_stitching -lopencv_alphamat \
    -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired \
    -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres \
    -lopencv_dpm -lopencv_face -lopencv_freetype \
    -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash \
    -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc \
    -lopencv_quality -lopencv_rapid -lopencv_reg \
    -lopencv_rgbd -lopencv_saliency -lopencv_sfm \
    -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping \
    -lopencv_superres -lopencv_optflow -lopencv_surface_matching \
    -lopencv_tracking -lopencv_highgui -lopencv_datasets \
    -lopencv_text -lopencv_plot -lopencv_videostab \
    -lopencv_videoio -lopencv_viz -lopencv_xfeatures2d \
    -lopencv_shape -lopencv_ml -lopencv_ximgproc \
    -lopencv_video -lopencv_dnn -lopencv_xobjdetect \
    -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs \
    -lopencv_features2d -lopencv_flann -lopencv_xphoto \
    -lopencv_photo -lopencv_imgproc -lopencv_core
    
  6. If you need to work with images inside the project and do not want absolute paths, in Edit Scheme go to the Options tab, select Run in the list and check Use Custom Working Directory. Then select the project directory.

  7. If you need access to webcamera,

    1. Create new file: File->New->File->Property list with a name Info.plist
    2. Add new row with Privacy - Camera Usage Description as a key and Need access to camera as a value
    3. Copy Info.plist to the folder where your target (product) lives.
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>
#include "iostream"
int showImage() {
std::string path = "img/test.jpeg";
cv::Mat img = cv::imread(path);
imshow("Portrait", img);
cv::waitKey(0);
return 0;
}
int showWebCameraContent() {
// open the first webcam plugged in the computer
cv::VideoCapture camera(0);
if (!camera.isOpened()) {
std::cerr << "ERROR: Could not open camera" << std::endl;
return 1;
}
// create a window to display the images from the webcam
cv::namedWindow("Webcam");
// this will contain the image from the webcam
cv::Mat frame;
// display the frame until you press a key
while (1) {
// capture the next frame from the webcam
camera >> frame;
// show the image on the window
cv::imshow("Webcam", frame);
// wait (10ms) for a key to be pressed
if (cv::waitKey(10) >= 0) {
break;
}
}
return 0;
}
int main(int, char**) {
showImage();
// showWebCameraContent();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment