Now it is time to compile promised getFeatures. I do with the following code:
#include <cv.h>
#include <highgui.h>
#include <mat.h>
#include <mex.h>
#include"MxArray.hpp"// https://github.com/kyamagu/mexopencvclassNewRFFeatureGetter : publiccv::RFFeatureGetter
{
public:
NewRFFeatureGetter() : name("NewRFFeatureGetter"){}
virtualvoidgetFeatures(constcv::Mat &src, NChannelsMat &features,
constintgnrmRad, constintgsmthRad,
constintshrink, constintoutNum, constintgradNum) const
{
// here your feature extraction code, the default one is:// resulting features Mat should be n-channels, floating point matrix
}
protected:
cv::Stringname;
};
MEXFUNCTION_LINKAGEvoidmexFunction(intnlhs, mxArray *plhs[], intnrhs, constmxArray *prhs[])
{
if (nlhs != 1) mexErrMsgTxt("nlhs != 1");
if (nrhs != 1) mexErrMsgTxt("nrhs != 1");
cv::Matsrc = MxArray(prhs[0]).toMat();
src.convertTo(src, cv::DataType<float>::type);
std::stringmodelFile = MxArray(prhs[1]).toString();
NewRFFeatureGetter *pDollar = createNewRFFeatureGetter();
cv::Matedges;
pDollar->getFeatures(src, edges, 4, 0, 2, 13, 4);
// you can use other numbers hereedges.convertTo(edges, cv::DataType<double>::type);
plhs[0] = MxArray(edges);
}
Place compiled mex file into root dir and run edgesDemo. You will need to wait a couple of hours after that the new model will appear inside models/forest/.
The final step is converting trained model from Matlab binary format to YAML which you can use with our ocv::StructuredEdgeDetection. For this purpose run opencv_contrib/ximgproc/tutorials/scripts/modelConvert(model, "model.yml")
How to use your model
Just use expanded constructor with above defined class NewRFFeatureGetter