Jack
Jack

Reputation: 723

Opencv stops when aruco::detectMarkers crashes is called

I am trying to track my aruco markers but when I call the detectMarkers() function my application stops and I have absolutely no idea why.

So I am using it like this :

aruco::detectMarkers(colorMat, markerDictionnary, markerCorners, markerIds);

The variables are declared like that :

vector<vector<Point2f>> markerCorners;
Ptr<aruco::Dictionary> markerDictionnary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME::DICT_4X4_50);
vector<int> markerIds;

My colorMat is declared and populated in previous functions so I'm just going to copy every line where it is used:

cv::Mat colorMat;
colorMat = Mat(colorHeight, colorWidth, CV_8UC4, &colorBuffer[0]).clone();
cv::flip(colorMat, colorMat, 1);
cv::imshow("Color", colorMat);

The error I get in my console is:

OpenCV(4.3.0) Error: Assertion failed (_in.type() == CV_8UC1 || _in.type() == CV_8UC3) in cv::aruco::_convertToGrey, file C:\Users\...\Librairies\opencv_contrib-4.3.0\modules\aruco\src\aruco.cpp, line 107
OpenCV(4.3.0) C:\Users\...\Librairies\opencv_contrib-4.3.0\modules\aruco\src\aruco.cpp:107: error: (-215:Assertion failed) _in.type() == CV_8UC1 || _in.type() == CV_8UC3 in function 'cv::aruco::_convertToGrey'

Does anyone know where this error is coming from? Thank in advance!

Upvotes: 0

Views: 2071

Answers (1)

Doch88
Doch88

Reputation: 737

As you see there:

colorMat = Mat(colorHeight, colorWidth, CV_8UC4, &colorBuffer[0]).clone();

You're creating a cv::Mat that has 4 channels, that are the Blue, Red, Green, and alpha channel; so your Mat is holding a BGRA image. Like you see in the error, detectMarkers want either a BGR (or RGB) image (3 channels) or a grey image (1 channel).

So you should convert your image before passing it to detectMarker. A way to do so is, for example:

 cvtColor(colorMat, colorMat, COLOR_BGRA2GRAY);

that convert your image into a grayscale picture.

Upvotes: 3

Related Questions