简体   繁体   中英

C++ OpenCV exception Assertion Failed (scn ==3|| scn == 4) in cvt::Color

I'm currently having a nasty bug I don't know how to fix myself that only started up when I started not using the debug mode in Visual Studio 2017 and went to release. I'm trying to get my openCV code to recognize checkerboard corners to help calibrate the camera based off of this example: http://aishack.in/tutorials/calibrating-undistorting-opencv-oh-yeah/ Here is the code I specifically have:

// 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;
}

Here is the output I end up getting:

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 (scn == 3 || scn == 4) in cv::cvtColor, 
file C:\build\master_winpack-build-win64- 
vc15\opencv\modules\imgproc\src\color.cpp, line 11147

This only really started being an issue when I went to the release mode instead of debug and I don't know what exactly is going on here. I would like a specific answer obviously, but even just a general direction that specifically applies to this case would be much appreciated, if any more details are needed, I'll gladly add them to this post. I'm still new to C++, Visual Studio, and OpenCV, so please keep that in mind.

Edit: it's odd how it's referencing a file path that doesn't exist too, there's no build folder in the C:/ directory unless it's referencing something in my include path maybe?

Edit 2: The answer for OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv:: cvtColor, file ..\\..\\..\\..\\opencv\\modules\\imgproc\\src\\color.cpp, line 3737 did not solve my problem, trying that solution yields new errors:

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

I should also mention that the exception in all cases according to visual studio happens on line 52 of my main.cpp:

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

It turns out in the rtsp stream, I was trying to fetch a video stream from an IP that was down, causing the exception since there was no image to process. I swapped it to one that was up and verified it worked on release and debug, thanks for all your help!

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