static void Main( ) { // Load the input image. Write("Enter file name: "); string pathIn = ReadLine(); Retina retina = new Retina(pathIn + ".png"); Retina outputRetina = new Retina(pathIn + ".png"); for (int row = 1; row < retina.Pixels.GetLength(0) - 1; row++) { for (int col = 1; col < retina.Pixels.GetLength(1) - 1; col++) { int red = Convolution(row, col, "red", retina); int green = Convolution(row, col, "green", retina); int blue = Convolution(row, col, "blue", retina); outputRetina.Pixels[row, col] = SD.Color.FromArgb(red, green, blue); // Original image is not modified } } // Store the resulting image. string pathOut = pathIn + "_output.png"; outputRetina.SaveToFile(pathOut); }
static int Convolution(int row, int col, string colour, Retina retina) { int[,] kernel_1 = { { 5, 5, 5 }, { -3, 0, -3 }, { -3, -3, -3 } }; int[,] kernel_2 = { { -3, 5, 5 }, { -3, 0, 5 }, { -3, -3, -3 } }; int[] convolutionValues = new int[8]; // Array to store the sums of convolution switch (colour) { case "red": for (int convolution = 0; convolution < 8; convolution += 2) { int sum1 = 0; int sum2 = 0; sum1 += retina.Pixels[row - 1, col - 1].R * kernel_1[0, 0]; sum1 += retina.Pixels[row - 1, col].R * kernel_1[0, 1]; sum1 += retina.Pixels[row - 1, col + 1].R * kernel_1[0, 2]; sum1 += retina.Pixels[row, col - 1].R * kernel_1[1, 0]; sum1 += retina.Pixels[row, col + 1].R * kernel_1[1, 2]; sum1 += retina.Pixels[row + 1, col - 1].R * kernel_1[2, 0]; sum1 += retina.Pixels[row + 1, col].R * kernel_1[2, 1]; sum1 += retina.Pixels[row + 1, col + 1].R * kernel_1[2, 2]; sum2 += retina.Pixels[row - 1, col - 1].R * kernel_2[0, 0]; sum2 += retina.Pixels[row - 1, col].R * kernel_2[0, 1]; sum2 += retina.Pixels[row - 1, col + 1].R * kernel_2[0, 2]; sum2 += retina.Pixels[row, col - 1].R * kernel_2[1, 0]; sum2 += retina.Pixels[row, col + 1].R * kernel_2[1, 2]; sum2 += retina.Pixels[row + 1, col - 1].R * kernel_2[2, 0]; sum2 += retina.Pixels[row + 1, col].R * kernel_2[2, 1]; sum2 += retina.Pixels[row + 1, col + 1].R * kernel_2[2, 2]; convolutionValues[convolution] = sum1; convolutionValues[convolution + 1] = sum2; kernel_1 = RotateKernel(kernel_1); kernel_2 = RotateKernel(kernel_2); } break; case "green": for (int convolution = 0; convolution < 8; convolution += 2) { int sum1 = 0; int sum2 = 0; sum1 += retina.Pixels[row - 1, col - 1].G * kernel_1[0, 0]; sum1 += retina.Pixels[row - 1, col].G * kernel_1[0, 1]; sum1 += retina.Pixels[row - 1, col + 1].G * kernel_1[0, 2]; sum1 += retina.Pixels[row, col - 1].G * kernel_1[1, 0]; sum1 += retina.Pixels[row, col + 1].G * kernel_1[1, 2]; sum1 += retina.Pixels[row + 1, col - 1].G * kernel_1[2, 0]; sum1 += retina.Pixels[row + 1, col].G * kernel_1[2, 1]; sum1 += retina.Pixels[row + 1, col + 1].G * kernel_1[2, 2]; sum2 += retina.Pixels[row - 1, col - 1].G * kernel_2[0, 0]; sum2 += retina.Pixels[row - 1, col].G * kernel_2[0, 1]; sum2 += retina.Pixels[row - 1, col + 1].G * kernel_2[0, 2]; sum2 += retina.Pixels[row, col - 1].G * kernel_2[1, 0]; sum2 += retina.Pixels[row, col + 1].G * kernel_2[1, 2]; sum2 += retina.Pixels[row + 1, col - 1].G * kernel_2[2, 0]; sum2 += retina.Pixels[row + 1, col].G * kernel_2[2, 1]; sum2 += retina.Pixels[row + 1, col + 1].G * kernel_2[2, 2]; convolutionValues[convolution] = sum1; convolutionValues[convolution + 1] = sum2; kernel_1 = RotateKernel(kernel_1); kernel_2 = RotateKernel(kernel_2); } break; case "blue": for (int convolution = 0; convolution < 8; convolution += 2) { int sum1 = 0; int sum2 = 0; sum1 += retina.Pixels[row - 1, col - 1].B * kernel_1[0, 0]; sum1 += retina.Pixels[row - 1, col].B * kernel_1[0, 1]; sum1 += retina.Pixels[row - 1, col + 1].B * kernel_1[0, 2]; sum1 += retina.Pixels[row, col - 1].B * kernel_1[1, 0]; sum1 += retina.Pixels[row, col + 1].B * kernel_1[1, 2]; sum1 += retina.Pixels[row + 1, col - 1].B * kernel_1[2, 0]; sum1 += retina.Pixels[row + 1, col].B * kernel_1[2, 1]; sum1 += retina.Pixels[row + 1, col + 1].B * kernel_1[2, 2]; sum2 += retina.Pixels[row - 1, col - 1].B * kernel_2[0, 0]; sum2 += retina.Pixels[row - 1, col].B * kernel_2[0, 1]; sum2 += retina.Pixels[row - 1, col + 1].B * kernel_2[0, 2]; sum2 += retina.Pixels[row, col - 1].B * kernel_2[1, 0]; sum2 += retina.Pixels[row, col + 1].B * kernel_2[1, 2]; sum2 += retina.Pixels[row + 1, col - 1].B * kernel_2[2, 0]; sum2 += retina.Pixels[row + 1, col].B * kernel_2[2, 1]; sum2 += retina.Pixels[row + 1, col + 1].B * kernel_2[2, 2]; convolutionValues[convolution] = sum1; convolutionValues[convolution + 1] = sum2; kernel_1 = RotateKernel(kernel_1); kernel_2 = RotateKernel(kernel_2); } break; default: return(0); break; } int maxSum = convolutionValues.Max(); if (maxSum > 255) { return(255); } else if (maxSum < 0) { return(0); } else { return(maxSum); } }