cv::Mat_<double> invpoly = cv::Mat::zeros(12, 1, CV_64F);should be cv::Mat_<double> invpoly = cv::Mat::zeros(nrinvpol, 1, CV_64F);