Jan Hackenberg
Jan Hackenberg

Reputation: 511

Expectation Maximization openCV

I am trying to execute based on example code:

  using namespace cv;
  using namespace cv::ml;
  // mat is a Mat containing the features computed beforehand
  Ptr<EM> model = EM::create();
  model->setClustersNumber(static_cast<int>(m_numberClusters));
  Mat logs, labels, probs;
  model->trainEM(mat, logs, labels, probs);

setClustersNumber gives a segmentation fault, if commented out then trainEM does. Debugging reveals, that those are pure virtual functions, which is in coincidence with OpenCV API.

See here:

enter image description here

Nevertheless I do not find any implementation class having those pure virtual functions nor other examples for Expectation Maximization by OpenCV with 3.x.x versions.

enter image description here

enter image description here

Upvotes: 0

Views: 323

Answers (1)

Mansoor
Mansoor

Reputation: 2438

This runtime error is explained by these other answers 1 & 2.

The EM::Create static method returns a ptr to an abstract type, I think this is a poor API design choice. The intention is likely for you to define a derived type that overrides these abstract methods. Since static methods don't have access to the current object, I don't see what purpose it serves.

It may be the case that your compiler has no way of knowing the dynamic type of the object, which happens to be abstract. The single layer of indirection, because of the pointer, may make it difficult for the compiler to detect this issue. Here is a contrived example (godbolt).

struct a
{
    a(int) {};
    virtual int test() = 0;

    static a* Create() {return (a*)(new int{});};
};

int main()
{
    auto aptr = a::Create();
    aptr->test();
}

Upvotes: 1

Related Questions