I've gone through questions, the source code, and other examples, and I can't for the life of me understand what the Distance d = Distance() parameter means in the function
template<typename Distance> int flann::hierarchicalClustering(const Mat& features, Mat& centers, const cvflann::KMeansIndexParams& params, Distance d=Distance())
It's addressed in the comments of this question, but I can't find the Distance type anywhere. I've imported the following files:
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/tracking.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/flann/flann.hpp"
I've checked the cv, cvflann, and cv::flann namespaces and can't find anything.
Here's my code:
int tmp = cv::flann::hierarchicalClustering<cv::L2<float>>(descriptors, centers, params, ______ );
The ___ is obviously where this last parameter goes. I've tried something like cv::L2<float>() but that doesn't work either.
In the source code it looks like a template.
I've also tried:
int tmp = cv::flann::hierarchicalClustering<float, float>(descriptors, centers, params);
and I get the error "no instance of overloaded function "cv::flann::hierachicalClustering" matches the argument list. Argument types are: (cv::Mat, cv::Mat, cv::KMeansIndexParams).
I am using OpenCV 2.4.11.
Any ideas?
There's also more documentation in the FLANN user manual. It looks like it was copied verbatim into OpenCV.
The parameter Distance d = Distance() is a default method argument that sets a distance algorithm. Usually, this will be FLANN_DIST_L2. Why you can't find the Distance "type" anywhere is because it's actually a typename. OpenCV opted to use C++ specialization (via templates) instead of inheritance to make sure that different distance functions have the same traits.
This is a nontrivial subject, so http://www.gotw.ca/publications/mxc++-item-4.htm should provide a fairly painless introduction. If I whet your appetite, check out Andrei Alexandrescu's seminal Modern C++ Design.
Adding this for future reference, since I was just struggling with the OpenCV FLANN classes myself
This worked for me:
// define parameters
cvflann::KMeansIndexParams kmeansParams(10, 100, cvflann::FLANN_CENTERS_KMEANSPP);
// note cvflann - not cv::flann - namespace there
int nClusters = flann::hierarchicalClustering<flann::L2<float>>(samples, centers, kmeansParams );
// note regular flann namespace here
the issue was that marcman was using <cv::L2<float>> instead of <flann::L2<float>>
The flann namespaces are super confusing, and documentation is lacking
and it's a struggle to figure it out in the IDE due to the templating.
Related
I am going through the documentation of Tensorflow on 'Adding a new op'.
I understand how macros work in C++ but I am not able to understand the syntax that has been used.
The code given is:
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/shape_inference.h"
using namespace tensorflow;
REGISTER_OP("ZeroOut")
.Input("to_zero: int32")
.Output("zeroed: int32")
.SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) {
c->set_output(0, c->input(0));
return Status::OK();
});
I am confused with the implementation of 'REGISTER_OP("ZeroOut")' and followed by the '.' operator. (I understand that the '.' operator can be used for accessing members of a class object but not sure why it has been used here.)
Could anyone please tell me if this is just a variation of some implementation of Macros?
Link for TF documentation: https://www.tensorflow.org/extend/adding_an_op
TIA.
If you look at the reference code in op.h that REGISTER_OP macro is in, each of these methods below returns *this, that means the chaining of
.Input
.Output
.SetShapeFn()
is a design pattern called Builder, which is to
provide a flexible solution to separate the construction of a complex object from its representation.
This design pattern is widely used in unit testing as well.
I've encountered to cvUpdateMotionHistory function in the OpenCV. after searches, I understand that this function developed for C language and equivalent function at the C++ is update_mhi. In OpenCV 3 release, cvUpdateMotionHistory function don't work correctly but update_mhi function work correctly. Now, I want to understand the equivalent function's:
cvCalcMotionGradient
cvSegmentMotion
cvCalcGlobalOrientation
what's the equivalent function's?
Some of the OpenCV functions are not only under the cv namespace, but under the module namespace.
Here you can see that the documentation of OpenCV refers to this function as:
cv::motempl::calcGlobalOrientation
If you are using (which I do not recommend) :
using namespace cv;
then you need to call the function with motempl::calcGlobalOrientation if not call it with cv::motempl::calcGlobalOrientation
Don't forget to include:
#include "opencv2/optflow.hpp"
for some examples take a look to the link given by Micka.
I am fairly new to c++ and boost.
I want to create a set of numbers that derived from a skewed distribution using Boost's skewed_normal_distribution class.
I'm not sure how to even start. I want to define a distribution with mean 0.05, variance 0.95, but a skew of 0.5. The following code doesn't work and I realise I need to set up a variate_generator as well.
Can anyone provide some pointers? I am not finding the boost documentation page on skew_normal_distribution very intuitive but that might be because of my inexperience.
Note the main problem I am getting is the message:
‘skew_normal_distribution’ is not a member of ‘boost’
Many thanks
#include <boost/random.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/math/distributions/skew_normal.hpp>
int main () {
boost::skew_normal_distribution<> normal(0.05, 0.95, 0.5);
return 0;
}
Use boost::math::skew_normal_distribution instead of boost::skew_normal_distribution for the message
skew_normal_distribution is not a member of boost
I'm trying to learn boost::ublas but am having some trouble compiling code with the subrange function. As usual, the boost docs don't really seem to shed light on this. Here's what I've done:
#include <boost/numeric/ublas/matrix.hpp>
void DoNothing()
{
boost::numeric::ublas::matrix<double> a(1,2);
boost::numeric::ublas::subrange(a,boost::numeric::ublas::range(0,1),boost::numeric::ublas:range(0,2));
}
Here's the error message I'm getting:
test.cpp:14:5: error: ‘subrange’ is not a member of
‘boost::numeric::ublas’
boost::numeric::ublas::subrange(a,boost::numeric::ublas::range(0,1),boost::numeric::ublas::range(0,2));
^
I'm guessing I haven't included a header file that I need, but I've read through the documentation (http://www.boost.org/doc/libs/1_52_0/libs/numeric/ublas/doc/operations_overview.htm) but can't see anything to suggest which header, if any, I need to add to make this work.
#include <boost/numeric/ublas/matrix_proxy.hpp>
I am trying to calculate the overlap area between two ellipses. I am approximating the ellipses with polygons now and I have found an example that apparently used an old version of Boost.Geometry, as per this answer. From the second answer to this question, I can see that this is an old example as well, since some of the header files are not there in v1.53.
I have replaced those with:
#include <boost/geometry/geometries/adapted/c_array.hpp>
#include <boost/geometry/multi/multi.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
and also added this code:
typedef boost::geometry::model::d2::point_xy<double,
boost::geometry::cs::cartesian> point_2d;
typedef boost::geometry::model::polygon<point_2d> polygon_2d;
and almost everything works. The only problem is with this:
polygon_2d poly, poly2;
typedef std::vector<polygon_2d > polygon_list;
polygon_list v;
intersection_inserter<polygon_2d >(poly2, poly, std::back_inserter(v));
I am getting an error:
intersection_inserter was not declared in this scope
expected primary expression before '>' token
The documentation of boost on the matter here is from 2009, so I guess it does not apply anymore... Their example is written the same as mine, as far as I can tell. I have found the place on the header file intersection.hpp where intersection_inserter is defined but I cannot make heads or tails of it...
I am getting the same error both in VS2012 in win7 and Qt 4.7.4 in Linux Mint 14. Any help would be greatly appreciated!
I cannot find any reference to intersection_inserter in the current boost documentation. Perhaps this functionality has been removed?
It seems that the "official" way to calculate intersections in boost::geometry is through the intersection function, as documented (with example) here