Pages

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: http://railsapps.github.io/xcode-command-line-tools.html

Install Homebrew

Short version:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Long version: http://brew.sh/

All Homebrew-related files can be found at:
/usr/local/Cellar

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:
https://www.youtube.com/watch?v=IkCRdySG6as
https://jjyap.wordpress.com/2014/05/24/installing-opencv-2-4-9-on-mac-osx-with-python-support/

Test code

main.cpp:
#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 );

  waitKey(0);
  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:
CC=g++
ARCH=-arch x86_64
 
SANDBOX_DIR=/usr/local/
INCLUDE_FLAGS=-I$(SANDBOX_DIR)/include
 
CFLAGS=-c -Wall $(INCLUDE_FLAGS)
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
 
SOURCES=main.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=testMe
 
all : $(SOURCES) $(EXECUTABLE)
 
clean :
 rm -f $(OBJECTS) $(EXECUTABLE)
 
$(EXECUTABLE): $(OBJECTS) Makefile
 $(CC) $(ARCH) $(LDFLAGS) $(OBJECTS) -o $@
 
.cpp.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}",
 "-I/usr/local/include/opencv",
 "-I/usr/local/include",
 "/usr/local/lib/libopencv_calib3d.dylib",
 "/usr/local/lib/libopencv_core.dylib",
 "/usr/local/lib/libopencv_features2d.dylib",
 "/usr/local/lib/libopencv_flann.dylib",
 "/usr/local/lib/libopencv_highgui.dylib",
 "/usr/local/lib/libopencv_imgproc.dylib",
 "/usr/local/lib/libopencv_ml.dylib",
 "/usr/local/lib/libopencv_objdetect.dylib",
 "/usr/local/lib/libopencv_photo.dylib",
 "/usr/local/lib/libopencv_stitching.dylib",
 "/usr/local/lib/libopencv_video.dylib",
 "/usr/local/lib/libopencv_videostab.dylib"],
 "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
 "working_dir": "${file_path}",
 "selector": "source.c, source.c++",
 
"variants":
 [{
"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