Greetings visitor from the year 2020! You can get the latest optimized working source code for this, including a version that does not use unsafe code, from my Github repo here. Thanks for visiting.
Recently I needed a way to find blank images among a large batch of images. I had tens of thousands of images to work with so I came up with this c# function to tell me whether an image is blank.
The basic idea behind this function is that blank images will have highly uniform pixel values throughout the whole image. To measure the degree of uniformity (or variability), the function calculates the standard deviation of all pixel values. An image is determined to be blank if the standard deviation falls below a certain threshold.
Here’s the code. In order to compile, the project to which this code resides must have “Allow Unsafe Code” checked.
public static bool IsBlank(string imageFileName)
{
double stdDev = GetStdDev(imageFileName);
return stdDev < 100000;
}
/// <summary>
/// Get the standard deviation of pixel values.
/// </summary>
/// <param name="imageFileName">Name of the image file.</param>
/// <returns>Standard deviation.</returns>
public static double GetStdDev(string imageFileName)
{
double total = 0, totalVariance = 0;
int count = 0;
double stdDev = 0;
// First get all the bytes
using (Bitmap b = new Bitmap(imageFileName))
{
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat);
int stride = bmData.Stride;
IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - b.Width * 3;
for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < b.Width; ++x)
{
count++;
byte blue = p[0];
byte green = p[1];
byte red = p[2];
int pixelValue = red + green + blue;
total += pixelValue;
double avg = total / count;
totalVariance += Math.Pow(pixelValue - avg, 2);
stdDev = Math.Sqrt(totalVariance / count);
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
}
return stdDev;
} To list available contexts: kubectl config get-contexts To show the current context: kubectl config current-context…
kubectl exec -it <podname> -- sh To get a list of running pods in the…
# Create a soft symbolic link from /mnt/original (file or folder) to ~/link ln -s…
git config --global user.name "<your name>" git config --global user.email "<youremail@somewhere.com>" Related Commands Show current…
TypeScript/JavaScript function getLastMonday(d: Date) { let d1 = new Date(d.getFullYear(), d.getMonth() + 1, 0); let…
I had to do some SMTP relay troubleshooting and it wasn't obvious how to view…
View Comments
Hi Leonardo: That makes a lot of sense. Thanks for sharing the info. I guess we can even calculate Standard Deviation for each color channel individually and make sure each of them is below the threshold.
<i understand that this code works on bitmaps. Does anyoe have a hint whether there ist something similar on compressed files (JPEG, JPEG2000) without decompressing the image? Thx, Jan
Jan: This code should work on JPEGs as well.
How awesome is this function? I am pulling hundreds of website snapshots. This will spot the blank ones and I can flag the listing.
I just have to work out all-black or color screens. The other issue is to spot "navagation cancelled.
Thanks for posting !!!!!
Thanks for your post Steve. Glad to hear that.
Can someone please confirm what the final working code is and a how do we call it
Adi: Sorry I have not gotten around to take everyone's feedback from this post and create an updated method. You will have to take my starting code, and incorporate the additions/changes from the comments in this post. Chinh
"On May 26th, 2009, Timex said:
Here is what I come up with: ..."
In my program your code doesn't work do detect white pages.
All pages give a number over 100000.
I have noticed you don't monetize your blog, don't waste your
traffic, you can earn extra bucks every month.
You can use the best adsense alternative for any type of website (they approve all websites), for more info simply search in gooogle: boorfe's
tips monetize your website
Hi,
I need the the code to perforn the same in c++. can someone please help ? it's very urgent?
I need the the code to perform the same in c++. can someone please help ? it’s very urgent?