Write a static method blur(double[][]picture) that you could use on a part of a picture file to

Question:

Write a static method blur(double[][]picture) that you could use on a part of a picture file to obscure a detail such as a person’s face or a license plate number. This method computes the weighted averages of the values in picture and returns them in a new twodimensional array. To find a weighted average of a group of numbers, you count some of them more than others. Thus, you multiply each item by its weight, add these products together, and divide the result by the sum of the weights.

For each element in picture, compute the weighted average of the element and its immediate neighbors. Store the result in a new two-dimensional array in the same position that the element occupies in picture. This new array is the one the method returns.

The neighbors of an element in picture can be above, below, to the left of, and to the right of it, either vertically, horizontally, or diagonally. So each weighted average in the new array will be a combination of up to nine values from the array picture. A corner value will use only four values: itself and three neighbors. An edge value will use only six values: itself and five neighbors. But an interior value will use nine values: itself and eight neighbors. So you will need to treat the corners and edges separately from the other cells.

The weights to use are:

1       2       1
2       4       2
1       2       1


The element itself has the highest weight of 4, the horizontal and vertical neighbors have a weight of 2, and the diagonal neighbors have a weight of 1.

For example, suppose the values in picture are

1.2 1.3 4.5 6.0 2.7 1.7 3.3 4.4 10.5 17.0 1.1 4.5 2.1 25.3 9.2 1.0 9.5 8.3 2.9 2.1


and the new array is called result. In assigning weights, we will arbitrarily start with an element and consider neighbors in a clockwise direction. Thus, the interior value in result[1]

[1]is equal to


To arrive at this equation, we started with the element at picture[1][1] and then, beginning with the neighbor to the right, we considered neighbors in a clockwise direction. The corner value in result[0][0]is equal to


Note that picture[0][0] has fewer neighbors than an interior value such as picture[1] [1]. The same is true for an edge value such as picture[0][1]. Thus, the edge value in result[0][1]is equal to


The final array, result, would be

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Question Posted: