A reliable way to check if the image is a gray scale

I am currently working on one use case where I need to determine if a Gray Scale or RGB image is loaded. I found several ways to identify this, but I'm not sure if they are reliable and can be used together to confirm that the image is grayed out or not.

Part 1: read the image and get NumberDataElements using Raster.

BufferedImage image = ImageIO.read(file);
        Raster ras = image.getRaster();
        int elem = ras.getNumDataElements();

I noticed that elem is "1" in some cases, but not all.

Part 2: Check the RGB value for each pixel. If the values โ€‹โ€‹of R, G, B coincide with the specified pixel.

BufferedImage image = ImageIO.read(file);
        Raster ras = image.getRaster();

        //Number of Color elements
        int elem = ras.getNumDataElements();

        int width = image.getWidth();
        int height = image.getHeight();

        int pixel,red, green, blue;

        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++) {
                //scan through each pixel
                pixel = image.getRGB(i, j);
                red = (pixel >> 16) & 0xff;
                green = (pixel >> 8) & 0xff;
                blue = (pixel) & 0xff;

                //check if R=G=B
                if (red != green || green != blue ) {
                    flag = true;
                    break;
                }


            }

Here I check the values โ€‹โ€‹of R, G, B are the same for any given pixel, and this behavior is consistent across all pixels.

, , . ..

+4
6

. , , .

BufferedImage image = ImageIO.read(file);
        Raster ras = image.getRaster();

        //Number of Color elements
        int elem = ras.getNumDataElements();

        int width = image.getWidth();
        int height = image.getHeight();

        int pixel,red, green, blue;

        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++) {
                //scan through each pixel
                pixel = image.getRGB(i, j);
                red = (pixel >> 16) & 0xff;
                green = (pixel >> 8) & 0xff;
                blue = (pixel) & 0xff;

                //check if R=G=B
                if (red != green || green != blue ) {
                    flag = true;
                    break;
                }


            }
+1

, - - . : * , ( - , , ). * leonbloy ,

, .

0

if (flag) { break; } .

, (red != green || green != blue). , , .

isGrayscale boolean true, false, , true. , , . , , .

, , , , .. , . :)

0

R = G = B , , . . , . - . , , jpg , , (, , , ). , , . , , R = G = B.

R = G = B. , , , false.

public static boolean isGreyscale(BufferedImage image)
{
    int pixel,red, green, blue;
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++) 
        {
            pixel = image.getRGB(i, j);
            red = (pixel >> 16) & 0xff;
            green = (pixel >> 8) & 0xff;
            blue = (pixel) & 0xff;
            if (red != green || green != blue ) return false;
        }
    }
    return true;
}

PS: . pohotohop jpg. gif , .

0

: , ?

, , (.. true, , ). , , , RGB, . . , .

- , . 255 elem=1, elem=2, -.

To check if your encoding is grayscale, I suggest the following test:

int type = image.getColorModel().getColorSpace().getType();
boolean grayscale = (type==ColorSpace.TYPE_GRAY || type==ColorSpace.CS_GRAY);

To do this, you will need to import the ColorModel and ColorSpace classes from java.awt.imageand java.awt.color.

You can also find out if or image.getType()matters .BufferedImage.TYPE_BYTE_GRAYBufferedImage.TYPE_USHORT_GRAY

0
source

Here is a very simple way:

  • Check image type
  • Check Channel Image Number
  • Check pixel values.

Here is the code

boolean isGrayScale(BufferedImage image)
    {
    // Test the type
    if ( image.getType() == BufferedImage.TYPE_BYTE_GRAY ) return true ;
    if ( image.getType() == BufferedImage.TYPE_USHORT_GRAY ) return true ;
    // Test the number of channels / bands
    if ( image.getRaster().getNumBands() == 1 ) return true ; // Single channel => gray scale

    // Multi-channels image; then you have to test the color for each pixel.
    for (int y=0 ; y < image.getHeight() ; y++)
    for (int x=0 ; x < image.getWidth() ; x++)
        for (int c=1 ; c < image.getRaster().getNumBands() ; c++)
            if ( image.getRaster().getSample(x, y, c-1) != image.getRaster().getSample(x, y, c) ) return false ;

    return true ;
    }
0
source

All Articles