Assignment 3: Median Filter

Background This assignment will get you to try implementing a non-trivial graphics operation using Inventor IDE (http://www.codecortex.com/ide/). I’d recommend first watching the video tutorial at http://showmedo.com/videotutorials/series?name=qdrYRTz8Z to get good idea of what’s involved and see some similar examples done for you. If there are any issues with Inventor IDE, ask Neil on the Code Cortex forum (http://www.codecortex.com/forum/) or by his email (neil dot g dot dickson at gmail).

In this assignment, we’ll try to make a 3x3 median filter, a technique often used to reduce noise in photos before performing image recognition operations. A 3x3 Gaussian blur is a technique also often used for noise reduction and is shown in Episode 4 (video #6) of the video tutorial. In the 3x3 median filter, we look at the 9 pixels in each 3x3 box and assign a pixel the median red value, median green value, and median blue value. To find the medians, we’ll simply use insertion sort until index 4 (the middle one) is reached. Note that for few elements, insertion sort can very fast when implemented in assembly (using xchg) as shown below, but most implementations are much slower, because they spend much of the time going backward through the array being sorted instead of always going forward.

The Algorithm void MedianFilter(RGB* pBitmap,int width,int height) { RGB values[9]; // NOTE: Stop 2 rows and 2 columns short of the end to avoid overflow int endIndex = width*(height-2) - 2; for (int p=0;p

// Insertion sort the blues to find the median for (int i=1;i<9;++i) { int blue = values[i].blue; if (blue < values[i-1].blue) { int j=0; while (j

// Insertion sort the greens to find the median for (int i=1;i<9;++i) { int green = values[i].green; if (green < values[i-1].green) { int j=0; while (j

// Insertion sort the reds to find the median for (int i=1;i<9;++i) { int red = values[i].red; if (red < values[i-1].red) { int j=0; while (j

Pass the parameters to MedianFilter via the stack using one of the calling conventions seen previously. No global variables should be used within MedianFilter, but the “values” array must be on the stack as a local array variable, and no other local variables should be used (except to save register values at the beginning of MedianFilter and restore them at the end if desired).

Example of Testing with a C Calling Convention In DrawImage (not MedianFilter), put something like the following: sub esp,12 mov [esp],ecx ;pBitmap mov [esp+4],eax ;width mov [esp+8],ebx ;height call MedianFilter add esp,12

Assessment Correctness of the program 12 marks Quality of source code (e.g. meaningful comments 6 marks and line label names, concise code, etc.) Testing (e.g. logical test cases with brief descriptions) 7 marks Total 25 marks

For testing, just make some (appropriate) images and save them as a 24-bit bitmap file (you can do this using a program such as MS Paint, or use files that are already 24-bit bitmaps) named input.bmp in the same directory as your code. The result of running your program will end up in output.bmp. You must submit the sample input.bmp (the Carleton tunnel maps photo) and the resulting output.bmp with your code. Submit your other test cases and their resulting images with names like AllRedTestInput.bmp and AllRedTestOutput.bmp, or EdgeCaseTestInput.bmp and EdgeCaseTestOutput.bmp, depending on the nature of the test. Also submit a text file with a description of each test case.

For up to two bonus marks, have at least one test case where the image is very “noisy” and the median filter greatly reduces the noise and at least one test case where the image is noisy but the median filter doesn’t do nearly as well at reducing noise. This may take a bit of investigation. 