Home > Blockchain >  How to flip iamge in opencv without library functions in python
How to flip iamge in opencv without library functions in python

Time:02-20

I am trying to flip an image in python but cannot use cv2.flip() as per my design brief. Dose anyone has any ideas to help me? Here is my code so far:

import cv2
import numpy as np

img = cv2.imread('img/bozu.png')
img = cv2.resize(img, (512, 512))

Thanks!

CodePudding user response:

You can flip the image like this as well.

import cv2
import numpy as np

img = cv2.imread('img/bozu.png')
img = cv2.resize(img, (512, 512))

# flip vertically
v_flip = img[::-1]

# flip horizontally
h_flip = img[:, ::-1]

# this is bgr to rgb
bgr2rgb = img[:, :, ::-1]

CodePudding user response:

cv2 uses numpy arrays to manipulate images.
Therefore Your img is 2D numpy array. You can use numpy flip method to flip it.

Here is example code snippet:

import cv2
import numpy as np

img = cv2.imread('img/bozu.png')
img = cv2.resize(img, (512, 512))

# Flip image vertically
v_flip = np.flip(img, axis=0)

# Flip image horizontally
h_flip = np.flip(img, axis=1)
  • Related