Reputation:
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.
image=cv2.imread('boat.jpg',1)
height,width,channel=image.shape
list1=[]
list2=[]
for i in range(height):
for j in range(width):
list1.append(image[i,j])
for a in range(len(list1)-1,-1,-1):
list2.append(list1[a])
b=0
for i in range(height):
for j in range(width):
image[i,j]=list2[b]
b+=1
But the flipped image is looking like this: https://ibb.co/KKVkd2d What am i doing wrong?
Upvotes: 0
Views: 4356
Reputation: 142631
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.
import cv2
import numpy
image = cv2.imread('boat.jpg', 1)
image = numpy.array(list(reversed(image)))
cv2.imshow('window', image)
cv2.waitKey(0)
BTW: if you want to flip horizontally then you have to reverse pixels in rows.
import cv2
import numpy
image = cv2.imread('boat.jpg', 1)
image = numpy.array([list(reversed(row)) for row in image])
cv2.imshow('window', image)
cv2.waitKey(0)
Upvotes: 1