Software Algorithm Complexity Cheat-Sheet

Software Algorithm Complexities – Having trouble remembering your O(n)’s from your O(log N)’s, not to worry I just found this very handy complexity cheat-sheet –

I am currently doing some research into possible indexing mechanisms for very large sets of images in Computer Vision applications, so this sheet is very handy, thanks Eric!

C# Create a Cognex 8bit image (CogImage8Grey) from an 8bit Grayscale image array (byte[])

Here is some code that shows how to create an 8bit grayscale cognex image (CogImage8Grey) from an 8bit raw image stored as a byte array (byte[]). This type of memory messing is difficult in .NET at the best of times and it’s just a pity the the cognex library doesn’t help much more than it does.


I also have a feeling that the cognex library is doing more copying than it strictly needs to, but in true style its software documentation does not detail if, or when, it copies image data (or much else for that matter!) So I copy the image data from the byte array into a malloc’ed buffer before creating the cognex image.


First we have to define a SafeBuffer through which cognex can free up the allocated memory when it is finished with it, to do this we can derive a class from SafeBuffer like this:


Its constructor mallocs the memory and when it is disposed ReleaseHandle() is called, and this frees the memory. I also added a cast to IntPtr so that we can pass it into functions that expect an IntPtr.


Now that we have SafeMalloc we can write function to create the cognex image like this:


This function allocates memory via SafeMalloc, it then copies the raw image data from the input array into this memory. Then CogImage8Root.Initialize() is called passing in a pointer to this memory. not that in this case the image’s stride is the same as its width. Once the CogImage8Root has been initialised we can create a CogImage8Grey image and set the root via a call to SetRoot()!


They certainly make you work for it!!


If you know that nobody else will be using your image array and are willing to go ‘unsafe'(!) then you could avoid this extra memory copy by pinning the array and getting a pointer to it, you could then pass this pointer directly to the root Initialize() function. In this case you won’t need the SafeMalloc class etc.


Thanks to all on this thread for hints on SafeBuffer!


C# Save Grayscale byte array Image (byte[]) as Bitmap file (.bmp) example

Here is a quick, dirty and inefficient example of how to save an 8bit Grey scale image stored in a C# byte array as a 32bit bitmap file (.bmp). Saving bitmaps can be quite suprisingly difficult in .NET so I am posting this for future reference!


This code copies each byte (8 bit pixel) in the 8bit image into an array of 32bit pixels (4 bytes per pixel) and then saves it to disk. Note that you have to build your project with the ‘Allow unsafe code’ checkbox checked (go to project properties / Build and you will see the ‘Allow unsafe code’ checkbox.)



Thanks to all on this thread for the pointers!


Update, 02/2014:

This code will save the 8 bit bitmap in a compressed (but perfectly valid) format, to have your software save it in an uncompressed format take a look at this post.

.NET, WPF – BitmapImage File Locking

Q: How can I stop BitmapImage, in .NET Windows Presentation Foundation (WPF) from locking the source image file?


A: By default BitmapInfo seems to lock the source image file so that you can use the bitmapinfo object and modify or delete the source file at the same (or similar) time. For example, this C# will probably yield a locked image file:


var bitmap = new BitmapImage(new Uri(imageFilePath));
// use bitmap...


You can get around this locking problem as follows, it’s a little bit more long-winded but it worked for us….


var bitmap = new BitmapImage();
var stream = File.OpenRead(imageFilePath);
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
// Use bitmap.....


Magento, Customized theme not using overridden images

Q: I have created a new skins folder beside my default theme’s skin folder, and placed some images that I want to override into an images sub folder. I have changed the ‘Skin (Images / CSS)’ setting in System -> Configuration -> Design -> Themes to point to my new skins, I have cleared the magento cache about 10 million times, but magento still won’t use my new images, it keeps using the originals…. what do I do?


A: This has caught me out once or twice and I have always forgotten about it by the time it happens again! The problem stems from the fact that the theme css file calls in images using a relative path, e.g.:


body {background-image:url('../images/bg.gif');}


So because the css file that uses your images remains in its original location it will still use your original images, to get it to use your overridden images you must create a css folder in your new skin folder and copy any css files that reference the images into there and clear the cache a further 20 times – hopefully magento will then use your new images!


Have a look here for some more details: