Im trying to count traffic with a live stream video of a ip camera in my raspberry pi 3 . One minute after running my programm this error comes out.
OpenCV Error: Assertion failed (dims <= 2 && step[0] > 0) in locateROI, file /home/pi/opencv/opencv-3.3.0/modules/core/src/matrix.cpp, line 981
terminate called after throwing an instance of 'cv::Exception'
what(): /home/pi/opencv/opencv-3.3.0/modules/core/src/matrix.cpp:981: error: (-215) dims <= 2 && step[0] > 0 in function locateROI
Abortado
This happen everytime I run it. Also when I run a code just to display the live video some times, this comes out.
[h264 @ 0x1f34000] left block unavailable for requested intra mode at 0 40
[h264 @ 0x1f34000] error while decoding MB 0 40, bytestream 0
I'm using a code I found to count cars that works pretty good with videos, but when I set a video stream instead, it crashes. I'm using C++
Actually I'm new on OpenCV and C++, so if anybody can help me understand what am I doing wrong, and why my programm works well just with videos and not with stream videos, I'll apreciate
int main(void) {
cv::VideoCapture capVideo;
cv::Mat imgFrame1;
cv::Mat imgFrame2;
std::vector<Blob> blobs;
cv::Point crossingLine[2];
capVideo.open("rtsp://myipcamera:554/11");
if (!capVideo.isOpened()) { // if unable to open video file
std::cout << "error reading video file" << std::endl << std::endl; // show error message
_getch(); // it may be necessary to change or remove this line if not using Windows
return(0); // and exit program
}
if (capVideo.get(CV_CAP_PROP_FRAME_COUNT) < 2) {
std::cout << "error: video file must have at least two frames";
_getch(); // it may be necessary to change or remove this line if not using Windows
return(0);
}
capVideo.read(imgFrame1);
capVideo.read(imgFrame2);
//Puntos para trazar la linea sensor
/*
*/
int intHorizontalLinePosition = (int)std::round((double)imgFrame1.cols * 0.55);
crossingLine[0].x = intHorizontalLinePosition;
crossingLine[0].y = 0;
crossingLine[1].x = intHorizontalLinePosition;
crossingLine[1].y = imgFrame1.rows - 1;
char chCheckForEscKey = 0;
bool blnFirstFrame = true;
int frameCount = 2;
//El loop que se realice mientras el video esté abierto o mientras no presionen esc
while (capVideo.isOpened() && chCheckForEscKey != 27) {
std::vector<Blob> currentFrameBlobs;
cv::Mat imgFrame1Copy = imgFrame1.clone();
cv::Mat imgFrame2Copy = imgFrame2.clone();
cv::Mat imgDifference;
cv::Mat imgThresh;
cv::cvtColor(imgFrame1Copy, imgFrame1Copy, CV_BGR2GRAY);
cv::cvtColor(imgFrame2Copy, imgFrame2Copy, CV_BGR2GRAY);
cv::GaussianBlur(imgFrame1Copy, imgFrame1Copy, cv::Size(5, 5), 0);
cv::GaussianBlur(imgFrame2Copy, imgFrame2Copy, cv::Size(5, 5), 0);
cv::absdiff(imgFrame1Copy, imgFrame2Copy, imgDifference);
cv::threshold(imgDifference, imgThresh, 30, 255.0, CV_THRESH_BINARY);
// cv::namedWindow("imgThresh", CV_WINDOW_AUTOSIZE); // Create a window for display.
cv::imshow("imgThresh", imgThresh);
cv::Mat structuringElement3x3 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::Mat structuringElement5x5 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
cv::Mat structuringElement7x7 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(7, 7));
cv::Mat structuringElement15x15 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(15, 15));
for (unsigned int i = 0; i < 2; i++) {
cv::dilate(imgThresh, imgThresh, structuringElement5x5);
cv::dilate(imgThresh, imgThresh, structuringElement5x5);
cv::erode(imgThresh, imgThresh, structuringElement5x5);
}
cv::Mat imgThreshCopy = imgThresh.clone();
std::vector<std::vector<cv::Point> > contours;
cv::findContours(imgThreshCopy, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
drawAndShowContours(imgThresh.size(), contours, "imgContours");
std::vector<std::vector<cv::Point> > convexHulls(contours.size());
for (unsigned int i = 0; i < contours.size(); i++) {
cv::convexHull(contours[i], convexHulls[i]);
}
drawAndShowContours(imgThresh.size(), convexHulls, "imgConvexHulls");
identifyMovilObject(currentFrameBlobs, imgThresh, convexHulls);
drawAndShowContours(imgThresh.size(), currentFrameBlobs, "imgCurrentFrameBlobs");
if (blnFirstFrame == true) {
for (auto ¤tFrameBlob : currentFrameBlobs) {
blobs.push_back(currentFrameBlob);
}
}
else {
matchCurrentFrameBlobsToExistingBlobs(blobs, currentFrameBlobs);
}
drawAndShowContours(imgThresh.size(), blobs, "imgBlobs");
imgFrame2Copy = imgFrame2.clone(); // get another copy of frame 2 since we changed the previous frame 2 copy in the processing above
drawBlobInfoOnImage(blobs, imgFrame2Copy);
bool blnAtLeastOneBlobCrossedTheLine = checkIfBigCarBlobsCrossedTheLine(blobs, intHorizontalLinePosition, bigCount);
blnAtLeastOneBlobCrossedTheLine = checkIfCarBlobsCrossedTheLine(blobs, intHorizontalLinePosition, carCount) || blnAtLeastOneBlobCrossedTheLine;
blnAtLeastOneBlobCrossedTheLine = checkIfMotoBlobsCrossedTheLine(blobs, intHorizontalLinePosition, motoCount) || blnAtLeastOneBlobCrossedTheLine;
if (blnAtLeastOneBlobCrossedTheLine == true) {
cv::line(imgFrame2Copy, crossingLine[0], crossingLine[1], SCALAR_GREEN, 2); //Linea separadora cuando pasa un carro
}
else {cv::line(imgFrame2Copy, crossingLine[0], crossingLine[1], SCALAR_RED, 2);} //Linea separadora
drawCarCountOnImage(imgFrame2Copy);
cv::imshow("imgFrame2Copy", imgFrame2Copy);
//cv::waitKey(0); // uncomment this line to go frame by frame for debugging
// now we prepare for the next iteration
currentFrameBlobs.clear();
imgFrame1 = imgFrame2.clone(); // move frame 1 up to where frame 2 is
if ((capVideo.get(CV_CAP_PROP_POS_FRAMES) + 1) < capVideo.get(CV_CAP_PROP_FRAME_COUNT)) {
capVideo.read(imgFrame2);
}
else {
std::cout << "end of video\n";
break;
}
blnFirstFrame = false;
frameCount++;
chCheckForEscKey = cv::waitKey(1);
}
if (chCheckForEscKey != 27) { // if the user did not press esc (i.e. we reached the end of the video)
cv::waitKey(0); // hold the windows open to allow the "end of video" message to show
}
// note that if the user did press esc, we don't need to hold the windows open, we can simply let the program end which will close the windows
return(0);
}