Last active
February 23, 2017 01:37
-
-
Save isomorpheric/d0bca0429fe9459bf1b1d889762d2f38 to your computer and use it in GitHub Desktop.
Image Blur problem with C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/* For every topic that I had to research, I wrote a REF down below (at the end of the | |
* document). They are all numbered and have a link to the resource. */ | |
namespace ImageBlurCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int[,] multiarray = { { 0, 0, 0, 0 }, | |
{ 0, 0, 1, 0 }, | |
{ 0, 0, 0, 0 }, | |
{ 0, 1, 0, 0 } }; | |
Console.WriteLine("Old array: "); | |
Console.WriteLine(" "); | |
OutputArray(multiarray); | |
Console.WriteLine(" "); | |
Console.WriteLine("New array: "); | |
Console.WriteLine(" "); | |
Blur(multiarray); | |
Console.ReadLine(); | |
} | |
public static void Blur(int[,] multiarray) | |
{ | |
// REF#2 - Difference between Array.Copy() and Array.Clone() | |
int[,] blurredArray = (int[,])multiarray.Clone(); | |
for (int row = 0; row < multiarray.GetLength(0); row++) | |
{ | |
for (int col = 0; col < multiarray.GetLength(1); col++) | |
{ | |
int point = multiarray[row, col]; | |
if (point == 1) | |
{ | |
if (row - 1 >= 0) | |
blurredArray[row - 1, col] = 1; | |
if ( row + 1 < multiarray.GetLength(0) ) | |
blurredArray[row + 1, col] = 1; | |
if (col - 1 >= 0) | |
blurredArray[row, col - 1] = 1; | |
if (col + 1 < multiarray.GetLength(1)) | |
blurredArray[row, col + 1] = 1; | |
} | |
} | |
} | |
OutputArray(blurredArray); | |
} | |
// Method outputs 2d array to the console. | |
static void OutputArray(int[,] multiarray) | |
{ | |
// REF#1 - Difference between .GetLength() and .Length() | |
for (int row = 0; row < multiarray.GetLength(0); row++) | |
{ | |
for (int col = 0; col < multiarray.GetLength(1); col++) | |
{ | |
int pixel = multiarray[row, col]; | |
Console.Write(pixel); | |
} | |
Console.WriteLine(""); | |
} | |
} | |
} | |
} | |
/* REF: | |
1- Difference between Array.GetLength() and Array.Length(): | |
http://stackoverflow.com/questions/2044591/what-is-the-difference-between-array-getlength-and-array-length | |
2. Difference between Array.Copy() and Array.Clone(): | |
http://stackoverflow.com/questions/198496/difference-between-the-system-array-copyto-and-system-array-clone | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment