Ejemplo n.º 1
0
 /// <summary>
 /// boxBlur() returns a blurred version of "this" PixImage.
 ///
 /// If numIterations == 1, each pixel in the output PixImage is assigned
 /// a value equal to the average of its neighboring pixels in "this" PixImage,
 /// INCLUDING the pixel itself.
 ///
 /// A pixel not on the image boundary has nine neighbors--the pixel itself and
 /// the eight pixels surrounding it.  A pixel on the boundary has six
 /// neighbors if it is not a corner pixel; only four neighbors if it is
 /// a corner pixel.  The average of the neighbors is the sum of all the
 /// neighbor pixel values (including the pixel itself) divided by the number
 /// of neighbors, with non-integer quotients rounded toward zero (as C# does
 /// naturally when you divide two integers).
 ///
 /// Each color (red, green, blue) is blurred separately.  The red input should
 /// have NO effect on the green or blue outputs, etc.
 ///
 /// The parameter numIterations specifies a number of repeated iterations of
 /// box blurring to perform.  If numIterations is zero or negative, "this"
 /// PixImage is returned (not a copy).  If numIterations is positive, the
 /// return value is a newly constructed PixImage.
 ///
 /// IMPORTANT:  DO NOT CHANGE "this" PixImage!!!  All blurring/changes should
 /// appear in the new, output PixImage only.
 /// </summary>
 /// <param name="numIterations"> the number of iterations of box blurring. </param>
 /// <returns> a blurred version of "this" PixImage. </returns>
 public virtual PixImage BoxBlur(int numIterations)
 {
     if (numIterations > 0)
     {
         //make a new image
         var newImage = new PixImage(this.Width, this.Height);
         for (int i = 0; i < this.Width; i++)
         {
             for (int j = 0; j < this.Height; j++)
             {
                 Pixel newPixel = this.GetBlurredPixel(i, j);
                 newImage.setPixel(i, j, newPixel.Red, newPixel.Green, newPixel.Blue);
                 // Console.WriteLine("At pixel {0},{1}: {2}, {3}, {4}. Counter: {5}", i, j, red, green, blue, counter);
             }
         }
         for (int n = 0; n < numIterations; n++)
         {
             newImage = newImage.BoxBlur(n);
         }
         return(newImage);
     }
     return(this);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// main() runs a series of tests to ensure that the convolutions (box blur
        /// and Sobel) are correct.
        /// </summary>
        public static void Main(string[] args)
        {
            // Be forwarned that when you write arrays directly in C# as below,
            // each "row" of text is a column of your image--the numbers get
            // transposed.
            PixImage image1 = array2PixImage(new int[][]
            {
                new int[] { 0, 10, 240 },
                new int[] { 30, 120, 250 },
                new int[] { 80, 250, 255 }
            });

            Console.WriteLine("Testing getWidth/getHeight on a 3x3 image.  " + "Input image:");
            Console.Write(image1);
            doTest(image1.Width == 3 && image1.Height == 3, "Incorrect image width and height.");

            Console.WriteLine("Testing blurring on a 3x3 image.");
            doTest(image1.BoxBlur(1).Equals(array2PixImage(new int[][]
            {
                new int[] { 40, 108, 155 },
                new int[] { 81, 137, 187 },
                new int[] { 120, 164, 218 }
            })), "Incorrect box blur (1 rep):\n" + image1.BoxBlur(1));
            doTest(image1.BoxBlur(2).Equals(array2PixImage(new int[][]
            {
                new int[] { 91, 118, 146 },
                new int[] { 108, 134, 161 },
                new int[] { 125, 151, 176 }
            })), "Incorrect box blur (2 rep):\n" + image1.BoxBlur(2));
            doTest(image1.BoxBlur(2).Equals(image1.BoxBlur(1).BoxBlur(1)), "Incorrect box blur (1 rep + 1 rep):\n" + image1.BoxBlur(2) + image1.BoxBlur(1).BoxBlur(1));

            Console.WriteLine("Testing edge detection on a 3x3 image.");
            doTest(image1.SobelEdges().Equals(array2PixImage(new int[][]
            {
                new int[] { 104, 189, 180 },
                new int[] { 160, 193, 157 },
                new int[] { 166, 178, 96 }
            })), "Incorrect Sobel:\n" + image1.SobelEdges());

            PixImage image2 = array2PixImage(new int[][]
            {
                new int[] { 0, 100, 100 },
                new int[] { 0, 0, 100 }
            });

            Console.WriteLine("Testing getWidth/getHeight on a 2x3 image.  " + "Input image:");
            Console.Write(image2);
            doTest(image2.Width == 2 && image2.Height == 3, "Incorrect image width and height.");

            Console.WriteLine("Testing blurring on a 2x3 image.");
            doTest(image2.BoxBlur(1).Equals(array2PixImage(new int[][]
            {
                new int[] { 25, 50, 75 },
                new int[] { 25, 50, 75 }
            })), "Incorrect box blur (1 rep):\n" + image2.BoxBlur(1));

            Console.WriteLine("Testing edge detection on a 2x3 image.");
            doTest(image2.SobelEdges().Equals(array2PixImage(new int[][]
            {
                new int[] { 122, 143, 74 },
                new int[] { 74, 143, 122 }
            })), "Incorrect Sobel:\n" + image2.SobelEdges());
            Console.ReadLine();
        }