简体   繁体   中英

"assertion failed (m.dims>=2) in Mat" Raspberry Pi OpenCV

I downloaded a project which allows to get frames from Pi camera module with OpenCV. When I try to run the downloaded code, It works without a problem. I just want to apply simple trheshold operation on frames but I got the error shown below.

I check the frames' type and channel. image.channels() returns 1 and image.type() returns 0. I can't see any reason for the threshold operation error.

What is the problem here?

The error:

在此处输入图片说明

The Code:

 #include "cap.h"
 #include <opencv2/opencv.hpp>
 #include <opencv2/highgui/highgui.hpp>

 using namespace cv;
 using namespace std;

 int main() {
 namedWindow("Video");

// Create capture object, similar to VideoCapture
// PiCapture(width, height, color_flag);
// color_flag = true  => color images are captured,
// color_flag = false => greyscale images are captured
PiCapture cap(320, 240, false);

Mat image,binary;
double time = 0;
unsigned int frames = 0;


cout << "Press 'q' to quit" << endl;
while(char(waitKey(1)) != 'q') {
    double t0 = getTickCount();
    image = cap.grab();

std::cout<<image.channels()<< endl;//check for channel
cout<<image.type()<< endl;//check for type
threshold(image,binary,150,255,THRESH_BINARY);//threshold operation

frames++;
    if(!image.empty()) imshow("Hello", image);
    else cout << "Frame dropped" << endl;

    time += (getTickCount() - t0) / getTickFrequency();
    cout << frames / time << " fps" << endl;
}

return 0;
}

Assertion m.ndims >= 2 is to check that the matrix in question is a valid two dimensional image. While you have a conditional to show the image only if it's not empty. But the assertion must be failing before the program reaches that conditional. So you shouldn't be seeing any image window pop up.

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