OpenCV错误:matrix.cpp行522断言失败 [英] OpenCV Error: Assertion failed in matrix.cpp line 522
问题描述
我试图运行一段代码进行测试,到目前为止,我遇到了这样的问题:
I was trying to run a piece of code for testing, so far I have a problem like this:
OpenCV错误:断言失败(0< = roi.x&& 0< = roi.width&& roi.x + roi.width< = m.cols&& 0< ; = roi.y&& 0< = roi.height&& roi.y + roi.height< = m.rows)在Mat中,文件/home/diji/opencv-3.2.0/modules /core/src/matrix.cpp,第522行 抛出'cv :: Exception'实例后调用终止 what():/home/diji/opencv-3.2.0/modules/core/src/matrix.cpp:522:错误:(-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 Mat
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 /home/diji/opencv-3.2.0/modules/core/src/matrix.cpp, line 522 terminate called after throwing an instance of 'cv::Exception' what(): /home/diji/opencv-3.2.0/modules/core/src/matrix.cpp:522: 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
我已经粘贴了下面的代码
I have pasted the code below
int main() {
// Set-Up
int houghVote = 200;
string arg = "";
// Set up windows
bool showOriginal = 1;
bool showCanny = 1;
bool showHough = 1;
bool showHoughP = 1;
// Capture Input
string window_name = "Processed Video";
namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
VideoCapture capture("/home/lane-detection-master/test_video/mm.mp4");
if (!capture.isOpened()) // Caputure Camera
{capture.open(atoi(arg.c_str()));}
capture.set(CV_CAP_PROP_POS_MSEC, 100000); //start the video at 100 seconds in
double dWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
std::cout << "Frame Size = " << dWidth << "x" << dHeight << std::endl;
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
// Encode resulting video
// VideoWriter oVideoWriter ("LaneDetection.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true);
// Process Frame
Mat image;
double frameItr = 0;
image = imread(arg);
int crestCount = 0, frameSkip = 0;
while (1)
{
// capture on intervals to make vid smoother
capture >> image;
frameItr += 100;
capture.set(CV_CAP_PROP_POS_MSEC, frameItr);
if (image.empty())
break;
Mat gray;
cvtColor(image,gray,CV_RGB2GRAY);
vector<string> codes;
Mat corners;
//findDataMatrix(gray, codes, corners);
//drawDataMatrixCodes(image, codes, corners);
// ROI
// optimized? -=> yes
int top = 0;
int left = 0;
int width = 800;
int height = 600;
Rect roi(left,top,width,height);
Mat imgROI = image(roi);
Scalar val = Scalar(0, 0, 0);
copyMakeBorder(imgROI, imgROI, 2, 2, 2, 2, BORDER_CONSTANT, val);
// Display the image
if(showOriginal) {
namedWindow("Original Image");
imshow("Original Image",imgROI);
imwrite("original.bmp", imgROI);
}
// Canny algorithm
Mat contours;
Canny(imgROI,contours,100,200);
Mat contoursInv;
threshold(contours,contoursInv,128,255,THRESH_BINARY_INV);
// Display Canny image
if(showCanny) {
namedWindow("Contours");
imshow("Contours1",contours); // use contoursInv for white
imwrite("contours.bmp", contours);
}
/*
Hough tranform for line detection with feedback
Increase by 25 for the next frame if we found some lines.
This is so we don't miss other lines that may crop up in the next frame
but at the same time we don't want to start the feed back loop from scratch.
*/
std::vector<Vec2f> lines;
if (houghVote < 1 or lines.size() > 2) { // we lost all lines. reset
houghVote = 300;
}
else{ houghVote += 25;}
while(lines.size() < 4 && houghVote > 0){
HoughLines(contours,lines,1,PI/180, houghVote);
houghVote -= 5;
}
std::cout << houghVote << "\n";
Mat result(imgROI.size(),CV_8U,Scalar(255));
imgROI.copyTo(result);
// Draw the lines
std::vector<Vec2f>::const_iterator it= lines.begin();
Mat hough(imgROI.size(),CV_8U,Scalar(0));
while (it!=lines.end()) {
float rho= (*it)[0]; // first element is distance rho
float theta= (*it)[1]; // second element is angle theta
if ( (theta > 0.09 && theta < 1.48) || (theta < 3.14 && theta > 1.66) ) { // filter to remove vertical and horizontal lines
// point of intersection of the line with first row
Point pt1(rho/cos(theta),0);
// point of intersection of the line with last row
Point pt2((rho-result.rows*sin(theta))/cos(theta),result.rows);
// draw a line: Color = Scalar(R, G, B), thickness
line( result, pt1, pt2, Scalar(255,255,255), 1);
line( hough, pt1, pt2, Scalar(255,255,255), 1);
}
//std::cout << "line: (" << rho << "," << theta << ")\n";
++it;
}
// Display the detected line image
if(showHough){
namedWindow("Detected Lines with Hough");
imshow("Detected Lines with Hough",result);
imwrite("hough.bmp", result);
}
// Create LineFinder instance
LineFinder ld;
// Set probabilistic Hough parameters
ld.setLineLengthAndGap(10,60); // min accepted length and gap
ld.setMinVote(15); // sit > 3 to get rid of "spiderweb"
// Detect lines
std::vector<Vec4i> li= ld.findLines(contours);
Mat houghP(imgROI.size(),CV_8U,Scalar(0));
ld.setShift(0,0);
ld.drawDetectedLines(houghP);
std::cout << "First Hough" << "\n";
if(showHoughP){
namedWindow("Detected Lines with HoughP");
imshow("Detected Lines with HoughP", houghP);
imwrite("houghP.bmp", houghP);
}
// bitwise AND of the two hough images
bitwise_and(houghP,hough,houghP);
Mat houghPinv(imgROI.size(),CV_8U,Scalar(0));
Mat dst(imgROI.size(),CV_8U,Scalar(0));
threshold(houghP,houghPinv,150,255,THRESH_BINARY_INV); // threshold and invert to black lines
if(showHoughP){
namedWindow("Detected Lines with Bitwise");
imshow("Detected Lines with Bitwise", houghP);
}
Canny(houghPinv,contours,100,350);
li = ld.findLines(contours);
// Test to draw point
//ld.drawPoint(image, Point(320,130));
// Set probabilistic Hough parameters
// more strict than above HoughP
ld.setLineLengthAndGap(5,2);
ld.setMinVote(1);
ld.setShift(top, left);
// draw point on image where line intersection occurs
int yShift = 25;
int allowableFrameSkip = 5;
ld.drawDetectedLines(image);
cv::Point iPnt = ld.drawIntersectionPunto(image, 2);
// track hill crest
int gap = 20;
cv::Point lptl(0, image.rows / 2 + yShift);
cv::Point lptr(gap, image.rows / 2 + yShift);
line(image, lptl, lptr, Scalar(255, 255, 255), 1);// left mid line
cv::Point rptl(image.cols - gap, image.rows / 2 + yShift);
cv::Point rptr(image.cols, image.rows / 2 + yShift);
line(image, rptl, rptr, Scalar(255, 255, 255), 1);// right mid line
cv::Point ulpt(0, image.rows / 2 - 50 + yShift);
cv::Point urpt(image.cols, image.rows / 2 - 50 + yShift);
bool hillCrestFound = (iPnt.y < (image.rows / 2 + yShift)) && (iPnt.y > (image.rows / 2 - 50 + yShift));
if(hillCrestFound) {
crestCount++;
frameSkip = 0;
} else if(crestCount != 0 && frameSkip < allowableFrameSkip)
frameSkip++;
else {
crestCount = 0;
frameSkip = 0;
}
cv::Point txtPt(image.cols / 2 - 31, image.rows / 2 - 140);
if(crestCount > 3)
putText(image, "tracking", txtPt, FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 255), 2, 8);
std::stringstream stream;
stream << "Lines Segments: " << lines.size();
putText(image, stream.str(), Point(10,image.rows-10), 1, 0.8, Scalar(0,255,0),0);
imshow(window_name, image);
imwrite("processed.bmp", image);
// oVideoWriter.write(image); //writer the frame into the file
char key = (char) waitKey(10);
lines.clear();
}
}
我假设与matrix.cpp文件存在冲突,但是无法确切地诊断出问题所在.任何建议将不胜感激!谢谢!
I assume there is a conflict with the matrix.cpp file, however can't diagnose what exactly the problem is. Any suggestion would be greatly appreciated! Thanks!
推荐答案
当未加载Mat
或尝试访问错误的高度或宽度值(例如负值)时,会发生这种情况.这些是常见原因.
This occurs when the Mat
is not loaded or you are trying to access the wrong value of height or width (eg. negative values). Those are the common reasons.
在您的代码中
int width = 800;
int height = 600;
Rect roi(left,top,width,height);
在这里,您正在尝试从捕获的图像中裁剪此矩形.但是捕获图像的宽度或高度可以小于此宽度.图片中的矩形必须包含在内.
Here, you are trying to crop this rectangle from the captured image. But the capture image's width or height can be less than that. The rectangle here must be inclusive in the image.
但是,这是一个很长的源代码.在更多情况下,代码可能会失败.因此,请使用一些调试功能来查找问题所在的位置.
However, this is a long source code. there are more cases where the code can fail. So use some debugging to find where the problem occurs.
注意 I assume there is a conflict with the matrix.cpp file
,这种可能性很小,因此,假设matrix.cpp文件没有冲突.
NOTE I assume there is a conflict with the matrix.cpp file
, this is very much unlikely, so assume that the matrix.cpp file has no conflict.
这篇关于OpenCV错误:matrix.cpp行522断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!