26 April, 2016

Setup: OpenCV on OS X using Homebrew

It is the spring of 2016 and computer vision is in the air. I have spent some time trying to get OpenCV applications written in C++ running on my MacBook. It was not as straight forward as I had hoped, so I made this guide to help you get going with OpenCV faster than I did.

My system

MacBook Pro Retina 13"
OS X El Capitan version 10.11.4

Open the Terminal

Install Xcode Command Line Tools

Short version:
xcode-select --install
Long version:

Install Homebrew

Short version:
/usr/bin/ruby -e "$(curl -fsSL"
Long version:

All Homebrew-related files can be found at:

Install OpenCV

brew tap homebrew/science
brew info opencv
brew install opencv --with-ffmpeg
brew link opencv          You may have to use: brew link --overwrite opencv

This might be useful when removing or overwriting files:
sudo chown -R `whoami` /usr/local/share

Thanks to:

Test code

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

int main()
  Mat src; 
  Mat hsv;

  src = imread( "/Users/USER/Documents/Test code/input-image.jpg", CV_LOAD_IMAGE_COLOR );

  cvtColor( src, hsv, CV_BGR2HSV );
  Mat hsv_res = hsv > 92;
  Mat hsv_res_gray;
  cvtColor( hsv_res, hsv_res_gray, CV_BGR2GRAY );
  Mat hsv_res_bw = hsv_res_gray > 128;

  imwrite( "/Users/USER/Documents/Test code/output-image.jpg", hsv_res_bw );

  return 0;

Make sure to change the paths of your input-image (line 12) and output-image (line 20).

Compile using Makefile

Create a file with the name "Makefile" with this content:
ARCH=-arch x86_64
LDFLAGS= -L$(SANDBOX_DIR)/lib -lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgproc \
        -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_videostab
clean :
 $(CC) $(ARCH) $(LDFLAGS) $(OBJECTS) -o $@
 $(CC) $(CFLAGS) $< -o $@

In Terminal, use the "cd" command to get to the folder with your main.cpp file. 
Compile using the command "make".
An executable file should appear in the same folder as main.cpp. Open it to run your code.

Thanks to:

Compile using Sublime Text

Create a new build system:
Sublime Text/Tools/Build System/New Build System...

The file should contain:
 "cmd": ["g++", "-Wall", "-Wextra", "${file}", "-o", "${file_path}/${file_base_name}",
 "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
 "working_dir": "${file_path}",
 "selector": "source.c, source.c++",
"name": "Run",
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' `/usr/bin/pkg-config --cflags --libs   /usr/Local/lib/pkgconfig/opencv.pc` && '${file_path}/${file_base_name}' "]

Name the file "opencv-homebrew.sublime-build" or whatever you like.

When you have your main.cpp open in Sublime Text, select the Build System you just created.
Compile by pressing: cmd + b
An executable file should appear in the same folder as main.cpp. Open it to run your code.


I hope this guide helped to get you started with OpenCV without hassle. If you have any questions or suggestions, please comment. 

No comments:

Post a Comment