Seastar
Seastar

Reputation: 386

Assertion failed with copyTo function (C++)

As many have before, I have an assertation error problem and am not able to solve it on my own... Here is my code- snippet:

void tuneParameters(cv::Ptr<cv::ml::KNearest>& classifier, cv::Mat& trainDataMat, cv::Mat& trainLabels, int k[], cv::Mat classes){
//Split Data in 10 random folds to do cross validation
    int n = trainDataMat.rows;
    std::vector <int> seeds;
    for (int cont = 0; cont < n; cont++)
        seeds.push_back(cont);
    cv::randShuffle(seeds);

    int n10 = floor(n/10);
    cv::Mat train(n10*9, 3, CV_32F);

    trainDataMat.copyTo(train(Rect(0,0,3,n10*9)));

    ...followed by the parameter tuning step
}

trainDataMat is of this datatype:

cv::Mat trainDataMat(32505, 3, CV_32F);

And when I run, on the line with .copyTo I get the error:

OpenCV Error: Assertion failed (!fixedSize() || ((Mat*)obj)->size.operator()() == Size(_cols, _rows)) in create, file /home/aschuett/cppWS/opencv-3.2.0/modules/core/src/matrix.cpp, line 2287
terminate called after throwing an instance of 'cv::Exception'
what():  /home/aschuett/cppWS/opencv-3.2.0/modules/core/src/matrix.cpp:2287: error: (-215) !fixedSize() || ((Mat*)obj)->size.operator()() == Size(_cols, _rows) in function create

I read through various forum entries, and could not find a solution to my problem... I checked the sizes of my matrices with the debugger, but they are corresponding, trainDataMat has cls:3 and rows: 32505, train has cols:3 and rows:29250.

Can maybe someone else see where I am doing something wrong? May it be in the usage of the Rect() function?

Thanks a lot in advance!! Cheers

Upvotes: 1

Views: 2661

Answers (1)

Seastar
Seastar

Reputation: 386

What worked for me:

cv::Rect r(0,0,3,n10*9);
trainDataMat(r).copyTo(train(r));

So apparently the larger Matrix has to be fitted to the "goal-matrix"-size before copying? Can anyone verify that or is it a different reason and this is just a workaround?;)

Upvotes: 2

Related Questions