Free Support Forum - aspose.com

Gray scale and Blank image detection

Hi,


Is there any functionality to detect Gray scale and blank page image using Aspose.Imaging SDK?

Thanks,
Dhivya

Hi Dhivya,

Thanks for inquiring Aspose.Imaging.

I have observed your requirements and like to share that even if a 32 or 24 bit image internally can have RGB distribution to make an image a gray scaled. So, in order to verify the image to be a gray scaled, you need to traverse every image pixel and verify if that is gray scaled or not.

Similarly, in order to check a blank image, you need to traverse every pixel and compare the two consecutive pixels. If there is any difference between two consecutive pixels, we cannot call image to be blank. Attach please find the sample code and sample images for verifying gray scale and blank images. I hope the shared information will be helpful.


Many Thanks,

Thank you Mudassir.


The code samples are working fine.

Hi,
me too, I need to check if a page is blank.
Can you share the sample code to check if a page is blank?

I tried with this code:

Public Function PageIsBlank(image As Stream, minBlackPixels As Integer) As Boolean
		Using img As Aspose.Imaging.Image = Aspose.Imaging.Image.Load(image)
			Dim r As Aspose.Imaging.RasterImage = TryCast(img, Aspose.Imaging.RasterImage)
			Dim width As Integer = r.Width
			Dim height As Integer = r.Height
			Dim pixels() As Aspose.Imaging.Color = r.LoadPixels(New Aspose.Imaging.Rectangle(0, 0, width, height))
			Dim count As Integer = 0
			Dim blankLimit As Integer = GetRGB(Aspose.Imaging.Color.White)
			' Considered blank if unded 99%
			blankLimit = CInt(blankLimit * 0.99)
			For Each p As Aspose.Imaging.Color In pixels
				If GetRGB(p) <= blankLimit Then count += 1
				If count >= minBlackPixels Then Exit For
			Next
			If count < minBlackPixels Then
				Return True
			End If
			Return False
		End Using
	End Function

	Private Function GetRGB(c As Color) As Integer
		Dim rgb As Integer = 0
		rgb += CInt(c.R)
		rgb += CInt(c.G)
		rgb += CInt(c.B)
		Return rgb
	End Function

@itconsult.developer,

I have observed the sample code. Have you found any issue while using this code. Can you please share the source image that you need to verify so that I may investigate this further on my end.

No issue with that code… My question wanted to be only: is there an “official” (or better) way to check if a page is blank?
Anyway, thank you.

@itconsult.developer,

Thank you for sharing your feedback. As I have shared earlier in my previous post as well that one need to traverse every pixel inside the image and in case of any change in pixel comparison the image shall be considered not as blank. Otherwise in case of no change in all pixels comparisons the image will be considered blank. This is only way to check the image.

Attached please find the sample code and sample images for verifying gray scale and blank images.

Have the attached code samples (especially the one for verifying grayscale) been removed, or am I looking in the wrong place? If removed, could they be restored?

Thank you,

Ken

@kjhughes,

I suggest you to please try using following sample code on your end to serve the purpose.

public static void CheckJpg()
{
    String path = @"C:\Imaging Data\";

    using (Aspose.Imaging.FileFormats.Jpeg.JpegImage jpgImage = (Aspose.Imaging.FileFormats.Jpeg.JpegImage)Aspose.Imaging.Image.Load(path + "TestBlank.jpg"))
        // using (Aspose.Imaging.FileFormats.Jpeg.JpegImage jpgImage = (Aspose.Imaging.FileFormats.Jpeg.JpegImage)Aspose.Imaging.Image.Load(path + "index.jpg"))
        {
            PixelFormat format=  jpgImage.RawDataFormat.PixelFormat;
   
            //Comment following line if using TestBlank.jpg 
         bool ifGray = TestGrayScale(jpgImage, jpgImage.RawDataFormat.BitsPerPixel);
            
        //Comment the following line when using index.jpg
        bool ifEmpty = IsEmpty(jpgImage);
    }

}
public static bool IsEmpty(Aspose.Imaging.FileFormats.Jpeg.JpegImage bmp)
{
      
    Color pixelColor = Color.Empty;
    int prevR=0,prevG=0,prevB=0;
        
    for (int x = 0; x < bmp.Width; x++)
    {
        for (int y = 0; y < bmp.Height; y++)
        {
            pixelColor = bmp.GetPixel(x, y);
            
            if (y > 0 && (prevR != pixelColor.R || prevG != pixelColor.G || prevB != pixelColor.B))//check if consecutive pixels are same
            {
                return false;
            }
            prevR = pixelColor.R;
            prevG = pixelColor.G;
            prevB = pixelColor.B;

        }
    }
    return true;
}
public static bool TestGrayScale(Aspose.Imaging.FileFormats.Jpeg.JpegImage bmp, int threshold)
{
    Color pixelColor = Color.Empty;
    int rgbDelta;

    for (int x = 0; x < bmp.Width; x++)
    {
        for (int y = 0; y < bmp.Height; y++)
        {
            pixelColor = bmp.GetPixel(x, y);
            rgbDelta = Math.Abs(pixelColor.R - pixelColor.G) + Math.Abs(pixelColor.G - pixelColor.B) + Math.Abs(pixelColor.B - pixelColor.R);
            if (rgbDelta > threshold) return false;
        }
    }
    return true;
}

@mudassir.fayyaz,

Thank you for posting sample code for detecting grayscale. It seems to be a functionally reasonable way to perform the check, however do you know of a solution with a complexity lower than the O(height x width) pixel checks that this approach requires?

@kjhughes,

I like to share that this is the only approach to verify the gray scale image by checking every pixel. There can be a one colored pixel inside entire presentation and you may not call that image a gray scale. So, to be sure you need to verify every pixel.

@mudassir.fayyaz,

Alright, I understand what you’re saying about how a single color pixel could cause an image to have to be classified as color. I was hoping that there might be metadata or pallet information that could be checked in constant time, but it sounds like that’s not the case. Thank you for your response and help.

@kjhughes,

Thank you for the understanding. Please share if I may help you any further.