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

断了今生、忘了曾经 提交于 2019-12-05 00:10:05
JM92

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.

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.

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

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;
    }
}

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.

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