Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can threshold your image with a relative low threshold and than count the non zero pixels, which should be under a threshold because of bad pixels and camera noise. Example:

threshold(imgIn, imgOut, 5, 255, THRESH_BINARY);
if(countNonZero(imgOut) < maxNonZeroPixelsInBlackImage)
    cout << "Image is a black one!" << endl;

Or you make an inverse threshold, than findContours and if the area of your found contour is nearly as big as the Image area it is a black one. Remember noise and bad pixels so it must be a bit smaller

vector<Mat> Contours;
threshold(imgIn, imgOut, 5, 255, THRESH_BINARY_INV);
findContours(imgOut, Contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
int nIdx = 0;
// If more than one contour was found choose the biggest one
if(Contours.size() > 1)
    int nMaxContour = -1;
    for(int i = 0; i < Contours.size(); i++) {
        if(nMaxContour < Contours[i].rows) {
            nMaxContour = Contours.rows;
            nIdx = i;
        }
    }
if(contourArea(Contours[nIdx] > imgIn.total()*0.99)
    cout << "Image is a black one!" << endl;

The first one should be the easier method, the second one should be more flexible, if just a part of the image has to be black or if you search a special area it can be adjusted. Check out: contours in OpenCV

You can threshold your image with a relative low threshold and than count the non zero pixels, which should be under a threshold because of bad pixels and camera noise. Example:

threshold(imgIn, imgOut, 5, 255, THRESH_BINARY);
if(countNonZero(imgOut) < maxNonZeroPixelsInBlackImage)
    cout << "Image is a black one!" << endl;

Or you make an inverse threshold, than findContours and if the area of your found contour is nearly as big as the Image area it is a black one. Remember noise and bad pixels so it must be a bit smaller

vector<Mat> Contours;
threshold(imgIn, imgOut, 5, 255, THRESH_BINARY_INV);
findContours(imgOut, Contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
int nIdx = 0;
// If more than one contour was found choose the biggest one
if(Contours.size() > 1)
    int nMaxContour = -1;
    for(int i = 0; i < Contours.size(); i++) {
        if(nMaxContour < Contours[i].rows) {
            nMaxContour = Contours.rows;
Contours[i].rows;
            nIdx = i;
        }
    }
if(contourArea(Contours[nIdx] > imgIn.total()*0.99)
    cout << "Image is a black one!" << endl;

The first one should be the easier method, the second one should be more flexible, if just a part of the image has to be black or if you search a special area it can be adjusted. Check out: contours in OpenCV

You can threshold your image with a relative low threshold and than count the non zero pixels, which should be under a threshold because of bad pixels and camera noise. Example:

threshold(imgIn, imgOut, 5, 255, THRESH_BINARY);
if(countNonZero(imgOut) < maxNonZeroPixelsInBlackImage)
    cout << "Image is a black one!" << endl;

Or you make an inverse threshold, than findContours and if the area of your found contour is nearly as big as the Image area it is a black one. Remember noise and bad pixels so it must be a bit smaller

vector<Mat> Contours;
threshold(imgIn, imgOut, 5, 255, THRESH_BINARY_INV);
findContours(imgOut, Contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
int nIdx = 0;
// If more than one contour was found choose the biggest one
if(Contours.size() > 1)
1) {
    int nMaxContour = -1;
    for(int i = 0; i < Contours.size(); i++) {
        if(nMaxContour < Contours[i].rows) {
            nMaxContour = Contours[i].rows;
            nIdx = i;
        }
    }
}
if(contourArea(Contours[nIdx] > imgIn.total()*0.99)
    cout << "Image is a black one!" << endl;

The first one should be the easier method, the second one should be more flexible, if just a part of the image has to be black or if you search a special area it can be adjusted. Check out: contours in OpenCV

You can threshold your image with a relative low threshold and than count the non zero pixels, which should be under a threshold because of bad pixels and camera noise. Example:

threshold(imgIn, imgOut, 5, 255, THRESH_BINARY);
if(countNonZero(imgOut) < maxNonZeroPixelsInBlackImage)
    cout << "Image is a black one!" << endl;

Or you make an inverse threshold, than findContours and if the area of your found contour is nearly as big as the Image image area it is a black one. Remember noise and bad pixels so it must be a bit smaller

vector<Mat> Contours;
threshold(imgIn, imgOut, 5, 255, THRESH_BINARY_INV);
findContours(imgOut, Contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
int nIdx = 0;
// If more than one contour was found choose the biggest one
if(Contours.size() > 1) {
    int nMaxContour = -1;
    for(int i = 0; i < Contours.size(); i++) {
        if(nMaxContour < Contours[i].rows) {
            nMaxContour = Contours[i].rows;
            nIdx = i;
        }
    }
}
if(contourArea(Contours[nIdx] > imgIn.total()*0.99)
    cout << "Image is a black one!" << endl;

The first one should be the easier method, the second one should be more flexible, if just a part of the image has to be black or if you search a special area it can be adjusted. Check out: contours in OpenCV

You can threshold your image with a relative low threshold and than count the non zero pixels, which should be under a threshold because of bad pixels and camera noise. Example:

threshold(imgIn, imgOut, 5, 255, THRESH_BINARY);
if(countNonZero(imgOut) < maxNonZeroPixelsInBlackImage)
    cout << "Image is a black one!" << endl;

Or you make an inverse threshold, than findContours and if the area of your found contour is nearly as big as the image area it is a black one. Remember noise and bad pixels so it must be a bit smaller

vector<Mat> Contours;
threshold(imgIn, imgOut, 5, 255, THRESH_BINARY_INV);
findContours(imgOut, Contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
int nIdx = 0;
// If more than one contour was found choose the biggest one
if(Contours.size() > 1) {
    int nMaxContour = -1;
    for(int i = 0; i < Contours.size(); i++) {
        if(nMaxContour < Contours[i].rows) if(contourArea(Contours[nIdx] > imgIn.total()*0.99) {
            nMaxContour = Contours[i].rows;
            nIdx = i;
        }
cout << "Image is a black one!" << endl;
        break;
    }
}
if(contourArea(Contours[nIdx] > imgIn.total()*0.99)
    cout << "Image is a black one!" << endl;

The first one should be the easier method, the second one should be more flexible, if just a part of the image has to be black or if you search a special area it can be adjusted. Check out: contours in OpenCV

You can threshold your image with a relative low threshold and than count the non zero pixels, which should be under a threshold because of bad pixels and camera noise. Example:

threshold(imgIn, imgOut, 5, 255, THRESH_BINARY);
if(countNonZero(imgOut) < maxNonZeroPixelsInBlackImage)
    cout << "Image is a black one!" << endl;

Or you make an inverse threshold, than findContours and if the area of your found contour is nearly as big as the image area it is a black one. Remember noise and bad pixels so it must be a bit smaller

vector<Mat> Contours;
threshold(imgIn, imgOut, 5, 255, THRESH_BINARY_INV);
findContours(imgOut, Contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
for(int i = 0; i < Contours.size(); i++) {
    if(contourArea(Contours[nIdx] if(contourArea(Contours[i] > imgIn.total()*0.99) {
        cout << "Image is a black one!" << endl;
        break;
    }
}

The first one should be the easier method, the second one should be more flexible, if just a part of the image has to be black or if you search a special area it can be adjusted. Check out: contours in OpenCV

You can threshold your image with a relative low threshold and than count the non zero pixels, which should be under a threshold because of bad pixels and camera noise. Example:

threshold(imgIn, imgOut, 5, 255, THRESH_BINARY);
if(countNonZero(imgOut) < maxNonZeroPixelsInBlackImage)
    cout << "Image is a black one!" << endl;

Or you make an inverse threshold, than findContours and if the area of your found contour is nearly as big as the image area it is a black one. Remember noise and bad pixels so it must be a bit smaller

vector<Mat> Contours;
threshold(imgIn, imgOut, 5, 255, THRESH_BINARY_INV);
findContours(imgOut, Contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
for(int i = 0; i < Contours.size(); i++) {
    if(contourArea(Contours[i] if(contourArea(Contours[i]) > imgIn.total()*0.99) {
        cout << "Image is a black one!" << endl;
        break;
    }
}

The first one should be the easier method, the second one should be more flexible, if just a part of the image has to be black or if you search a special area it can be adjusted. Check out: contours in OpenCV