[英]openCV Error: Assertion failed (scn == 3 || scn == 4) on Raspberry Pi
[英]C++ OpenCV exception Assertion Failed (scn ==3|| scn == 4) in cvt::Color
我當前遇到一個令人討厭的錯誤,我不知道該如何修復自己,該錯誤僅在我開始不使用Visual Studio 2017中的調試模式並發布時才啟動。 我正在嘗試獲取我的openCV代碼,以根據以下示例識別棋盤格角,以幫助校准相機: http : //aishack.in/tutorials/calibrating-undistorting-opencv-oh-yeah/這是我專門編寫的代碼有:
// 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;
}
這是我最終得到的輸出:
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
當我進入發布模式而不是調試時,這才真正成為一個問題,我不知道這里到底發生了什么。 我顯然希望提供一個具體的答案,但是即使只是專門針對此情況的一般指導也將受到贊賞,如果需要更多詳細信息,我很樂意將其添加到本帖子中。 我還是C ++,Visual Studio和OpenCV的新手,因此請記住這一點。
編輯:奇怪的是,它引用的文件路徑也不存在,除非它引用了我的包含路徑中的內容,否則C:/目錄中沒有構建文件夾?
編輯2: OpenCV錯誤的答案:斷言失敗(scn == 3 || scn == 4)在cv :: cvtColor中,文件.. \\ .. \\ .. \\ .. \\ opencv \\ modules \\ imgproc \\ src \\ color.cpp,第3737行沒有解決我的問題,嘗試使用該解決方案會產生新的錯誤:
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
我還應該提到,根據Visual Studio,在所有情況下,該異常都發生在main.cpp的第52行:
bool found = findChessboardCorners(image, board_sz, corners,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
事實證明,在rtsp流中,我試圖從已關閉的IP中獲取視頻流,由於沒有要處理的圖像而導致了異常。 我將其交換到啟動狀態,並驗證了它可用於發布和調試,感謝您的所有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.