OpenCV is a powerful image processing library that can make your project much more simple. However, when things go wrong with OpenCV, the errors that are returned are primarily useless without doing some serious digging into their code (which can be tricky in and of itself). I know every case is different, but here are some errors I’ve had, the largely cryptic messages, and how I fixed them.
Note that I’m using OpenCV 2.4.8 on a Macbook Pro with Xcode 5.0.2. I ought to write up some hints on how I got this to happen in the first place, as well…getting OpenCV working with Xcode isn’t really trivial. For now, here’s errors I faced and how I fixed them. My fixes may not be the same as yours, but hopefully they can give some direction if you’re getting the same error message.
I’d be happy to see some similar errors and fixes in the comments, I can always add them to the post itself over time. I intend to update with my own errors as I continue to encounter them. I’m certainly a far, far cry from an OpenCV expert, but if you have other questions please post them, myself or others may be able to help. Hopefully this post can be a good source for lots of fixes. My hope is that this post will appear when OpenCV coders search their error messages on Google, and can get ideas for how to debug.
Function: calibrateCamera
Error Message:
OpenCV Error: Assertion failed (nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total())) in collectCalibrationData, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/calib3d/src/calibration.cpp, line 3164
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/calib3d/src/calibration.cpp:3164: error: (-215) nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total()) in function collectCalibrationData
Problem:
The objectPoints (or 3D points) and imagePoints (or 2D points) vector of vectors (the first two arguments to the function) are not the same length.
Fix:
Turns out I had 33 3D point vectors in my objectPoint vector of vectors, and 32 2D point vectors in my imagePoints vector of vectors (which is the correct amount). Adjust loops that fill these values to make sure they’re both 32. Used a print statement to sanity check the values to be accurate.
Function: calibrateCamera
Error:
OpenCV Error: Assertion failed (ni >= 0) in collectCalibrationData, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/calib3d/src/calibration.cpp, line 3169
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/calib3d/src/calibration.cpp:3169: error: (-215) ni >= 0 in function collectCalibrationData
Problem:
This one left me confused. It had to do with how I was filling my objectPoints (3D points) vector of vectors. I got this error when I tried to fill it by making a single vector of 3D points accurate, then duplicating that vector once within objectPoints for each image I had. In theory, the error tells you that the image count is 0. I confirmed that objectPoints was the correct length, so this is still a confusing error for me. AKA, I don’t really know the exact problem.
Fix:
I tried to make the first vector within objectPoints accurate, then used the vector resize function to duplicate that one vector <number of images> times:
objectPoints.resize(imageCount, objectPoints[0]);
Function: undistort
Error:
OpenCV Error: Assertion failed (dst.data != src.data) in undistort, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/imgproc/src/undistort.cpp, line 173
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/imgproc/src/undistort.cpp:173: error: (-215) dst.data != src.data in function undistort
Problem:
It checks if the source and destination Mats (the first two arguments) are the same; throws this error if they are.
Fix:
In my case, this meant that, like the destination Mat, the source Mat was empty…I had written a wrong pathname to get the image data I wanted to undistort. I just had to fix this pathname so that the source Mat was a valid (non-empty) Mat.
Function: calibrateCamera
Error:
OpenCV Error: One of arguments’ values is out of range (image width and height must be positive) in cvCalibrateCamera2, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/calib3d/src/calibration.cpp, line 1477
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/calib3d/src/calibration.cpp:1477: error: (-211) image width and height must be positive in function cvCalibrateCamera2
Problem:
The image size (the third argument) must have a positive width and height (read: cannot equal 0).
Fix:
I was doing an experiment to see if the “src” Mat I was repeatedly using in my loops to find chessboard corners in a group of images would behave better for me if .release()’d after every loop iteration. As a result, when I .size()’d it in calibrateCamera, it had a size of 0. I changed the src.size() argument to Size(max_col,max_row).
Function: calibrateCamera, findChessboardCorners, etc
Error: No specific message. When attempting to calibrate a stereo camera pair, I was having the oddest issue: running on my professor’s test images, I got imperfect but decent results. Running on my own, everything seemed totally screwed up. I eventually discovered that, for whatever reason, the chessboard corners on my images were being found in the opposite direction from the ones on the sample images.
Problem: The ordering of the found corners seems to depend on the orientation of the chessboard! In my images, the top left square is black…in the sample images I was provided, the top left square is white. Some googling found that others had seen this phenomena. Apparently this isn’t a consistent thing across the board…but it’s a start.
Fix: Create the objectImages 3D points vector of vectors backwards. In the loop that would create these points normally ascending (each “i” or “j” representing width or height being multiplied by the real-life size of each chessboard square), I needed to create them starting from the highest values and working down to 0’s. Incredibly. Annoying.
Function: calibrateCamera
Error:
OpenCV Error: Assertion failed (i < 0) in getMat, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/core/src/matrix.cpp, line 963
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/core/src/matrix.cpp:963: error: (-215) i < 0 in function getMat
Problem:
Arguments rvecs and tvecs are of type Mat.
Fix:
Make them type vector<Mat>.
Function: calibrateCamera
Error:
OpenCV Error: Bad argument (For non-planar calibration rigs the initial intrinsic matrix must be specified) in cvCalibrateCamera2, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/calib3d/src/calibration.cpp, line 1592
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/calib3d/src/calibration.cpp:1592: error: (-5) For non-planar calibration rigs the initial intrinsic matrix must be specified in function cvCalibrateCamera2
Problem:
I’m still not entirely sure what the happenings under the hood are for this problem, but the gist of it is that when you are calibrating the camera (a single camera) with 3D points (this is what is meant by “non-planar calibration rigs.” In other words, the “Z” coordinates in your objectPoints aren’t all equal to zero), there needs to be an initial value for your intrinsic parameters (the intrinsic matrix argument, cameraMatrix).
Fix:
At first I thought this meant cameraMatrix needed to be initialized in some way, even with zeros or something, but that’s not quite the case…you simply need to use the flag CV_CALIB_USE_INTRINSIC_GUESS in the flags argument of the function call. Note that writing any flags in this argument (it’s the last argument of the function call) will overwrite the default set of flags that would be added if none are specified–this has given me problems in the past.
Function: solvePnP, Rodrigues (the same error applied to both in the same way)
Error:
OpenCV Error: Assertion failed (0 <= i && i < (int)vv.size()) in getMat, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/core/src/matrix.cpp, line 977
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/core/src/matrix.cpp:977: error: (-215) 0 <= i && i < (int)vv.size() in function getMat
Problem:
This is essentially the opposite of the “(i < 0)” assertion problem I faced with calibrateCamera; unlike calibrateCamera, solvePnP and Rodrigues only take a single Mat and single vector of Points for the objectPoints, imagePoints, rvecs, and tvecs argument.
Fix:
Just append an index to each of these arguments (ie, rvecs[0]). You may want to iterate through your entire set of parameters, rather than just analyze them for a single image at a time.
Function: cvtColor
Error:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/imgproc/src/color.cpp, line 3737
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtColor
Problem:
The source doesn’t have the right color format that the code (third argument in the function call) indicates it should. In my case, I was attempting to “CV_RGB2GRAY” my Mat image, but I’d already loaded it “CV_LOAD_IMAGE_GRAYSCALE.” I was trying to convert a grayscale image from color to grayscale; so cvtColor expected my source image to be color. This is I believe what is meant by the “scn == 3” thing….somehow saying the source doesn’t have at least 3 color channels.
Fix:
Ensure that your source image is of the right color type to match the conversion process. In my case I brought in my images (when using imread) in color (code CV_LOAD_IMAGE_COLOR); I could also just bring them in in grayscale and skip the conversion entirely.
Function: VideoWriter.open, most any function with parameters
Error:
In this case, I wasn’t getting an error, which was part of the problem. However, the produced video would cause VLC media player to freeze if I attempted to watch it. The video file I attempted to produce was far too small for a video, as well.
Problem:
Opening a VideoWriter is sort of complex; you need the proper codex (argument 2, typically of format such as “CV_FOURCC(‘D’, ‘I’, ‘V’, ‘X’)”), and you need the proper flags (the 5th/final argument). Otherwise your video just turns into a mess.
Fix:
In my case, and I imagine the same is true a good amount of the time, the fix is to leave the 5th argument field (the flags) unfilled. As in, do not put an argument there at all–only have four arguments. Do not put zero. Zero means absolutely no flags; no argument field at all means default flags…and that is likely just fine.
Function: Rect (inside of matchTemplate, though that’s probably irrelevant)
Error:
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/core/src/matrix.cpp, line 323
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/core/src/matrix.cpp:323: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat
Problem:
The region of interest I was attempting to pull out of an image by using Rect extended beyond the bounds of the original image.
Fix:
In my case I was using matchTemplate to try to track features, the Rect was intended to narrow the search window. I calculate the corners of the search window beforehand, and added conditionals to this calculation to bring the corner to an acceptable location on the edge (or appropriately spaced from it where necessary) if it attempted to go beyond it.
Function: findFundamentalMat
Error:
OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/core/src/matrix.cpp, line 1486
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.8/modules/core/src/matrix.cpp:1486: error: (-215) mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0) in function create
Problem:
The status matrix that I was using as the final argument to findFundamentalMat (available because I was using the FM_RANSAC flag) was a vector<int> instead of an OpenCV Mat. Why it is a Mat instead of a simple array is a mystery to me, but a Mat it must be.
Fix:
Make status a Mat, alter functions that utilize the info in status accordingly.