A faster alternative to the very slow GetPixel() and SetPixel() for .Net System.Drawing.Bitmap

Anybody who works with images often have probably come across .Net Bitmaps (System.Drawing.Bitmap) with their staggeringly slow GetPixel() and SetPixel() methods. Now, if you are going to work directly with images then you’re probably in the wrong place if you are using C# and .Net. However, sometimes you may want to do a small amount image analysis or manipulation from within .Net without the pain of having to pull in any other image libraries – but you find that GetPixel() and SetPixel() are just way too slow to use!

Now there is a faster way to access or manipulate the pixel data stored in a .Net Bitmap, that is to lock it (using LockBits()) and then directly access the raw image data in memory – you unlock it when you’re finished. This method is a lot faster than using Get/SetPixel() but is quite complicated to implement and it’s very messy to look at!

To get around this problem, and to avoid littering my code with gibberish I have written a bitmap wrapper class that wraps a bitmap, locks it, and provides it’s own GetPixel() and SetPixel() functions with which the original bitmap’s image data can be accessed. Using this class you can get fast access to a bitmap while using the familiar Get/SetPixel() paradigm – in this way it should act as a fairly easy drop in replacement for accessing the Bitmap objects directly.

The class is called BmpPixelSnoop and it is used like this:

First a BmpPixelSnoop object is created to wrap the bitmap, GetPixel() and SetPixel() can then be called on it. When the BmpPixelSnoop object is destroyed (on leaving the using() block) the original bitmap will be unlocked. It is important to note, that while the bitmap is being snooped the original bitmap object cannot be accessed as it’s locked! Currently BmpPixelSnoop only works for bitmaps with a Pixel Format of PixelFormat.Format32bppArgb which is the default format for Bitmaps (if you don’t specify an alternative when creating them).

So for a little extra complication you get easy & fast access to the bitmap data – but how much faster than the native Get/SetPixel() is it? My (non scientific) tests seem to indicate that it’s about 10 times faster, which is fast enough for simple imaging tasks. It is still quite inefficient however, this is a result of wanting to provide the same Get/SetPixel() interface as System.Drawing.Bitmap – for example, GetPixel() always returns all of the pixel data even if you just want to access the red component and hence is slower than it needs to be in this case. I may add extra accessor methods in the future to cater for other usage patterns and greater efficiency.

The code can be found in the git-hub repo: https://github.com/kgodden/DotNetPixelSnoop.

The class is defined in BmpPixelSnoop.cs, there is also some test code to check correctness and performance in Program.cs.

Here is the code:

Here is some sample output from the colsone based test program, showing relative times:

ExifTool- Query Exif Maker Note by ID on Command line

In Exif, each maker-note will have a unique (hex) id. ExifTool can be used to query a maker note’s contents by its id as follows:

This will return the value for makernote id 0x0013 contained in image.jpg.

PCL LNK2001 unresolved external symbol EuclideanClusterExtraction extract()

I have been getting the following linker error when building with the Point Cloud Library (PCL 1.8.1) in Visual Studio 2017 – not at all sure why:

I found that to get rid of the linker error I needed to include:

Again I am not sure why, and it is not usual to have to include impl/* files? – I may look into it a bit further if I get a chance but for now I am happy that my code links!

Hack – Sleeping for less that 1 second in Shell / Bash Script

To sleep in your shell script in units of 1 second – sleep will see you right. However what if you want to sleep for less than one second, say for 200ms?

On some newer system you can do something like this and everything is fine:

However when I try this on my embedded busybox device I just get a response like this:

So no luck there!

However there is a nice little hack using read, have a look at this:

This will wait for 200ms and then return! It waits for a line of text that never arrives, and times-out after 200ms – in effect it sleeps for 200ms. Now, if read receives a line during the 200ms it will return early, so only use it in cases where you’re sure it won’t or you might end up (temporarily) confused!

It’s not pretty but is better than nothing 😉

Bash – send data to serial (rs232) port and wait for response

Sending data to a serial port is quite easy in Bash, for example:

And you can read from a serial port using cat:

However cat must typically be run from a different shell instance as it blocks waiting for data. So is it possible to write and then read the response from a single shell instance?

Well, it is, but it requires a bit of sleight-of-hand. For example, if we start cat in the background and then send the command, cat will report the response as follows:

Which works but it a bit of a mouth-full! cat continues to run in the background, and will print more responses as they arrive.

But what if you want to just send one packet and then wait for a single response?

This is a bit harder, but if your response ends with an end of line character, or another known character then we can use read to help with this…

First we setup a read command in the background, unlike cat, this command will end when a response is received or when the timeout time arrives, then we can send our command:

This gets read to wait for up to 20 seconds (-t20) for a line of data (max size, 60 characters -n60) from /dev/ttyS0, which it reads into RESP, it then echos $RESP – all of this happens in the background. echo then sends the packet which will result in a response.

If your response packed ends with a character other than an EOL character then you can specify a delimiter to read using the -d command-line option.

Again it’s all a bit long winded, so we can wrap it all up in a bash script (send_tty.sh) as follows:

An example of using the script:

Github repo is here

Visual Studio Paho MQTT Error C2238 unexpected token(s) preceding ‘;’

If you’re receiving errors like the following when trying to build a project in Visual Studio 2017 using the Paho C client:

Then there is a quick fix, the problem seems to revolve around the following for DLLImport & DLLExport in the Paho header files:

The catch here is that neither WIN32 nor WIN64 will be defined as instead either _WIN32 or _WIN64 will be defined.

So a quick fix is to define either WIN32 or WIN64 in your project’s ‘Preprocessor Definitions’ depending on whether you are targeting x86 (32bit) or x64 (64bit).

Here are some of the other errors you might see when you have this problem:

PCL Octree Cheat Sheet

The point cloud library (PCL) is a fantastic resource for working with point clouds, however it is a large library and it can take a while to effectively find your way around it. The octree construct is very useful for working with point clouds, but again it can take a while to learn how to interact with octrees. To help me to remember how to interact with them I will include some examples here:

To iterate across the nodes of an octree (depth first):

To iterate across the leaf nodes:


Get a Voxel’s Bounding Region given node iterator:

Get point indices from leaf node iterator

Get point indices from leaf node pointer

An implementation of ntohf() with code

I am working on extracting data from some binary navigation messages today. Working with binary data can be difficult and quite confusing – especially when converting byte order from network (big endian) to host byte order (possibly little endian). The integer types are well covered by ntohs() and ntohl() et al. but when dealing with floats things can get a bit harder as ntohf() isn’t always available to the hard pressed programmer!

So I have included a simple implementation below, I have tested it during my own use, but it use at your own risk as this code may well be an example of a rather dubious use of unions!

This version is slightly adapted from an original implementation by Kevin Bowling (pls. see copyright notice).

ASCII ‘art’ for Camera Calibration Python Script

Working on a python script for a focus calibration (software based) routine for a computer vision camera, in order to spruce it up a bit I decide to add a nice ASCII introduction! #Friday

camera_cal

Using a Virtual USB Serial Port from a Guest OS on VirtualBox

Here is a decent set of instructions on how to access normal COM ports as well as USB Virtual com ports from you Guest OS running on VirtualBox – Thankfully it worked for me but I had to install the FTDI drivers on the Guest for rs232, I got them from here.

Thanks jorgensen!
.