Question
A gray-level image is sometimes stored as a list ofint values. The values represent the intensity of light at discrete positions in the image. An
A gray-levelimage is sometimes stored as a list ofint values. The values represent the intensity of light at discrete positions in the image. An image may be smoothed by replacing each element with the average of the element's neighboring elements.
Say that the original values are in the 2D array "image". Compute the smoothed array by doing this: Each value ofsmooth[r][c] is the average of nine values:
image[r-1][c-1], image[r-1][c ], image[r-1][c+1], image[r ][c-1], image[r ][c ], image[r ][c+1], image[r+1][c-1], image[r+1][c ], image[r+1][c+1].
Assume that the image is rectangular, that is, all rows have the same number of columns. Use integer arithmetic for this so that the values inSmooth are integers.
import java.io.* ; class Smooth { public static void main ( String[] args ) { int[][] image = {{0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,5,5,5,5,5,5,5,5,0,0}, {0,0,5,5,5,5,5,5,5,5,0,0}, {0,0,5,5,5,5,5,5,5,5,0,0}, {0,0,5,5,5,5,5,5,5,5,0,0}, {0,0,5,5,5,5,5,5,5,5,0,0}, {0,0,5,5,5,5,5,5,5,5,0,0}, {0,0,5,5,5,5,5,5,5,5,0,0}, {0,0,5,5,5,5,5,5,5,5,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0}}; // assume a rectangular image int[][] smooth = new int[ image.length ][ image[0].length ]; // Compute the smoothed value for // non-edge locations in the image. for ( int row=1; row
The edges of the image are a problem because only some of the nine values that go into the average exist. There are various ways to deal with this problem:
- Easy (shown above): Leave all the edge locations in the smoothed image to zero. Only inside locations get an averaged value from the image.
- Harder: Copy values at edge locations directly to the smoothed image without change.
- Hard: For each location in the image, average together only those of the nine values that exist. So at the edges of the image average six values. At the corners, average four values. This calls for some fairly trickyif statements, or a tricky set offor statements inside the outer two.
Here is a sample run of the hard solution:
Input: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 5 5 5 5 5 5 5 0 0 0 0 5 5 5 5 5 5 5 5 0 0 0 0 5 5 5 5 5 5 5 5 0 0 0 0 5 5 5 5 5 5 5 5 0 0 0 0 5 5 5 5 5 5 5 5 0 0 0 0 5 5 5 5 5 5 5 5 0 0 0 0 5 5 5 5 5 5 5 5 0 0 0 0 5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Output: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 1 2 3 3 3 3 3 3 2 1 0 0 1 3 5 5 5 5 5 5 3 1 0 0 1 3 5 5 5 5 5 5 3 1 0 0 1 3 5 5 5 5 5 5 3 1 0 0 1 3 5 5 5 5 5 5 3 1 0 0 1 3 5 5 5 5 5 5 3 1 0 0 1 3 5 5 5 5 5 5 3 1 0 0 1 2 3 3 3 3 3 3 2 1 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Once you have the program working using the array example in the above code, modify it so that the user can input the data from the keyboard. Try creating some interesting size and varieties of arrays.
Requirements: 2D arrays, bounds checking, looping, input redirection
Step by Step Solution
3.40 Rating (147 Votes )
There are 3 Steps involved in it
Step: 1
Heres the modified code that allows the user to input the ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started