# How can I integrate Open-CV with Mathematica?

I would like to run Open-CV functions from within Mathematica, can anyone figure out how to do this?

• Seems like it's been done before on a pc (see here). I'm trying to get it working on a mac... – M.R. Jan 5 '15 at 4:24
• I note some example here in documentation – yode Aug 14 '17 at 2:52

Please take a look at the file image_external.c. that you can find in your installation with the command

FileNames["image_external.c", {\$InstallationDirectory}, Infinity]


Therein, everything you want to know is described. When it comes down to compiling, then you need to ensure you have the development files for opencv and libraw installed and you know the places, where the header-files "cv.h", "highgui.h", "imgproc_c.h" and "libraw.h" are stored.

Then you can compile the image_external.c example with a command similar to

Get["CCompilerDriver"];

CreateLibrary[{"/home/patrick/tmp/ocv/image_external.c"},"imageLib",
"IncludeDirectories" -> { "/usr/include/opencv",
"/usr/include/opencv2/imgproc","/usr/include/libraw"}]


### How-to for Mac OS X

Although the approach for OS X is similar, you have to adapt some things. Most importantly you need to know where the include files and the libraries for opencv and libraw are stored. If you don't have them, then you can install them with e.g. MacPorts. To find this out, there has always been a nice tool on Unix like systems: pkg-config. Here is how you call it in a terminal to get the includes and libraries for opencv:

pkg-config --cflags --libs opencv


Using this command, you can build your final Mathematica call in a minute. Here it is for my machine:

CreateLibrary[{"/Users/patrick/tmp/image_external.c"}, "imageLib",
"IncludeDirectories" -> {"/opt/local/include",
"/opt/local/include/opencv", "/opt/local/include/opencv2/imgproc",
"/opt/local/include/libraw"},
"LibraryDirectories" -> {"/opt/local/lib"},
"Libraries" -> {"opencv_calib3d", "opencv_contrib", "opencv_core",
"opencv_features2d", "opencv_flann", "opencv_gpu",
"opencv_highgui", "opencv_imgproc", "opencv_legacy", "opencv_ml",
"opencv_nonfree", "opencv_objdetect", "opencv_photo",
"opencv_stitching", "opencv_superres", "opencv_ts", "opencv_video",
"opencv_videostab", "raw", "stdc++"}]

• CreateLibrary[...] is giving the error: CreateLibrary::cmperr: Compile error: /Users/mike/Library/Mathematica/SystemFiles/LibraryResources/MacOSX-x86-64/Working-Michaels-MacBook-Pro-28562-2017858304-56/test.c:1:1: error: expected identifier or '(' >> – M.R. Jan 5 '15 at 6:30
• DLLEXPORT is a C-define! It has nothing to do with windows except that has dll in its name. Please look at the file WolframLibrary.h to see how it is defined on Mac. – halirutan Jan 5 '15 at 6:31
• lib = CreateLibrary[ "/Users/mike/tmp/image_external.c" ,"image_external" ,"Language"->"C++" ] – M.R. Jan 5 '15 at 6:34
• What options are required in CreateLibrary for this to work on the mac? I've tried just about every possible combination... Are you using a mac @halirutan? – M.R. Jan 5 '15 at 6:36
• I have included the information of our chat into the answer so that later visitors will find them easily. – halirutan Jan 7 '15 at 9:10

I wanted to make a short comment to the above answer by @halirutan, but had to write an answer due to lack of reputation. In the OS X solution, "Libraries" should include all the libs from "pkg-config" as well as "raw" and "stdc++". Then, it generates the lib charmingly. The following could generate the libs for me:

strConfig = Import["!" <> "/opt/local/bin/pkg-config --cflags --libs opencv", "Text"];
strLib = StringSplit[strConfig][[4 ;;]];
libs = StringDrop[#, 2] & /@ strLib;
Join[libs, {"raw", "stdc++"}]


Results:

{"opencv_shape","opencv_stitching","opencv_objdetect","opencv_superres","opencv_videostab","opencv_calib3d","opencv_features2d","opencv_highgui","opencv_videoio","opencv_imgcodecs","opencv_video","opencv_photo","opencv_ml","opencv_imgproc","opencv_flann","opencv_core","raw","stdc++"}
`