Ask Your Question

Revision history [back]

here is the implemention of @theodore 's answer

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "iostream"

using namespace cv;

bool isGrayImage( Mat img ) // returns true if the given 3 channel image is B = G = R
{
    Mat dst;
    Mat bgr[3];
    split( img, bgr );
    absdiff( bgr[0], bgr[1], dst );

    if(countNonZero( dst ))
        return false;

    absdiff( bgr[0], bgr[2], dst );
    return !countNonZero( dst );
}

int main(int argc, char** argv)
{
    static const char* str[] = {" is a COLOR image"," is a GRAY image"};
    char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg";
    Mat src = imread(filename);

    if(src.data)
    {
        std::cout << filename << str[isGrayImage( src )] << std::endl;
        imshow(filename, src );
        waitKey();
    }
    return 0;
}

here is the implemention of second step of @theodore 's answer

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "iostream"

using namespace cv;

bool isGrayImage( Mat img ) // returns true if the given 3 channel image is B = G = R
{
    Mat dst;
    Mat bgr[3];
    split( img, bgr );
    absdiff( bgr[0], bgr[1], dst );

    if(countNonZero( dst ))
        return false;

    absdiff( bgr[0], bgr[2], dst );
    return !countNonZero( dst );
}

int main(int argc, char** argv)
{
    static const char* str[] = {" is a COLOR image"," is a GRAY image"};
    char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg";
    Mat src = imread(filename);

    if(src.data)
    {
        std::cout << filename << str[isGrayImage( src )] << std::endl;
        imshow(filename, src );
        waitKey();
    }
    return 0;
}