6

Since OpenCV provide C++ API,and the Mathemtica provide so many method to connect C++. I think we can call it with our Mathematica. The OpenCV provide many powerfull function for image-processing. It can help us a lot.

As the Szabolcs here,Mathematica provide such directory,maybe contain a ready-made link in

FileNameJoin[{$TopDirectory, "SystemFiles", "Links", "OpenCVLink"}]

And I can use some function with one argument,such as OpenCVLinkPrivate$Dilation

<< OpenCVLink`
img = ExampleData[{"TestImage", "Lena"}];
OpenCVLink`Private`$Dilation[img, 2]

But I don't know how to use other function such as OpenCVLink`Private`$HoughCircles or stitcher.stitch in opencv_stitching310.dll for this question. And I don't think this is a right way to call it since it include a Private`.Can anyone can tell me more?

yode
  • 26,686
  • 4
  • 62
  • 167
  • It doesn't seem like there are any public functions there. But here's a way to find all of the LibraryFunction: ``Pick[Names["OpenCVLinkPrivate*"], MatchQ[{_ :> _LibraryFunction}] /@

    ToExpression[Names["OpenCVLinkPrivate*"], StandardForm, OwnValues] ]``. This listing seems reasonable.

    – b3m2a1 Sep 05 '17 at 13:39
  • @b3m2a1 Thanks,but I think since the context contain Private.I think it is not a open solution.. – yode Sep 05 '17 at 13:58
  • The private simply means it's free to change at whim. But that's the case with all the undocumented functionality we use. Core OpenCVLink is distributed in the Links directory, not as a paclet, so it should be stable at least within versions and, probably, between point releases. – b3m2a1 Sep 05 '17 at 14:00
  • @b3m2a1 These file name show this OpenCVLink include a complete 3.1.0 version.Your method just show a part of the opencv_core310.dll I think.. – yode Sep 05 '17 at 14:20
  • Look at OpenCVLink`Private`InitOpenCVLink. It shows you how you can load new functions from the links (i.e. through safeLibraryFunctionLoad after sticking all the library files on the LibraryPath). The ones I provided are the ones they load by default. – b3m2a1 Sep 05 '17 at 14:25
  • 1
    From Mathematica, you would only be able to call the library functions as listed in InitOpenCVLink as pointed out by b3m2a1. There is no LibraryLink interface implemented for every single OpenCV function. As for how to call $HoughCircles, it shouldn't be difficult to figure out from the OpenCV documentation what is the meaning of the four real and two integer parameters: dp, minDist, param1, param2, minRadius and maxRadius in that order. – ilian Sep 06 '17 at 22:56
  • 1
    @b3m2a1 The fact that it is located under Links does not mean it cannot be paclet-updated if desired -- the paclet manager would simply use the highest version available, which could be a downloaded one. – ilian Sep 07 '17 at 03:00
  • @ilian Of course, but I figure an update would be unlikely to remove functionality altogether. The link won't disappear arbitrarily is what I was getting at. – b3m2a1 Sep 07 '17 at 03:01
  • @ilian I think our Mathematica can load any function in a dll file before this.And I hope so. – yode Sep 07 '17 at 03:13

1 Answers1

4

Here's an example of something you can do with this. Not having seen ilian's comment, all I did was accessed the private LibraryFunction, looked at its parameters, looked at the docs and guessed from there at the parameters.

houghCircles[img_,
  dp : _?NumericQ : 5,
  minDist : _?NumericQ | Scaled[_?NumericQ] : Scaled[.25],
  cannyThreshold : _?NumericQ : 100,
  accumulatorThreshold : _?NumericQ : 100,
  minRadius : _Integer : 0,
  maxRadius : _Integer : 0
  ] :=
 (
  Needs["OpenCVLink`"];
  Partition[
   OpenCVLink`Private`$HoughCircles[
    Image[
     First@ColorSeparate@
       ColorConvert[img, "Grayscale"],
     "Byte"
     ],
    dp,
    Replace[minDist,
     Scaled[i_] :> ImageDimensions[img][[2]]*i
     ],
    cannyThreshold, accumulatorThreshold,
    minRadius, maxRadius
    ],
   3
   ]
  )

imageHoughCircleAnnotate[
  img_,
  houghParams___
  ] :=
 With[{hCs = houghCircles[img, houghParams]},
  Graphics[
   {
    Inset[Image[img, ImageSize -> ImageDimensions[img]], Center, 
     Center, ImageDimensions[img]],
    {
       {Green, Circle[Most[#], 3]},
       {Red, Circle[Most[#], Last[#]]}
       } & /@ hCs
    },
   ImageSize -> ImageDimensions[img],
   PlotRange -> Thread[{0, ImageDimensions[img]}],
   AspectRatio -> Full
   ]
  ]

Beware that I'm pretty sure my annotation function is garbage though. I just did this to see how it worked:

img = Import["https://i.stack.imgur.com/qV2Hz.jpg"];

imageHoughCircleAnnotate[img]

garbage output

I'm sure with a bit more doc reading it could be made not garbage though

b3m2a1
  • 46,870
  • 3
  • 92
  • 239
  • 1
    garbage indeed.Sorry for laugh about that result image. :) – yode Sep 16 '17 at 08:29
  • @yode I'm not offended at all. I think that function took a total of 5 minutes to spit out, including docs reading. – b3m2a1 Sep 16 '17 at 08:30