I would like to run Open-CV functions from within Mathematica, can anyone figure out how to do this?
-
2$\begingroup$ Seems like it's been done before on a pc (see here). I'm trying to get it working on a mac... $\endgroup$– M.R.Commented Jan 5, 2015 at 4:24
-
$\begingroup$ I note some example here in documentation $\endgroup$– yodeCommented Aug 14, 2017 at 2:52
2 Answers
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++"}]
-
$\begingroup$ 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 '(' >> $\endgroup$– M.R.Commented Jan 5, 2015 at 6:30
-
$\begingroup$
DLLEXPORT
is a C-define! It has nothing to do with windows except that has dll in its name. Please look at the fileWolframLibrary.h
to see how it is defined on Mac. $\endgroup$ Commented Jan 5, 2015 at 6:31 -
$\begingroup$ lib = CreateLibrary[ "/Users/mike/tmp/image_external.c" ,"image_external" ,"Language"->"C++" ] $\endgroup$– M.R.Commented Jan 5, 2015 at 6:34
-
$\begingroup$ 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? $\endgroup$– M.R.Commented Jan 5, 2015 at 6:36
-
1$\begingroup$ I have included the information of our chat into the answer so that later visitors will find them easily. $\endgroup$ Commented Jan 7, 2015 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++"}