Mex for Opencv 的 groupRectangles

Mex for Opencv's groupRectangles

我正在使用来自 matlab 的 mexopencv,但是我注意到那里的 groupRectangles Matlab 包装器仅支持 3 个输入参数,而源代码有 3 个不同的版本。

我不懂 C++,但我尝试遵循指南和编写的代码,但我无法编译它;它给出了一个特殊的错误。

如果有人可以提供帮助,我将不胜感激,我需要 return 我项目的最终边界框分数。

太棒了!我在网上找到了一个非常相似的问题和答案:

在OpenCV的cascadedetect.cpp中,groupRectangles函数有几种变体: void groupRectangles(std::vector& rectList, int groupThreshold, double eps); void groupRectangles(std::vector& rectList, std::vector& weights, int groupThreshold, double eps); void groupRectangles(std::vector& rectList, std::vector& rejectLevels, std::vector& levelWeights, int groupThreshold, double eps); 但是在 OpenCV 文档中,只有第一个变体被清楚地记录下来,提到了第二个变体,但没有解释 weights 参数。第三个甚至没有提到。

我们想要获得分组矩形的分数,已记录的 groupRectangles 变体无济于事 us.We 必须使用第三个,rejectLevels 设置为零: 矢量水平(wins.size(),0); groupRectangles(胜利、水平、分数、groupThreshold、eps); 其中scores是胜场的分数。它们的大小相同。

所以我一直在尝试使用 -Developing a new MEX function- 以类似于 Kyamagu 的 mexopencv 的方式编写包装器,如此处所述 https://github.com/kyamagu/mexopencv

/**
 * @file groupRectangles.cpp
 * @brief mex interface for groupRectangles //manual 
 * @author Kota Yamaguchi
 * @date 2011
 */
#include "mexopencv.hpp"
using namespace std;
using namespace cv;

template <>
vector<Rect> MxArray::toVector<Rect>() const
{
    vector<Rect> vr;
    if (isNumeric())
        vr.push_back(toRect());
    else if (isCell()) {
        int n = numel();
        vector<MxArray> vm(toVector<MxArray>());
        vr.reserve(n);
        for (int i=0; i<n; ++i)
            vr.push_back(vm[i].toRect());
    }
    else
        mexErrMsgIdAndTxt("mexopencv:error","Invalid input");
    return vr;
}

/*
* edit start
*/

template <>
vector<Scalar> MxArray::toVector<Scalar>() const
{
    vector<Scalar> levels;
    if (isNumeric())
        levels.push_back(toScalar());
    else if (isCell()) {
        int n = numel();
        vector<MxArray> vm(toVector<MxArray>());
        levels.reserve(n);
        for (int i=0; i<n; ++i)
            levels.push_back(vm[i].toScalar());
    }
    else
        mexErrMsgIdAndTxt("mexopencv:error","Invalid input");
    return levels;
}

template <>
vector<Scalar> MxArray::toVector<Scalar>() const
{
    vector<Scalar> scores;
    if (isNumeric())
        scores.push_back(toScalar());
    else if (isCell()) {
        int n = numel();
        vector<MxArray> vm(toVector<MxArray>());
        scores.reserve(n);
        for (int i=0; i<n; ++i)
            scores.push_back(vm[i].toScalar());
    }
    else
        mexErrMsgIdAndTxt("mexopencv:error","Invalid input");
    return scores;
}





/*
* edit end
*/



/**
 * Main entry called from Matlab
 * @param nlhs number of left-hand-side arguments
 * @param plhs pointers to mxArrays in the left-hand-side
 * @param nrhs number of right-hand-side arguments
 * @param prhs pointers to mxArrays in the right-hand-side
 */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] )
{
    // Check the number of arguments
    if (nrhs<2 || (nrhs%2)!=0 || nlhs>1)
        mexErrMsgIdAndTxt("mexopencv:error","Wrong number of arguments");

    // Argument vector
    vector<MxArray> rhs(prhs,prhs+nrhs);
    vector<Rect> rectList(rhs[0].toVector<Rect>());



/*
* edit start
*/

  vector<Scalar> levels(rhs[1].toVector<Scalar>());
  vector<Scalar> scores(rhs[2].toVector<Scalar>());

/*
* edit end
*/

/*
* edited
*/


    int groupThreshold = rhs[3].toInt();
    double eps=0.2;

    for (int i=4; i<nrhs; i+=2) {
        string key(rhs[i].toString());
        if (key=="EPS")
            eps = rhs[i+1].toDouble();
        else
            mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option %s", key.c_str());
    }

    groupRectangles(rectList,levels,scores,groupThreshold,eps);
    plhs[0] = MxArray(rectList);
}

我得到的错误是:

src/+cv/fullgroupRectangles.cpp:52:16: error: redefinition of ‘std::vector<Tp> MxArray::toVector() const [with T = cv::Scalar]’ src/+cv/fullgroupRectangles.cpp:34:16: error: ‘std::vector<Tp> MxArray::toVector() const [with T = cv::Scalar]’ previously declared here src/+cv/fullgroupRectangles.cpp: In function ‘void mexFunction(int, mxArray**, int, const mxArray**)’: src/+cv/fullgroupRectangles.cpp:123:62: error: no matching function for call to ‘groupRectangles(std::vector >&, std::vector >&, std::vector

&, int&, double&)’ src/+cv/fullgroupRectangles.cpp:123:62: note: candidates are: In file included from /usr/local/include/opencv2/opencv.hpp:54:0, from include/MxArray.hpp:14, from include/mexopencv.hpp:14, from src/+cv/fullgroupRectangles.cpp:7: /usr/local/include/opencv2/objdetect/objdetect.hpp:330:17: note: void cv::groupRectangles(std::vector >&, int, double) /usr/local/include/opencv2/objdetect/objdetect.hpp:330:17: note:
candidate expects 3 arguments, 5 provided /usr/local/include/opencv2/objdetect/objdetect.hpp:331:19: note: void cv::groupRectangles(std::vector >&, std::vector&, int, double) /usr/local/include/opencv2/objdetect/objdetect.hpp:331:19: note:
candidate expects 4 arguments, 5 provided In file included from /usr/local/include/opencv2/opencv.hpp:54:0, from include/MxArray.hpp:14, from include/mexopencv.hpp:14, from src/+cv/fullgroupRectangles.cpp:7: /usr/local/include/opencv2/objdetect/objdetect.hpp:332:17: note: void cv::groupRectangles(std::vector >&, int, double, std::vector, std::vector) /usr/local/include/opencv2/objdetect/objdetect.hpp:332:17: note: no known conversion for argument 2 from ‘std::vector ’ to ‘int’ /usr/local/include/opencv2/objdetect/objdetect.hpp:333:17: note: void cv::groupRectangles(std::vector >&, std::vector&, std::vector&, int, double) /usr/local/include/opencv2/objdetect/objdetect.hpp:333:17: note: no known conversion for argument 2 from ‘std::vector ’ to ‘std::vector&’

mex: compile of ' "src/+cv/fullgroupRectangles.cpp"' failed.

make: *** [+cv/fullgroupRectangles.mexa64] 错误 255

非常感谢,谢谢!

groupRectangles() 有 5 个参数,采用矢量级别和矢量分数。只是需要修复。

  /**
   * @file groupRectangles.cpp
   * @brief mex interface for groupRectangles //manual 
   * @author Kota Yamaguchi
   * @date 2011
   */
  #include "mexopencv.hpp"
  using namespace std;
  using namespace cv;


  /*
  * edit end
  */



  /**
   * Main entry called from Matlab
   * @param nlhs number of left-hand-side arguments
   * @param plhs pointers to mxArrays in the left-hand-side
   * @param nrhs number of right-hand-side arguments
   * @param prhs pointers to mxArrays in the right-hand-side
   */
  void mexFunction( int nlhs, mxArray *plhs[],
                    int nrhs, const mxArray *prhs[] )
  {
      // Check the number of arguments
      if (nrhs<2 || (nrhs%2)!=0 || nlhs>1)
          mexErrMsgIdAndTxt("mexopencv:error","Wrong number of arguments");

      // Argument vector
      vector<MxArray> rhs(prhs,prhs+nrhs);
      vector<Rect> rectList(rhs[0].toVector<Rect>());



  /*
  * edit start
  */

    vector<int> levels(rhs[1].toVector<int>());
    vector<double> scores(rhs[2].toVector<double>());

  /*
  * edit end
  */

  /*
  * edited
  */


      int groupThreshold = rhs[3].toInt();
      double eps=0.2;

      for (int i=4; i<nrhs; i+=2) {
          string key(rhs[i].toString());
          if (key=="EPS")
              eps = rhs[i+1].toDouble();
          else
              mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option %s", key.c_str());
      }

      groupRectangles(rectList,levels, scores,groupThreshold,eps);
      plhs[0] = MxArray(rectList);

  }

谢谢,这个returns不过是单实例值。每次我执行返回值都会改变。我相信我需要以某种方式访问​​我从 matlab 发送的所有元素并将它们添加到向量中。