v9.1.1
Menu

Flip image

easyFREEImage manipulationLinear AlgebraCV - DSA

Implement a function to manipulate a given n×nn \times n binary image matrix, flipping it horizontally and then inverting it. The goal is to understand how image transformation and binary inversion work together to produce the resulting image. In image processing, flipping an image horizontally involves reversing the order of elements in each row, which can be represented as [a,b,c][a, b, c] becoming [c,b,a][c, b, a]. Inverting replaces every 00 with 11 and every 11 with 00:

f(x)=1−x\begin{aligned} f(x) &= 1 - x \end{aligned}

This technique is widely used in data augmentation for training machine learning models.

Example

Input
image = [[1,1,0],[1,0,1],[0,0,0]]
Output
[[1,0,0],[0,1,0],[1,1,1]]
Reasoning
  • image is a 3×33 \times 3 binary picture: row ii, column jj holds pixel xijx_{ij}, where 11 is a lit pixel and 00 is a dark one.
  • A horizontal flip mirrors the picture left to right, so column jj of the result takes the pixel from column n−1−jn-1-j. Row [1,1,0] becomes [0,1,1][0,1,1], row [1,0,1] is a palindrome and stays [1,0,1][1,0,1], and the all-dark row [0,0,0][0,0,0] is unchanged.
  • Inverting applies f(x)=1−xf(x) = 1 - x to every pixel: [0,1,1]→[1,0,0][0,1,1] \to [1,0,0], [1,0,1]→[0,1,0][1,0,1] \to [0,1,0], [0,0,0]→[1,1,1][0,0,0] \to [1,1,1].
  • Combined, each output pixel is oij=1−xi, n−1−jo_{ij} = 1 - x_{i,\,n-1-j}, giving [[1,0,0],[0,1,0],[1,1,1]]: still 3×33 \times 3, still only 00s and 11s.
  • Notice that rows never move up or down, only within themselves. Since the flip moves pixels and the inversion changes values independently, doing them in the other order gives the same picture.

Constraints

n == image.length n == image[i].length 1 <= n <= 20

More from CV - DSA

solution.py

Test results

Sign in to run your code against the test cases.