C++ C++;cvt::Color中的OpenCV异常断言失败(scn==3 | | scn==4)

C++ C++;cvt::Color中的OpenCV异常断言失败(scn==3 | | scn==4),c++,visual-studio,opencv,console-application,C++,Visual Studio,Opencv,Console Application,我目前有一个讨厌的bug,我不知道如何修复自己,它只是在我在Visual Studio 2017中开始不使用调试模式并发布时启动的。我正试图让我的openCV代码识别棋盘格角点,以帮助根据以下示例校准相机:以下是我专门拥有的代码: // ConsoleApplication2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <opencv2\videoi

我目前有一个讨厌的bug,我不知道如何修复自己,它只是在我在Visual Studio 2017中开始不使用调试模式并发布时启动的。我正试图让我的openCV代码识别棋盘格角点,以帮助根据以下示例校准相机:以下是我专门拥有的代码:

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

#include "stdafx.h"
#include <opencv2\videoio.hpp>
#include <opencv2\highgui.hpp>
#include <opencv\cv.hpp>
#include <opencv\cv.h>
#include <iostream>
#include <stdio.h>


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.28: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) {
    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;
}
当我进入发布模式而不是调试时,这才真正成为一个问题,我不知道这里到底发生了什么。很明显,我想要一个具体的答案,但即使只是一个具体适用于这个案例的总体方向,我也会非常感激,如果需要更多的细节,我很乐意将它们添加到这个帖子中。我对C++、Visual Studio和OpenCV仍然是新的,所以请记住这一点。 编辑:它引用一个不存在的文件路径的方式很奇怪,C:/目录中没有构建文件夹,除非它引用的是我的include路径中的某个东西

编辑2:的答案没有解决我的问题,尝试该解决方案会产生新的错误:

warning: Error opening file 
(/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:834)
warning: rtsp://172.16.127.28:554/mpeg4 
(/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:835)
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

事实证明,在rtsp流中,我试图从一个关闭的IP获取视频流,这导致了异常,因为没有要处理的图像。我把它换成了一个已经启动的版本,并在发布和调试时验证了它的有效性,谢谢你们的帮助

在尝试使用图像之前,是否可以测试并打印
image.empty()
image.channels
?codekaizer读取我的编辑;米卡,由于这个节目的性质,我不太明白它是如何工作的,我不认为它将永远适用于空图像和什么是image.channels以及我如何使用它?
warning: Error opening file 
(/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:834)
warning: rtsp://172.16.127.28:554/mpeg4 
(/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:835)
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
bool found = findChessboardCorners(image, board_sz, corners, 
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);