简体   繁体   中英

OpenCV(3.4.1) Error: Assertion failed (dims <= 2 && step[0] > 0) in cv::Mat::locateROI

I will post my code and the exception after explaining my issue. So basically, I'm making a program where the end goal is to be able to calibrate a fisheye camera which is then on a rtsp stream from the camera that will be recorded via zoneminder using OpenCV and a lot of the code so far I have gotten from here:

http://aishack.in/tutorials/calibrating-undistorting-opencv-oh-yeah/

But I've just started to notice an exception that has been popping up on line 53 of my code

 bool found = findChessboardCorners(image, board_sz,corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS); 

and I have no idea what exactly is causing it, I'm still new to a lot of what I'm trying to do along with stack overflow in general, so feel free to leave any input you feel might be helpful or ask any questions that may need to be asked.

Also a quick note if you run the code yourself is that stack overflow seems to like to split certain lines up, so keep that in mind along with the fact that you'll need OpenCV.

Code:

// ConsoleApplication2.cpp : Defines the entry point for the console 
application.

#include <opencv2\videoio.hpp>
#include <opencv2\highgui.hpp>
#include <opencv\cv.hpp>
#include <opencv\cv.h>
#include <iostream>
#include <stdio.h>
#include <chrono>
#include <thread>


using namespace cv;
using namespace std;

int main (){
int numBoards = 0;
int numCornersHor;
int numCornersVer;

printf("Enter number of corners along width: ");
scanf_s("%d", &numCornersHor);

printf("Enter number of corners along height: ");
scanf_s("%d", &numCornersVer);

printf("Enter number of boards: ");
scanf_s("%d", &numBoards);

int numSquares = numCornersHor * numCornersVer;
Size board_sz = Size(numCornersHor, numCornersVer);

VideoCapture capture = VideoCapture("rtsp://172.16.127.27:554/mpeg4");

vector<vector<Point3f>> object_points;
vector<vector<Point2f>> image_points;

vector<Point2f> corners;
int successes = 0;

Mat image;
Mat gray_image;
capture >> image;

vector<Point3f> obj;
for (int j = 0; j < numSquares; j++)
    obj.push_back(Point3f(j / numCornersHor, j%numCornersHor, 0.0f));

while (successes < numBoards) {
    this_thread::sleep_for(chrono::milliseconds(100));
    cvtColor(image, gray_image, CV_BGR2GRAY);

    bool found = findChessboardCorners(image, board_sz, corners, 
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);

    if (found) {
        cornerSubPix(gray_image, corners, Size(11,11), Size(-1, -1), 
TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
        drawChessboardCorners(gray_image, board_sz, corners, found);
    }

    imshow("win1", image);
    imshow("win2", gray_image);

    capture >> image;
    int key = waitKey(1);

    if (key == 27)

        return 0;

    if (key == ' ' && found != 0){
        image_points.push_back(corners);
        object_points.push_back(obj);

        printf("Snap stored!");

        successes++;

        if (successes >= numBoards)
            break;
    }
}

Mat intrinsic = Mat(3, 3, CV_32FC1);
Mat distCoeffs;
vector<Mat> rvecs;
vector<Mat> tvecs;

intrinsic.ptr<float>(0)[0] = 1;
intrinsic.ptr<float>(1)[1] = 1;

calibrateCamera(object_points, image_points, image.size(), intrinsic, 
distCoeffs, rvecs, tvecs);

Mat imageUndistorted;
while (1) {
    capture >> image;
    undistort(image, imageUndistorted, intrinsic, distCoeffs);

    imshow("win1", image);
    imshow("win2", imageUndistorted);
    waitKey(1);
}

capture.release();

return 0;
}

Error in Console:

OpenCV(3.4.1) Error: Assertion failed (dims <= 2 && step[0] > 0) in 
cv::Mat::locateROI, file C:\build\master_winpack-build-win64- 
vc15\opencv\modules\core\src\matrix.cpp, line 760

Exception:

Exception thrown at 0x00007FFEFC21A388 in ConsoleApplication2.exe: Microsoft 
C++ exception: cv::Exception at memory location 0x000000637891D640.
Exception thrown at 0x00007FFEFC21A388 in ConsoleApplication2.exe: Microsoft 
C++ exception: [rethrow] at memory location 0x0000000000000000.
Exception thrown at 0x00007FFEFC21A388 in ConsoleApplication2.exe: Microsoft 
C++ exception: cv::Exception at memory location 0x000000637891D640.
Unhandled exception at 0x00007FFEFC21A388 in ConsoleApplication2.exe: 
Microsoft C++ exception: cv::Exception at memory location 0x000000637891D640.

Edit: So, oddly, I'm no longer having this issue, but I'll still leave this question up in case anyone else happens to have this issue or has some insight that may still be relevant, I'm still having an issue where the camera seems to freeze on the first frame it can capture over rtsp despite not having the issue when using the webcam, but I'll leave that for a separate question since this is not the issue here.

What was the last code you added before it started to freeze in the first frame calibration code?

# Reference your sleep statement (which is in the first frame calibration code). I'm assuming you introduced to reduce CPU usage, I would remove it and modify the waitKey() method in the main loop at the bottom to waitKey (100) to wait 100 milliseconds (or less) in between reads from the stream.

  1. The sleep statement where you have it is not where most of your CPU usage is. Your just in the calibration section of the app that is only executed once. I would remove it.

  2. Where your computer really spins is the main while(1) loop at the bottom where it is continually reading from the stream.

  3. That said, you don't actually need to use a sleep statement in the main while loop at the bottom as you call the waitKey() function. Simply change the call to waitKey(100).

# Reference your scanf_s statements: Either convert those to #defines so we all know what your inputs are or show us the command line you entered to run this. Important piece of info.

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM