Getting matrix_wrap.cpp:1393: error: (-215:Assertion failed)

I am new to C++ and OpenCV and I was trying to write a program to detect aruco codes:

int main() {

    // read image
    cv::Mat imgCopy, img = cv::imread("/tmp/codes.png");

    if (img.empty()) {
        std::cout << "Could not open or find the image" << std::endl;
        return -1;
    }

    // convert to Gray scale before detection
    std::cout << "Converting to Gray scale!" << std::endl;
    cvtColor(img, imgCopy, cv::COLOR_BGR2RGB);

    // detect aruco markers:
    cv::aruco::Dictionary dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_100);
    cv::aruco::DetectorParameters parameters;    
    cv::aruco::ArucoDetector detector(dictionary, parameters);

    std::vector<int> markerIds;
    std::vector<std::vector<cv::Point2f>> markerCorners;

    std::cout << "Calling detector!" << std::endl;
    detector.detectMarkers(imgCopy, markerIds, markerCorners);

    // print detected markers
    std::cout << "Detected markers: " << markerIds.size() << std::endl;
    for (int i = 0; i < markerIds.size(); i++) {
        std::cout << "Marker ID: " << markerIds[i] << std::endl;
        std::cout << "Marker Corners: " << markerCorners[i] << std::endl;
    }

    // draw detected markers
    if (markerIds.size() > 0) cv::aruco::drawDetectedMarkers(img, markerCorners, markerIds);

    cv::imshow("Image", img);

    // wait for a key press to exit
    cv::waitKey(0);
    cv::destroyAllWindows();
    img.release();
    imgCopy.release();

    return 0;
}

I keep getting this error:

Hello, World!
Converting to Gray scale!
Calling detector!
[ INFO:0@0.166] global registry_parallel.impl.hpp:96 ParallelBackendRegistry core(parallel): Enabled backends(3, sorted by priority): ONETBB(1000); TBB(990); OPENMP(980)
OpenCV(4.10.0-dev) Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file /home/rajesh/dev/opencv/modules/core/src/matrix_wrap.cpp, line 1393
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.10.0-dev) /home/rajesh/dev/opencv/modules/core/src/matrix_wrap.cpp:1393: error: (-215:Assertion failed) mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0) in function 'create'

Aborted

Can someone point me to where I am going wrong?

I modified matrix_wrap.cpp to print some of these variables:
std::printf("mtype: %d, type0: %d, cv_mat_mtype: %d, cv_mat_type0: %d, \n", mtype, type0, CV_MAT_CN(mtype), CV_MAT_CN(type0));

Mostly the variables read:
mtype: 12, type0: 12, cv_mat_mtype: 2, cv_mat_type0: 2,
but finally (where the assert happens), I get:
mtype: 13, type0: 4, cv_mat_mtype: 2, cv_mat_type0: 1,

  • not sure what these variables mean or why they are different in this specific case.

Updates:
I thought something might be wrong with the marker file I was using. So I added this function:

#define DICT cv::aruco::DICT_5X5_100
cv::Mat generateMarker(int id) {
    cv::aruco::Dictionary dictionary = cv::aruco::getPredefinedDictionary(DICT);
    cv::Mat marker;
    cv::aruco::generateImageMarker(dictionary, 0, 200, marker, 1);
    return marker;
}

and initializing the img variable as follows:
cv::Mat imgCopy, img = generateMarker(10);

I still end up with same assertion. So something else must be going wrong.

reconsider the use of COLOR_BGR2RGB, if the comment and log line are your goal

hmm, what do you have there ? (z.type(), z.depth() ?)
(expected: a photo of an aruco marker in the wild)

Thank you Sir. I had updated the line to:
cvtColor(img, imgCopy, cv::COLOR_BGR2GRAY);
Even after this change, i get the same error.

It is a simple Aruco marker file for marker 10. As you can see from my earlier update, I dont think the file is the issue because even when I generate the marker and directly pass to the detector, i still get the same exception

and initializing the img variable as follows:
cv::Mat imgCopy, img = generateMarker(10);

Till yesterday I was using Opencv 4.10. I tried with 4.8 and got same result. Today I tried with 4.5.3 and it worked perfectly fine though the code is a bit different.

// For OpenCV version: 4.5.3:
int main() {
    std::cout << "OpenCV version: " << CV_VERSION << std::endl;

    // read image
    cv::Mat imgCopy, img = cv::imread("/tmp/qcodes.png");

    std::cout << "Converting to Gray scale! " << std::endl;
    cvtColor(img, imgCopy, cv::COLOR_BGR2GRAY);
    std::vector<int> ids;
    std::vector<std::vector<cv::Point2f>> corners;
    cv::Ptr<cv::aruco::Dictionary> dictionary =
        cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);

    cv::aruco::detectMarkers(imgCopy, dictionary, corners, ids);
    if (ids.size() > 0) {
        cv::aruco::drawDetectedMarkers(imgCopy, corners, ids);
    }

    cv::imshow("Image", imgCopy);

    cv::waitKey(0);
    cv::destroyAllWindows();
    img.release();
    imgCopy.release();
}

Looks like I interchanged the variables while calling detectMarker. It should have been detector.detectMarkers(imgCopy, markerCorners, markerIds); in my program.

answer on SO: