How to determine if a cv::Mat is a zero matrix?

心不动则不痛 提交于 2019-12-10 02:12:16

问题


I have a matrix that is dynamically being changed according to the following code;

 for( It=all_frames.begin(); It != all_frames.end(); ++It)
{
    ItTemp = *It;

    subtract(ItTemp, Base, NewData);

    cout << "The size of the new data for ";
    cout << " is \n" << NewData.rows << "x" << NewData.cols << endl;
    cout << "The New Data is: \n" << NewData << endl << endl;

    NewData_Vector.push_back(NewData.clone());

}

What I want to do is determine the frames at which the cv::Mat NewData is a zero matrix. I've tried comparing it to a zero matrix that is of the same size, using both the cv::compare() function and simple operators (i.e NewData == NoData), but I can't even compile the program.

Is there a simple way of determining when a cv::Mat is populated by zeroes?


回答1:


I used

if (countNonZero(NewData) < 1) 
{
    cout << "Eye contact occurs in this frame" << endl;
}

This is a pretty simple (if perhaps not the most elegant) way of doing it.




回答2:


To check the mat if is empty, use empty(), if NewData is a cv::Mat, NewData.empty() returns true if there's no element in NewData.

To check if it's all zero, simply, NewData == Mat::zeros(NewData.size(), NewData.type()).

Update:

After checking the OpenCV source code, you can actually do NewData == 0 to check all element is equal to 0.




回答3:


countNonZero(Mat ) will give u number of non zeros in mat




回答4:


How about this..

Mat img = Mat::zeros(cvSize(1024, 1024), CV_8UC3);
bool flag = true;

MatConstIterator_<double> it = img.begin<double>();
MatConstIterator_<double> it_end = img.end<double>();
for(; it != it_end; ++it)
{
    if(*it != 0)
    {
        flag = false;
        break;
    }
}



回答5:


The Mat object has an empty property, so you can just ask Mat to tell you if it has something or it's empty. The result will be either true or false.



来源:https://stackoverflow.com/questions/13907574/how-to-determine-if-a-cvmat-is-a-zero-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!