Assertion error inside kmeans

I’m getting a consistent error: (-215:Assertion failed) (flags & FIXED_TYPE) != 0 in function ‘cv::debug_build_guard::_InputArray::type’ inside the cv::kmeans function in two different 3.4.* releases ( 3.4.10 and 3.4.16).
I think it’s related to my code, and not an actual implementation error in opencv, but can’t figure out what I do wrong. Please help me find my mistake!
The error is thrown at:
kmeans.cpp:

...
        if (compactness < best_compactness)
        {
            best_compactness = compactness;
            if (_centers.needed())
            {
                if (_centers.fixedType() && _centers.channels() == dims)
                    centers.reshape(dims).copyTo(_centers);
                else
                    centers.copyTo(_centers);
            }
            _labels.copyTo(best_labels); <----here
        }

copy.cpp:

...
    int dtype = _dst.type(); <---here

matrix_wrap.cpp:

...
    if( k == STD_VECTOR_MAT )
    {
        const std::vector<Mat>& vv = *(const std::vector<Mat>*)obj;
        if( vv.empty() )
        {
            CV_Assert((flags & FIXED_TYPE) != 0); <--- here the assertion fails
            return CV_MAT_TYPE(flags);
        }
        CV_Assert( i < (int)vv.size() );
        return vv[i >= 0 ? i : 0].type();
    }

My code calling kmeans:

void MultiGaussEstimation::kmeans_estimation(cv::Mat data_as_rows)
{
	cv::Mat masks;
	std::vector<cv::Mat>means;
	cv::Mat data; data_as_rows.convertTo(data, CV_32FC1);
	data = data.clone();
	cv::kmeans(data, K, masks, cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::COUNT, 10, 1.0), 2, cv::KmeansFlags::KMEANS_PP_CENTERS,means);
....
}

I figured where the error should be:
the means parameter causes this assertion error, if it’s not placed inside the code, it works.
What should be the form of the means parameter to not cause assertion error?

okay, I was thinking loudly on this forum, the means supposed to be a single Mat, which mat’s every row will represent the mean value of a given segment.
it’s clearly stated here in the docs, but for some reason it missed my attention.

2 Likes