Assignment 1: Problem Solving Example


First, an outline

 

Let's start with what we know:

 

A1.3 Exercise: Before reading ahead, think about what needs to go into the method as parameters, and what the structure of that method ought to be.


Next steps: the nitty gritty of the computation

 

Notice that once we know which cell we're doing the average for, all we need is the location in the first array ... and of course the array itself.

                B[i][j] = boxAverage (A, i, j)
    
We'll call it boxAverage(). It returns a single number (the neighbor average) that goes into the i,j-th element of B.

What's nice is that, all we need now is the details of this method:

 

A1.4 Exercise: Before reading ahead, try thinking through this.

 

A1.4 Exercise: In StrangeAverage2D.java write up all the above code and complete the remaining parts to make it work.

To test your code, use the following in main()

    public static void main (String[] argv)
    {
	// Test 1:
	double[][] A = {
	    {0,1,0,1,0,1},
	    {1,0,1,0,1,0},
	    {0,1,0,1,0,1}
	};

	print (strangeAverage(A));

	// Test 2:
	double[][] A2 = {
	    {1,2,3,4},
	    {5,6,7,8},
	    {9,10,11,12},
	    {13,14,15,16}
	};

	print (strangeAverage(A2));
    }

    static void print (double[][] A)
    {
        // Neater printing with printf:
	for (int i=0; i<A.length; i++) {
	    for (int j=0; j<A[i].length; j++) {
		System.out.printf (" %6.3f", A[i][j]);
	    }
	    System.out.println ();
	}
	
    }
    
You should get
  0.500  0.500  0.500  0.500  0.500  0.500
  0.500  0.444  0.556  0.444  0.556  0.500
  0.500  0.500  0.500  0.500  0.500  0.500

  3.500  4.000  5.000  5.500
  5.500  6.000  7.000  7.500
  9.500 10.000 11.000 11.500
 11.500 12.000 13.000 13.500
    

An application

 

When your code is working, replace the code in main() with

        // Display an image, unmodified:
	ImageTool imTool = new ImageTool ();
	int[][] pixels = imTool.imageFileToGreyPixels ("eniac.jpg");
	imTool.showImage (pixels, "ENIAC");

        // Our strange-average uses a double array, so copy over
        // the integer pixels into a double 2D array:
	double[][] A = convertToDouble (pixels);

        // Our strange average:
	double[][] B = strangeAverage (A);

        // Convert back to an integer array, ENSURING that all
        // values are between 0 and 255. (Truncate if out of these bounds).
	int[][] pixels2 = convertToPixels (B);

        // Display the resulting image:
	imTool.showImage (pixels2, "ENIAC2");
    
 

A1.5 Exercise: Write the additional code needed to make this work. What is the effect on an image. You can use eniac.jpg or your own black-and-white JPEG image. You will also need ImageTool.java.

So, at last we know why the strange averaging method was used.

About this type of averaging:

  • This is one of several such "image operators", small computations that act on images.
  • They are called convolutions in the image processing literature.
  • Variations of the basic procedure allows one to sharpen an image, reduce noise, identify edges and so on.


Back to the assignment



© 2017, Rahul Simha