Checking to see if an image is Blank in C# - c#

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.
Writing unsafe code - pointers in C
Using Pointers in C#
/unsafe (C# Compiler Options)
Bitmap.LockBits Method (Rectangle, ImageLockMode, PixelFormat)
(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;
}

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.

If you're using System.Drawing.Bitmap you can speed up things up (substantially), by:
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.
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:
When using System.Drawing.Bitmap your image may be in device memory and accessing it may be time consuming.
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.

Scale the image to 1x1 then check one pixel
new Bitmap(previousImage, new Size(1, 1));

Related

AForge ExhaustiveTemplateMatching works extremely slow

I am trying to find coordinates of one image inside of another using AForge framework:
ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching();
TemplateMatch[] matchings = tm.ProcessImage(new Bitmap("image.png"), new Bitmap(#"template.png"));
int x_coordinate = matchings[0].Rectangle.X;
ProcessImages takes about 2 minutes to perform.
Image's size is about 1600x1000 pixels
Template's size is about 60x60 pixels
Does anyone know how to speed up that process?
As addition to the other answers, I would say that for your case:
Image's size is about 1600x1000 pixels Template's size is about 60x60 pixels
This framework is not the best fit. The thing you are trying to achieve is more search-image-in-other-image, than compare two images with different resolution (like "Search Google for this image" can be used).
About this so
called pyramid search.
it's true that the algorithm works way faster for bigger images. Actually the image-pyramid is based on template matching. If we take the most popular implementation (I found and used):
private static bool IsSearchedImageFound(this Bitmap template, Bitmap image)
{
const Int32 divisor = 4;
const Int32 epsilon = 10;
ExhaustiveTemplateMatching etm = new ExhaustiveTemplateMatching(0.90f);
TemplateMatch[] tm = etm.ProcessImage(
new ResizeNearestNeighbor(template.Width / divisor, template.Height / divisor).Apply(template),
new ResizeNearestNeighbor(image.Width / divisor, image.Height / divisor).Apply(image)
);
if (tm.Length == 1)
{
Rectangle tempRect = tm[0].Rectangle;
if (Math.Abs(image.Width / divisor - tempRect.Width) < epsilon
&&
Math.Abs(image.Height / divisor - tempRect.Height) < epsilon)
{
return true;
}
}
return false;
}
It should give you a picture close to this one:
As bottom line - try to use different approach. Maybe closer to Sikuli integration with .Net. Or you can try the accord .Net newer version of AForge.
If this is too much work, you can try to just extend your screenshot functionality with cropping of the page element that is required (Selenium example).
2 minutes seems too much for a recent CPU with the image a template sizes you are using. But there are a couple of ways to speed up the process. The first one is by using a smaller scale. This is called pyramid search. You can try to divide the image and template by 4 so that you will have an image of 400x250 and a template of 15x15 and match this smaller template. This will run way faster but it will be also less accurate. You can then use the interesting pixels found with the 15x15 template and search the corresponding pixels in the 1600x1000 image using the 60x60 template instead of searching in the whole image.
Depending on the template details you may try at an even lower scale (1/8) instead.
Another thing to know is that a bigger template will run faster. This is counter-intuitive but with a bigger template you will have less pixel to compare. So if possible try to use a bigger template. Sometimes this optimization is not possible if your template is already as big as it can be.

Bitmap pixel replacement based on brightness

Simply, I want to set any pixel in an image to white where the brightness is greater than a given threshold. I have written the following code which works suitably fast and accurately for my purposes.
using (Bitmap image = new Bitmap("C:\\temp\\test1.png"))
{
for (int x = 0; x < image.Width; x++)
{
for (int y = 0; y < image.Height; y++)
{
Color c = image.GetPixel(x, y);
float f = c.GetBrightness(); //From 0 (black) to 1 (white)
if (f > 0.1) { image.SetPixel(x, y, Color.White); }
}
}
image.Save("C:\\temp\\test2.png");
}
However, it just feels wrong to have to loop through every pixel one-by-one. Is there an optimized approach to this problem using another .NET Imaging or Graphics approach?
For some special cases (such as power of 2 thresholds), you can work on 32-bit chunks using bitmasking tricks, but I doubt it would buy you very much. The far bigger overhead in the code you provided are the GetPixel and SetPixel calls, which are very inefficient. You can greatly speed up the procedure using the LockBits method and processing the underlying data directly from a byte[].
You might optimize by using proven native algorithm implementations (e.g. OpenCV). Although I'm not an expert on this subject, think you will need to iterate through all pixels anyway. Even scientific papers about thresholding algorithms (which rather focus on finding a good threshold) loop through the whole image, so I don't think you have to feel wrong about.
Anyway, if there's some mathematical approach that works on any image, I would also be interested, but I doubt the existence.
Consider it this way, if you want to know if a pixel is above or below the threshold for all the pixels, you are going to have to visit all the pixels. Any pixel not visited will mean you don't know its value.
There is no way to write this in better than O(n) time, especially if you intend to change values of certain pixels.

Counting unique pixels in a drawn image of shapes

I'm working on a graphics application where the user can draw any number of Lines (with some thickness from point A to point B), Rectangles, or Ellipses on a canvas.
After they're done, I have a set of shape data indicating the location of each shape and line drawn and I need to determine how many unique pixels they've colored as part of a research project.
My naive algorithm is to implement bool shape.Contains(x,y) for each shape and call it for every drawn shape for every pixel in the image to determine if that pixel was drawn by a line, rectangle or ellipse.
Working the other way, I could create void shape.SetPixels(bool[,] canvas) and have each shape set to true, each pixel it contains. This is what I've actually implemented and with large data sets, it's painfully slow.
I have a feeling that there's a more direct way to go from the raw shape data to the output that I need without examining every pixel. So my question is, given the set of shape data, is there a O(n) function bool[,] IsColored(int x, int y) {} that can generate a matrix of true/falses for colored pixels more directly than either idea I've given?
Avoid the Bitmap.GetPixel method. It is very very slow. If possible your low level access to the bitmap data with LockBits or similar techniques.
In one of my projects I used:
public void LoadFromBitmap(Bitmap bmp)
{
if (bmp.Width != Width || bmp.Height != Height)
throw new ArgumentException("Size missmatch");
unsafe
{
BitmapData bmpData = null;
try
{
bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
for (int y = 0; y < bmpData.Height; y++)
{
uint* p = (uint*)((byte*)bmpData.Scan0 + y * bmpData.Stride);
for (int x = 0; x < bmpData.Width; x++)
{
this[x, y] = RawColor.FromARGB(*p);
p++;
}
}
}
finally
{
if (bmpData != null)
bmp.UnlockBits(bmpData);
}
}
}
https://github.com/CodesInChaos/ChaosUtil/blob/master/Chaos.Image/Pixels.cs
Another optimization is implementing a pool for your pixel containing arrays. Frequently allocating objects on the large-object-heap stresses the gc very much in my experience.
The two methods you're talking about are generally your two main options. Either check each pixel as needed, or build some kind of data structure for fast lookup beforehand.
If you have a large canvas, but only a few shapes (thus, relatively few "on" pixels), then it may be best to just record every pixel that is hit by any shape, in a hash for example.
HashSet<KeyValuePair<int,int>> listOfPixelsHitByAnyShape = new HashSet()
foreach(Shape s in allShapes)
{
s.Draw(listOfPixelsHitByAnyShape); // will update listOfPixelsHitByAnyShape
}
// Now we can easily query if a pixel is set
bool isSet = listOfPixelsHitByAnyShape.Contains(new KeyValuePair(10,99))
This should be fast for lookup, at the expense of memory, and time to build the HashSet.
But it won't be quite as fast as your SetPixels(bool[,] canvas) version, nor using as much memory (in the sparse case we're talking about).
If you aren't using a graphics library, please, please do!
If it's a graphics application presumably you're already drawing what the user inputs. Can't you just query that? You could always draw to 2 different targets, one for the pretty user version, and another for the query (each object a unique colour).
How do you want to deal with overlaps?
How large a dataset are we talking? The rendering should be 'free' as the bitmap could be built as the user is drawing in the application.
If what you've done is really slow you could consider using the GPU to draw and query (possibly using fancy shaders).

Make foregroundcolor black or white depending on background

Something like calculating the average value of rgb components and then decide whether to use black or white?
Do I have to convert RGB to HSV in first step 'cause RGB is not always what the human eyes see?
I'm using C#
It just so happens I needed this function for a project not long ago.
private int PerceivedBrightness(Color c)
{
return (int)Math.Sqrt(
c.R * c.R * .241 +
c.G * c.G * .691 +
c.B * c.B * .068);
}
This formula I found on the web at Nbd Tech that dealt with perceived colors and color conversion formula. The site gives a lot of information that is helpful.
Here's how to use this to select black or white:
var foreColor = (PerceivedBrightness(backColor) > 130 ? Color.Black : Color.White);
You can use a value other than 130 as the cutoff; it is preference.
Update: According to Darel Rex Finley at his site:
The values I came up with by playing with Photoshop were actually .241, .691, and .068, but I have since been informed that the values .299, .587, and .114 are more accurate.
This specification follows ITU-R Recommendation BT.601 (or Rec. 601 for short). The site I mentioned above, Nbd Tech, hasn't yet been updated to reflect this.
Based on this, here is the updated method (thanks to DTI-Matt for the comment):
private int PerceivedBrightness(Color c)
{
return (int)Math.Sqrt(
c.R * c.R * .299 +
c.G * c.G * .587 +
c.B * c.B * .114);
}
Note on threshold preference:
Colors with a perceived brightness near the middle (e.g. 120-140) will be more subjective. For example, it's debatable whether red (FF0000), which evaluates to 139, is clearer with a black or white overlay.
what about that?
private static Color GetReadableForeColor(Color c)
{
return (((c.R + c.B + c.G) / 3) > 128) ? Color.Black : Color.White;
}
The Color struct supports conversion to HSB natively.
if (Color.GetBrightness() > 0.5f) {
// win
}
You may want to add a component of saturation as well, considering saturation also contributes to apparent 'lightness'.
You could do a simple calculation depending on color depth, if say you had a #FFFFFF color format, you could just add the RGB values and calculate if they're under half way.
The max in that case is 255 (F x F = 256) per, so just check if it's under that threshold:
var colorCode = ((R + B + G) < 255*3) ? "#FFFFFF" : "#000000";
If the color's below, it's darker...use a white background. If it's above, it's lighter...use a black background. It's not perfect, but an idea to get started.
If I'm understanding correctly, one approach might be to get hold of the desktop wallpaper image, check in some manor what colour it is and then change your application colour based on that.
There's an article on geekpedia about getting hold of the current desktop wallpaper (and lots of hits on google on that), but the basic premise is to grab the registry value of the current wallpaper:
RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", false);
string WallpaperPath = rkWallPaper.GetValue("WallPaper").ToString();
You could then use that path to open the image. You can then get hold of lots of properties, such as the dimensions and individual pixel colours in RGB.
To work out whether it's "more white" or "more black" you have many options.
One idea would be to get each pixel colour in RGB, average the values (to get the greyscale value) and then average the greyscale value of each pixel across the image. If this comes out > 128 then it could be condidered to be "white". If < 128 then "black". Basically you are deciding which side of the mid-grey dividing line the images pixel intensities average out to.
// Psudo code - can't check the C# spec atm.
foreach(Pixel p in image)
{
// get average of colour components.
double greyScale = (p.Red + p.Green + p.Blue) / 3;
totalIntensity += greyScale;
}
double averageImageIntensity = totalIntensity / image.NumPixels;
if(totalIntensity > 128) // image could be considered to be "white"
else // image could be considered "black".
Problems: could be a slow procedure, you might want to sample only some of the pixels (say, every 10th one etc.) to save time. More generally, it seems like a fairly hacky thing to be doing at all. Pulling user files at run-time and messing with them is hardly clean, and it provides potential security and stability concerns. What if the image is duff or corrupt etc.
Personally I'd suggest simply giving the user the choice of colour / theme / skin themselves, or better yet let them customise it!

Convert 1-bit image to 8-bits

How can I convert a 1 bit image to an 8 bit image using C#? What color matrix should be used? Can you please provide samples or links?
Disclaimer: I don't know C#, but I've done too much image processing on C/C++ so I can't pass on answering - I'll answer in C since I think C# shares a similar syntax.
Both 1bit (two colors) and 8bit (256 colors) images have a palette. but transitioning a 1bit to 8bit transition is easy - since there's no quantization involved, just up-sampling.
First you need to choose (or import) the two colors of the 1bit image's palette. If you don't have any, I suggest using black (0x000000FF) and white (0xFFFFFFFF) for clarity (note: both colors are RGBA, I think windows uses ABGR). This will be your 'palette'.
Then map each color to the palette - The input image is going to have width * height / 8 bytes. Each byte represents eight pixels. Since I don't know about your expertise in bittwiddling (i.e I don't want to confuse you and I don't want you to mindlessly copy and paste code you've been granted on the internets), I'll keep this answer simple.
// Insert your image's attributes here
int w = image.width;
int h = image.height;
// Data of the image
u8* data = image.data;
/*
* Here, you should allocate (w * h) bytes of data.
* I'm sure C# has ByteArray or something similar...
* I'll call it output in my code.
*/
u8* output = new u8[w * h];
u8* walker = output;
// Loop across each byte (8 pixels)
for(int i=0; i<w*h/8; ++i) {
// Loop across each pixel
for(int b=(1<<7); b>0; b>>=1) {
// Expand pixel data to output
*walker++ = !!(data[i] & b);
}
}
Hope that helps!
Does this help:
http://www.wischik.com/lu/programmer/1bpp.html
But of course it needs some clean up. It could use some try..finally's for all the delete's and releasing of DC's

Categories

Resources