Checking to see if an image is Blank in C#
Asked Answered
R

4

7

I've looked everywhere but there doesn't seem to be a standard (I could see) of how one would go about checking to see if an image is blank. In C#

I have a way of doing this, but would love to know what the correct way is of checking to see if an image is blank, so everyone could also know in the future.

I'm not going to copy paste a bunch of code in, if you want me to, it will be my pleasure, but I just first want to explain how i go about checking to see if an image is blank.

You take a .jpg image, Get the width of it. For example 500 pixels Then you divide that by 2 giving you 250

Then you check what the colour of every pixel is in the location of (250 width, and i height) (where you iterate thought the hight of the image.

What this then do is only check the middle line of pixels of an image, vertically. It goes though all the pixels checking to see if the colour is anything Except white. I've done this so you wont have to search ALL 500*height of pixels and since you will almost always come across a colour in the middle of the page.

Its working... a bit slow...There must be a better way to do this? You can change it to search 2/3/4 lines vertically to increase your chance to spot a page that's not blank, but that will take even longer.

(Also note, using the size of the image to check if it contains something will not work in this case, since a page with two sentences on and a blank page's size is too close to one another)

After solution has been added.

Resources to help with the implementation and understanding of the solution.

(Note that on the first website, the stated Pizelformat is actually Pixelformat) - Small error i know, just mentioning, might cause some confusion to some.

After I implemented the method to speed up the pixel hunting, the speed didn't increase that much. So I would think I'm doing something wrong.

Old time = 15.63 for 40 images.

New time = 15.43 for 40 images

I saw with the great article DocMax quoted, that the code "locks" in a set of pixels. (or thats how i understood it) So what I did is lock in the middle row of pixels of each page. Would that be the right move to do?

private int testPixels(String sourceDir)
    {
         //iterate through images
        string[] fileEntries = Directory.GetFiles(sourceDir).Where(x => x.Contains("JPG")).ToArray();

        var q = from string x in Directory.GetFiles(sourceDir)
                where x.ToLower().EndsWith(".jpg")
                select new FileInfo(x);

        int holder = 1;
        foreach (var z in q)
        {
            Bitmap mybm= Bitmap.FromFile(z.FullName) as Bitmap;
            int blank = getPixelData2(mybm);
           
            if (blank == 0)
            {
                holder = 0;
                break;
            }
        }
        return holder;
    }

And then the class

private unsafe int getPixelData2(Bitmap bm)
        {
            BitmapData bmd = bm.LockBits(new System.Drawing.Rectangle((bm.Width / 2), 0, 1, bm.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bm.PixelFormat);

            int blue;
            int green;
            int red;

            int width = bmd.Width / 2;
            for (int y = 0; y < bmd.Height; y++)
            {
                byte* row = (byte*)bmd.Scan0 + (y * bmd.Stride);

                blue = row[width * 3];
                green = row[width * 2];
                red = row[width * 1];

                // Console.WriteLine("Blue= " + blue + " Green= " + green + " Red= " + red);
                //Check to see if there is some form of color
                if ((blue != 255) || (green != 255) || (red != 255))
                {
                    bm.Dispose();
                    return 1;
                }
            }
            bm.Dispose();
            return 0;
        }
Runin answered 2/10, 2012 at 5:14 Comment(7)
Have you timed how long it takes? If so, what was the time and what was the height of the image?Katheykathi
You must be doing something wrong in your implementation, because your algorithm seems sound to me. It should take no time at all to scan down one column ofa bitmap.Redvers
Total Time for 32 images = 00:00:12.9187389 Height = 6552 Width=4580Runin
Might there be some external factors playing a role? PC speed etc?Runin
If you can post your entire test code we might spot something. From experience reading all the pixels in 600x800 image with LockBits takes no time at all (milliseconds if that.)Outrigger
when you iterate through the files, i would guess that a Parallel.ForEach will make a huge boost. Usually does for me resizing map tile sets. (i know its old, but ppl still read old questions)Hemato
I once did some performance testing when I wanted to compute the average color of images. I tried four approaches: 1) iterating the whole image with a loop, 2) parallelization using TPL, 3) parallelization using SIMD, and 4) down-scaling the image using the framework. In the end I didn't use any of the approaches because sub-sampling was much faster and sufficient to get an average color. The (crude) benchmark code can be found here.Boomkin
D
9

If you can tolerate the chance of getting it wrong, the approach seems fine; I have done something very similar in my case, although I always had a visual confirmation to deal with errors.

For the performance, the key open question is how you are getting the pixels to test. If you are using Bitmap.GetPixel, you are bound to have performance problems. (Search for "Bitmap.GetPixel slow" in Google to see lots of discussion.)

Far better performance will come from getting all the pixels at once and then looping over them. I personally like Bob Powell's LockBits discussion for clarity and completeness. With that approach, checking all of the pixels may well be reasonable depending on your performance needs.

Deepdyed answered 2/10, 2012 at 5:34 Comment(4)
You are actually spot on, I am using Bitmap.GetPixel. Thank you, ill Check that solution out, and post my findings here so there's one full solution and "dare i say" standard on how to check an image.Runin
So i Implemented the solution. The only thing is, the times are almost identical Old time = 15.63 for 40 Images. And new time is = 15.43 for 40 images. Ill explain as additional info on my question above what i did.Runin
It does speed it up with a fraction. I Think its difficult to see the increase with the amount of data i have. Thank youRunin
Glad it helped some. It would probably be helpful to time the various parts of the execution to see what is slow: is it BitMap.FromFile, the LockBits call, the for loop or something else. With 15 seconds of processing time, Stopwatch instances with diagnostic start/stop calls sprinkled though your code should be plenty.Deepdyed
O
2

If you're using System.Drawing.Bitmap you can speed up things up (substantially), by:

  1. Not using GetPixel to access the pixels, use LockBits and UnlockBits to copy the image bitmap to regular memory. See the examples on the MSDN documentation for usage.
  2. Not calling the Width, Height or Size properties in for loop. Call Size once, store the values in a local variable and use those in the loop.

Notes:

  1. When using System.Drawing.Bitmap your image may be in device memory and accessing it may be time consuming.
  2. I don't remember whether loading an image into a Bitmap already converts it to RGB format as other formats are more difficult to work with, but if that is not the case you can create an RGB Bitmap of the same size as your original image, get it's Graphic object (Graphics.FromImage) and use DrawImage to draw the original image in the RGB bitmap.

Edit: Beat to the punch by DocMax.

In any case for speed you can also try using alternative libraries such as the excellent FreeImage which includes C# wrappers.

Outrigger answered 2/10, 2012 at 5:49 Comment(0)
M
0

Scale the image to 1x1 then check one pixel

new Bitmap(previousImage, new Size(1, 1));

Misguided answered 4/12, 2014 at 15:3 Comment(2)
This is brilliant, unfortunately it doesn't work on most images that I've tried it with (a set of 64x56 tiles with argb color values). The single pixel is frequently 0,0,0,0 even when the source contains some image data.Pirogue
I did a further test using 2x2 and checking all four pixels. It was better but still reported empty when there was a small amount of content in the tile.Pirogue
S
0
    internal static bool IsImageBlankUsingPixel(string imagePath)
    {
        //convert image to thumbnail
        Image image = Image.FromFile(imagePath);
        Image thumb = image.GetThumbnailImage(100, 100, () => false, IntPtr.Zero);

        //thumb.Save(Path.ChangeExtension(imagePath, "thumb"));

        bool isPageBlank = true;

        using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(thumb))
        {
            // get the first pixel color
            System.Drawing.Color firstColor = bitmap.GetPixel(0, 0);

            // loop through pixel till we get a different color 
            for (int x = 0; x <= bitmap.Width; x++)
            {
                for (int y = 0; y <= bitmap.Height; y++)
                {
                    if (firstColor != bitmap.GetPixel(x, y))
                    {
                        isPageBlank = false;
                        break;
                    }
                    if (!isPageBlank) break;
                }
            }
        }
        return isPageBlank;
    }
Swordbill answered 15/2 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.