# Getting matrix\_wrap.cpp:1393: error: (-215:Assertion failed)

**URL:** <https://forum.opencv.org/t/getting-matrix-wrap-cpp-error-215-assertion-failed/18451>\
**Category:** C++\
**Tags:** aruco\
**Created:** [August 7, 2024, 4:59pm UTC](https://forum.opencv.org/t/getting-matrix-wrap-cpp-error-215-assertion-failed/18451 "2024-08-07T16:59:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajmb](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@rajmb](https://forum.opencv.org/u/rajmb)\
**Post date:** [August 7, 2024, 4:59pm UTC](https://forum.opencv.org/t/getting-matrix-wrap-cpp-error-215-assertion-failed/18451/1 "2024-08-07T16:59:16Z")

</div>

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

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![rajmb](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@rajmb](https://forum.opencv.org/u/rajmb)\
**Post date:** [August 8, 2024, 8:25am UTC](https://forum.opencv.org/t/getting-matrix-wrap-cpp-error-215-assertion-failed/18451/2 "2024-08-08T08:25:28Z")

</div>

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:

```auto
#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.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 8, 2024, 12:07pm UTC](https://forum.opencv.org/t/getting-matrix-wrap-cpp-error-215-assertion-failed/18451/3 "2024-08-08T12:07:39Z")

</div>

> [@rajmb](#):
>
> ```auto
> // convert to Gray scale before detection
> std::cout << "Converting to Gray scale!" << std::endl;
> cvtColor(img, imgCopy, cv::COLOR_BGR2RGB);
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 8, 2024, 12:56pm UTC](https://forum.opencv.org/t/getting-matrix-wrap-cpp-error-215-assertion-failed/18451/4 "2024-08-08T12:56:05Z")

</div>

> [@rajmb](#):
>
> `img = cv::imread("/tmp/codes.png");`

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

---

<div class="post-metadata">

**Author:** ![rajmb](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@rajmb](https://forum.opencv.org/u/rajmb)\
**Post date:** [August 8, 2024, 1:01pm UTC](https://forum.opencv.org/t/getting-matrix-wrap-cpp-error-215-assertion-failed/18451/5 "2024-08-08T13:01:15Z")

</div>

> [@crackwitz](#):
>
> reconsider the use of `COLOR_BGR2RGB`, if the comment and log line are your goal

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

> [@berak](#):
>
> hmm, what do you have there ? (z.type(), z.depth() ?)

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);`

---

<div class="post-metadata">

**Author:** ![rajmb](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@rajmb](https://forum.opencv.org/u/rajmb)\
**Post date:** [August 9, 2024, 4:28pm UTC](https://forum.opencv.org/t/getting-matrix-wrap-cpp-error-215-assertion-failed/18451/6 "2024-08-09T16:28:31Z")

</div>

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.

```auto
// 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();
}

```

---

<div class="post-metadata">

**Author:** ![rajmb](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@rajmb](https://forum.opencv.org/u/rajmb)\
**Post date:** [August 11, 2024, 10:38am UTC](https://forum.opencv.org/t/getting-matrix-wrap-cpp-error-215-assertion-failed/18451/9 "2024-08-11T10:38:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 11, 2024, 12:04pm UTC](https://forum.opencv.org/t/getting-matrix-wrap-cpp-error-215-assertion-failed/18451/10 "2024-08-11T12:04:58Z")

</div>

answer on SO:

> <https://stackoverflow.com/questions/78856981/assertion-failed-error-when-trying-to-detect-aruco-code-via-opencv-4-8-and-4-10/78857489#78857489>
