Message Boards Message Boards

LibraryLink Example with OpenCV from Macports

Posted 9 years ago

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.
Homepage:             http://opencv.org

Extract Dependencies: unzip
Build Dependencies:   cmake, pkgconfig
Library Dependencies: zlib, bzip2, libpng, jpeg, jasper, tiff, ilmbase, openexr, ffmpeg
Platforms:            darwin
License:              BSD
Maintainers:          stromnov@macports.org, openmaintainer@macports.org

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

/Applications/Mathematica.app/Contents/SystemFiles/Links/LibraryLink/LibraryResources/Source/image_external.c

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

First we load the LibraryLink and CCompilerDriver

Needs["LibraryLink`"]
Needs["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:

lib=CreateLibrary[{FileNameJoin[{sourcePath,"image_extern2.c"}]},"image_external",
"IncludeDirectories" -> {
"/opt/local/include", 
"/opt/local/include/opencv", 
"/opt/local/include/opencv2/imgproc", 
"/opt/local/include/libraw"
}, 
 "LibraryDirectories" -> {"/opt/local/lib"}, 
 "Libraries" -> { "opencv_core", "opencv_highgui","opencv_imgproc", "raw"},
 "Debug"->False
];

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 = 
 LibraryFunctionLoad[lib, 
  "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)

SOURCE=image_extern2.c
OBJECTS =image_external.dylib 

CFLAGS =...

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

Target = image_external.dylib

$(TargetLocation)/image_external.dylib:$(SOURCE)
    $(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:

lib=CreateLibrary[{FileNameJoin[{sourcePath,"image_extern2.c"}]},"image_external",
"IncludeDirectories" -> {...},
"ShellCommandFunction"->Print
];
/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/Mathematica.app/Contents/SystemFiles/IncludeFiles/C" -I"/Applications/Mathematica.app/Contents/SystemFiles/Links/MathLink/DeveloperKit/MacOSX-x86-64/CompilerAdditions" -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/Mathematica.app/Contents/SystemFiles/Links/MathLink/DeveloperKit/MacOSX-x86-64/CompilerAdditions" -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

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

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

Attachments:
POSTED BY: Shenghui Yang
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract