Message Boards Message Boards

LibraryLink Example with OpenCV from Macports


Recently Macports is gaining more attention from the open source community on the Mac platform. It has a build for OpenCV which is every intuitive to install in terminal. Here is the package info of this OpenCV port:

$ port info opencv
opencv @3.0.0_1 (graphics, science)
Variants:             contrib, dc1394, debug, eigen, java, opencl, openni, python27, python34, qt4, qt5, tbb,
                      universal, vtk

Description:          OpenCV is a library that is mainly aimed at real time computer vision. Some example
                      areas would be Human-Computer Interaction (HCI), Object Identification, Segmentation and
                      Recognition, Face Recognition, Gesture Recognition, Motion Tracking, Ego Motion, Motion
                      Understanding, Structure From Motion (SFM), and Mobile Robotics.

Extract Dependencies: unzip
Build Dependencies:   cmake, pkgconfig
Library Dependencies: zlib, bzip2, libpng, jpeg, jasper, tiff, ilmbase, openexr, ffmpeg
Platforms:            darwin
License:              BSD

Usually default install location is /opt/local/. We use the image_external example from this link and the original source code is from


assuming you have the default mathematica installation on the Mac machine.

First we load the LibraryLink and CCompilerDriver


I put a modified version of the source code in the same directory as the working notebook (image_extern2.c contains some type casting of cvCreateImage -> ImageData which returns a char *)

Load the path to the source file:

In[3]:= sourcePath=NotebookDirectory[]
Out[3]= /Users/shenghuiyang/Desktop/opencv_Mathematica/

To build this file, we have 2 options. First, we can use the CreateLibrary function:

"IncludeDirectories" -> {
 "LibraryDirectories" -> {"/opt/local/lib"}, 
 "Libraries" -> { "opencv_core", "opencv_highgui","opencv_imgproc", "raw"},

The key to use this function with Macports's OpenCV (and libra) is to set the proper directories. Use the following command in the terminal to find them

pkg-config --cflags --libs opencv
pkg-config --cflags --libs libraw

One thing to note is that the actual file of these libraries are libopencv_core and so on. You should omit the lib before the file name on OS X. Load the dynamic library with LibraryFunctionLoad function

opencvDilate = 
  "opencv_dilate", {{LibraryDataType[Image], "Constant"}, 
   Integer}, {LibraryDataType[Image]}]

The second method is to use a makefile to build the dynamic library first and use the LibaryFunctionLoad to call the name of the library directly.

CC = clang
TargetLocation = /Users/shenghuiyang/Library/Mathematica/SystemFiles/LibraryResources/MacOSX-x86-64

$(shell rm $(TargetLocation)/image_external.dylib)

OBJECTS =image_external.dylib 


IFLAGS = ...
LIBDIR = -L/opt/local/lib
LIBFLAGS = -lopencv_core -lopencv_highgui -lopencv_imgproc
LIBFLAGS += -lraw -lstdc++ -lstdc++

Target = image_external.dylib

    $(CC) -dynamiclib -o $(TargetLocation)/image_external.dylib $(CFALGS) $(FRAMEWORKFLAG) $(IFLAGS) $(SOURCE) $(LIBDIR) $(LIBFLAGS) 2>&1

How do I know this file works? Well I called CreateLibrary with this option in ShellCommandFunction:

"IncludeDirectories" -> {...},
/usr/bin/clang -dynamiclib -o "/Users/shenghuiyang/Library/Mathematica/SystemFiles/LibraryResources/MacOSX-x86-64/Working-shenghuiy-37929-2049441792-2/image_external.dylib" -m64 -fPIC -O2 -mmacosx-version-min=10.6 -framework Foundation  -I"/Applications/" -I"/Applications/" -I"/opt/local/include" -I"/opt/local/include/opencv" -I"/opt/local/include/opencv2/imgproc" -I"/opt/local/include/libraw" "/Users/shenghuiyang/Desktop/opencv_Mathematica/image_extern2.c"  -F"/Applications/" -L"/opt/local/lib"  -framework "mathlink" -l"opencv_core" -l"opencv_highgui" -l"opencv_imgproc" -l"raw" -lstdc++ 2>&1

You can download the makefile in the attachment. After this makefile is executed, the .dylib is transferred to a member of $LibraryPath automatically. Then we can load this library with


Finally you can use the opencvDilate function in Mathematica directly as the following screenshot. In a very similar way we can load the readRawImage to load the .arw file into Mathematica (about 60MB in size)

enter image description here

POSTED BY: Shenghui Yang
2 years ago

Group Abstract Group Abstract