2011/04/12 Matlab imagesc resizing

Whilst looking at someone elses project the other day, an oddity was recognised in Matlab. The following is further investigation of this. If an image is loaded into matlab and displayed using the imagesc function:

>> i = imread('big tux.png');
>> imagesc(i)

Saving the resulting figure (using the normal file, save as option) changes the size of the image, even if the white borders created are taken into account. The following images show this. The left-hand image shows the dimensions of the original image, whilst the right-hand image shows the dimensions of the image saved from the created figure.


April - Cookies


March - Flapjack


2011/03/17 - Efficiently testing for a single point in a range

I came across this neat method the other day for testing if a single value is between two others, regardless of which of the two values is the smallest. If the value under consideration is P and the two values forming the range are P1 and P2, P is within P1 and P2 if:
(P1 < P) == (P <= P2)
The following provides the six possible configurations of the three points considered and tables alongside the examples show the results of the above method (tick = true, cross = false). Note: under this method, the smallest of P1 and P2 did not have to be found and the values swapped - potentially speeding up computation.


February - Jammy Biscuits


January - Strawberry Cheesecake


2010/12/06 - Quickly iterating through image data in C#

The following is useful to use in C# to iterate through Bitmaps quickly, instead of using using the GetPixel and SetPixel methods. The specific example given below creates a Bitmap object and sets every pixel to black (0,0,0). Note, this needs to be compiled in Visual Studio using the unsafe code option.

Bitmap bmp = new Bitmap(img.Width, img.Height);

BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
    ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

unsafe
{
	for (int y = 0; y < bmp.Height; y++)
        {           
        	byte* ptr = (byte*)data.Scan0 + (y * data.Stride);
	        for (int x = 0; x < img.Width; x++)
        	{
        		ptr[x * 3] = 0;		// blue
	                ptr[x * 3 + 1] = 0;	// green
        	        ptr[x * 3 + 2] = 0;	// red
	        }
    	}
}

bmp.UnlockBits(data);

2010/11/30 - Multi-language webpage with WinXP

So, I've managed to produce to produce a mutli-language webpage using WinXP with relatively little trouble. It can be found by following this link. It doesn't contain any chinese or japanese characters because I couldn't find my WinXP installation disk to install them from.

I do have to mention that I found the jEdit program invaluable when editing UTF-8 files as I couldn't find any other program already installed on my WinXP partition which could easily do the job (I didn't want to try and make MS Word do this and my usual Windows editor of Notepad was not up to the task).


2010/10/23 - CImg and function calling increases execution time

A while ago when working on some other project I noticed that when calling functions in VC++ in Visaul Studio 2008, which performed operations on CImg objects, there was a distinct increase in execution time compared to executing the code in-line with the calling function. I've finally got round to testing this theory.

So I tested this theory using the two code setups.

Setup 1 - in-line code:

void main()
{
  begin timing
  
  create image
  create template

  perform correlation with image and template

  end timing
}

Setup 2 - function calling code:

void main()
{
  begin timing
  
  create image
  create template

  correlation(image,template)

  end timing
}

void correlation(image,template)
{
  perform correlation with image and template
}

These two setups were executed 1000 times and the average time computed and got the following results (times are in milliseconds):
  • Setup 1: mean time = 1120.01, standard deviation = 8.62
  • Setup 2: mean time = 1932.30, standard deviation = 11.05

Thus, calling the function increases the execution when considering CImg objects.


2010/10/02 - Bubble sort

Just because everyone loves bubble sort, it seems to be the only sorting algorithm I can remember off by heart these days, I've added a tutorial here including an animated c# application.


2010/09/17 - Efficiently creating a checkerboard pattern

Checkerboard patterns are great, they can be used for camera calibration and are an interesting pattern to create automatically in an efficient. So this got me thinking, what's the few lines of code that can be used to create a checkerboard pattern? I played around with a few ideas and the code below (C#) is the best I could come up with. But I still wonder if I can make this better. For your enjoyment, I have implemented this in a little app which creates user defined checkerboard patterns and allows them to be saved. The source code for the project can downloaded here. NOTE: please rename the downloaded file extension from .txt to .zip - it was changed to allow upload to my current free hosting service.
Bitmap chk = new Bitmap(square_size * grid_size, square_size * grid_size);

int x_count = 0;
int y_count = 0;

for (int x = 0; x < chk.Width; x++)
{
	for (int y = 0; y < chk.Height; y++)
        {
        	chk.SetPixel(x, y, Color.FromArgb(255, 255, 255));
        }
}

for (int i = 0; i < (grid_size * grid_size) / 2; i++)
{
	for (int x = x_count; x < x_count + square_size; x++)
        {
        	for (int y = y_count; y < y_count + square_size; y++)
                {
                	chk.SetPixel(x, y, Color.FromArgb(0, 0, 0));
                }
        }

        y_count += (square_size * 2);

        if (y_count == (square_size * grid_size))
        {
	        y_count = square_size;
                x_count += square_size;
        }

        if (y_count == (square_size * grid_size) + square_size)
        {
	        y_count = 0;
                x_count += square_size;
        }
}


2010/08/01 - Installing PHP, MySQL and Apache on Windows 7

  1. Download PHP5, follow this link and find the latest version of the PHP X.X zip package (at the time of writing this is the PHP 5.2.14 zip package). Unzip the contents into a folder.
  2. Download the appropriate version of the MySQL MSI from here and install using the downloaded MSI.
  3. Download the latest MSI for Apache from here and install.
  4. After installing Apache, goto a web broswer and type the address 127.0.0.1 into the navigation bar. If all is well, you should see a message telling you Apache is working.
  5. Goto Start -> All Programs -> Apache HTTP Server 2.2 -> Configure Apache Server -> Edit the Apache httpd.com Configuration File. In the text file which opens add the line:

    LoadModule php5_module "C:/php/php5apache2_2.dll"

    Beneath all of the other LoadModule and #LoadModule lines. Where, C:/php/ is the folder you extracted the contents of the PHP zip in step 1. Then, add the two lines:

    AddType application/x-httpd-php .php

    AddType application/x-httpd-php-source .phps

    Near all of the other AddType lines. Finally, save the file (you might need to change the security permissions to do this).
  6. Goto Start -> All Programs -> Apache HTTP Server 2.2 -> Control Apache Server -> Restart.
  7. Goto a web broswer and type the address 127.0.0.1 into the navigation bar, if you see the everythings ok message, you're good to go.


2010/07/20 - Generating permutations of numbers

So, after many attempts at this, I have finally figured out how to automatically generate all of the (unique) permutations for the numbers 0...n. The following gives the function which produces the set of permutations (it's written in vc++).

void ComputeCombinations(List^>^ combinations, int num)
{
	for (int i = 0; i < num; i++)
	{
		List^ l = gcnew List();
		l->Add(i);
		combinations->Add(l);

		List^>^ combA = gcnew List^>();
		combA->Add(l);
		List^>^ combB = gcnew List^>();

		for (int z = 0; z < num-1; z++)
		{
			combB = gcnew List^>();
			for (int h = 0; h < combA->Count; h++)
			{
				for (int j = combA[h][combA[h]->Count-1]+1; j < num; j++)
				{
					List^ nc = gcnew List();

					for (int k = 0; k < combA[h]->Count; k++)
					{
						nc->Add(combA[h][k]);
					}
					nc->Add(j);

					combB->Add(nc);
				}
			}	
			combA = combB;

			for (int h = 0; h < combA->Count; h++)
			{
				combinations->Add(combA[h]);
			}
		}
	}	
}

Where combinations contains the output list of permutations for numbers 0...num-1. So, for example, passing in num = 3 produces the following output:
    1. 0
    2. 0,1
    3. 0,2
    4. 0,1,2
    5. 1
    6. 1,2
    7. 2


2010/07/15 - LaTex font sizes

The following are all of the latex font size commands (I'm bored of searching for these every few weeks so I've written them down here so I have no reason to forget them).
    1. \tiny
    2. \scriptsize
    3. \footnotesize
    4. \small
    5. \normalsize
    6. \large
    7. \Large
    8. \LARGE
    9. \huge
    10. \Huge

Useful hint: use \font_size{ ... tabular environment ... } to change the size of the font inside a table.


2010/07/12 - LaTex floats spanning multiple columns

Some handy LaTex I came across while writing a paper ...

If you've got a float like the following:
\begin{float_type} 
   ...
\end{float_type}
				

and want the contents of the float to span multiple columns in the document, add an asterix (*) to the LaTex code after the float_name just like below:
\begin{float_type*} 
   ...
\end{float_type*}


2010/07/03 - Compiling a DLL in VS2008 with CImg.h

Ok, so I was trying to make a library in VC++ and wanted to use some of the imaging functionality in the CImg library (cough ... header file) and when I added my include and namespace lines I got a bunch of errors which I could not figure out for the life of me. After hours of trying to solve this problem I came to solution.

First, the errors. The images below shows the skeleton code of a VC++ class library with the CImg.h header file included and the errors which result from building the solution.

The solution. Right click on your project in the Solution Explorer and select the Properties menu option. In the dialog that appears, expand the Linker option and select Input (all in the image below). Now, in the Additional Dependencies field it will say '$(NoInherit)'. Delete the contents of this field, click the OK button on the dialog and rebuild the project. All of the errors should now of disappeared.

Again, I stumbled upon this answer after hours of messing around, building and re-buidling the project. I don't know if this is the actual solution to this problem and I just never found it or if there is a better way of working round these errors. But I hope this helps someone, and saves them all the time I wasted trying to solve this.