Invert or Blur an Image
Problem statement
A grayscale image is represented by a rectangular integer matrix image. Each entry is a pixel value from 0 through 255.
Apply the operation selected by filter and return a new matrix:
- If
filter == 1, reflect every row across the image's vertical axis. - If
filter == 2, blur each pixel using the original image. Replace it with the arithmetic mean of all existing neighboring pixels among the eight surrounding positions, rounded to the nearest integer. Because values are nonnegative, a fractional part of exactly0.5rounds up.
When a pixel has no neighbors, keep its original value. Do not use already blurred values while computing another pixel.
Function
applyImageFilter(image: int[][], filter: int) → int[][]Examples
Example 1
image = [[1,2,3],[4,5,6]]filter = 1return = [[3,2,1],[6,5,4]]Reflecting across the vertical axis reverses each row.
Example 2
image = [[0,0,0],[0,255,0],[0,0,0]]filter = 2return = [[85,51,85],[51,0,51],[85,51,85]]Each corner sees three neighbors, each edge-center sees five, and the center sees eight. Every average uses the original image and rounds to the nearest integer.
Constraints
1 <= image.length <= 2001 <= image[r].length <= 200- All rows have the same length.
0 <= image[r][c] <= 255filteris1or2.