A “blurplane”

Last week I set out to solve a coding problem with C#. I decided to solve a classic “Image Blur” problem, to get a deeper understanding as to how arrays, specifically multidimensional arrays, work in C#. It is worth noting that I solved this problem a while ago, but using Ruby. Solving it in C# gave me a perspective into how C# deals with multidimensional arrays, which is very different from Ruby. For instance, you actually declare a multidimensional array like this:

int[,] multidimensionalArray = new int[,] { { 1, 2 }, { 3, 4 } };

// Or like this:

int[,] multidimensionalArray1 = { { 1, 2 }, { 3, 4 } };

In Ruby, you treat multidimensional arrays just like regular arrays. It’s just that inside that “regular” array, there are other regular arrays. That’s for another post, though. Let’s get to the challenge!

Problem:

The task is that, given a 2-dimensional array the program must search the array for any (1)’s. Whenever it finds a (1), the numbers above, below, to the left and to the right of the (1) must be turned into (1)’s as well. Essentially it is searching for specific pixels in the array, and when it finds them it turns those around it to the same value. Example:

This:

00000

00000

01000

00010

00000

Should turn into this:

00000

01000

11110

01111

00010

Finished Problem:

The first thing I did was create a Multidimensional Array to work with in the Main Method. The array is static, and you cannot pass it a different array to blur; you must go into the main method and change it manually. That wasn’t the point of this challenge, but maybe I will add that functionality later.

Great, the array is there… but how do I print it to console? I had to find a way to loop through each subarray, without going out of bounds in either rows or columns. To accomplish this, I used nested for loops:

1- Difference between Array.GetLength() and Array.Length(): http://stackoverflow.com/questions/2044591/what-is-the-difference-between-array-getlength-and-array-length

At this point is where I learned the difference between .GetLength() and .Length() I put the reference I used to figure this out below the image. A quick summary is that .GetLength() is used to find out the length of the dimension you pass it, while .Length() gives you the number of items in the array. Neat!

So the 2d array prints, now it has to be blurred! Enter .Blur method…

The first thing the .Blur method does is clone the original array. If you don’t clone the array, you will inevitably change all the numbers after the first (1) to (1)’s, and it gets messy fast. This is I had to use .Clone() instead of .Copy() ; The latter merely ties the variable to the original array, so each time you modify [the copy], you are modifying the original array as well. This nets you a mess of (1)’s and a non-working program.

Difference between Array.Copy() and Array.Clone(): http://stackoverflow.com/questions/198496/difference-between-the-system-array-copyto-and-system-array-clone

Great, the array is cloned so now lets loop through it:

Same idea as OutputArray (two nested for loops)

Similarly to OutputArray(multidimensionalArray) , I iterated through the array with nested for loops but instead of just printing each point to the console I implemented the blur logic: If point == 1 then change the point above, below, left and right to (1) as well. To take boundaries into consideration we just check that the point we’re trying to change even exists:

Check above — if (row — 1 >= 0) prevents us from going out of bounds from above the array. Any index (row) below 0 does not exist, and therefore should be ignored.

Check below — if ( row + 1 < multiarray.GetLength(0) ) harnesses the power of .GetLength() yet again. Since we’re looking at “row” dimension, we want the total number of rows there are in the multidimensional array. Therefore we pass it a (0), for the 1st dimension. If row + 1 is a value higher than the maximum amount of rows, the point does not exist and should be ignored.

Check left— if (col — 1 >= 0) prevents us from going out of bounds to the left of the array. Any index (col) below 0 does not exist, and therefore should be ignored.

Check right — if ( col + 1 < multiarray.GetLength(1) ) is similar to the “check below” logic, but to the side instead of the bottom.

After changing the array, it is then printed with the .OutputArray method:

There it is — image successfully blurred:

Feel free to reach out with any questions or even better, suggestions!

For next post I want to —

Write about the .NET MVC application I’m working on. Worked with Linked Lists on C#

Till next time!