ali kiani
ali kiani

Reputation: 887

how detect rgb image format really is gray?

i have image processing program with opencv c++. inmy program two preprocessing path is for 2 type of gray level images and color images. but some times i get images is graylevel but store in RGB format means when look image is gray but this image have 3 channel.

how i can detect a rgb image format is really colored or gray ?

Upvotes: 2

Views: 1679

Answers (2)

Sunreef
Sunreef

Reputation: 4542

You can go through the pixels and check that their value for R, G and B are the same everytime.

cv::Mat image;  // your image with type CV_8UC3
bool grayscale = true;
for(int i = 0; i < image.rows; i++) {
    cv::Vec3b* ptr = image.ptr<cv::Vec3b>(i);
    for(int j = 0; j < image.cols; j++) {
        if(!(ptr[j][0] == ptr[j][1] && ptr[j][0] == ptr[j][2])) {
            grayscale = false;
            break;
        }
    }
    if(!grayscale)
        break;
}

Upvotes: 6

Vaibhav Bajaj
Vaibhav Bajaj

Reputation: 1934

Nearly all colors of grey have red == green == blue. Simply checking that red green and blue values are +10 or -10 should do the trick.

If instead you wish for a particular form of gray, you can check out this website to see the relevant colours of gray that can be formed or experiment yourself on a RGB input portal.

Upvotes: 3

Related Questions