2
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ It doesn't seem like there are any public functions there. But here's a way to find all of the LibraryFunction: Pick[Names["OpenCVLink`Private`*"], MatchQ[{_ :> _LibraryFunction}] /@ ToExpression[Names["OpenCVLink`Private`*"], StandardForm, OwnValues] ]. This listing seems reasonable. $\endgroup$ – b3m2a1 Sep 5 '17 at 13:39
  • $\begingroup$ @b3m2a1 Thanks,but I think since the context contain Private.I think it is not a open solution.. $\endgroup$ – yode Sep 5 '17 at 13:58
  • $\begingroup$ 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. $\endgroup$ – b3m2a1 Sep 5 '17 at 14:00
  • 1
    $\begingroup$ 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. $\endgroup$ – ilian Sep 6 '17 at 22:56
  • 1
    $\begingroup$ @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. $\endgroup$ – ilian Sep 7 '17 at 3:00
3
$\begingroup$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.