I’m trying to flip an image vertically without using any default flip() or similar functions.I tried to iterate along the pixels and by using loops tried to reverse it so i can flip the image vertically.
JavaScript
x
15
15
1
image=cv2.imread('boat.jpg',1)
2
height,width,channel=image.shape
3
list1=[]
4
list2=[]
5
for i in range(height):
6
for j in range(width):
7
list1.append(image[i,j])
8
for a in range(len(list1)-1,-1,-1):
9
list2.append(list1[a])
10
b=0
11
for i in range(height):
12
for j in range(width):
13
image[i,j]=list2[b]
14
b+=1
15
But the flipped image is looking like this: https://ibb.co/KKVkd2d What am i doing wrong?
Advertisement
Answer
To flip vertically you have to reverse rows in array – first row has to be last, last row has to be first. You don’t have to move pixels in rows.
JavaScript
1
11
11
1
import cv2
2
import numpy
3
4
image = cv2.imread('boat.jpg', 1)
5
6
image = numpy.array(list(reversed(image)))
7
8
cv2.imshow('window', image)
9
10
cv2.waitKey(0)
11
BTW: if you want to flip horizontally then you have to reverse pixels in rows.
JavaScript
1
11
11
1
import cv2
2
import numpy
3
4
image = cv2.imread('boat.jpg', 1)
5
6
image = numpy.array([list(reversed(row)) for row in image])
7
8
cv2.imshow('window', image)
9
10
cv2.waitKey(0)
11