Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flip image with opencv and python( without cv2.flip)

Tags:

python

opencv

i just want to flip image vertically without cv2.flip(), but output is completely black image. where is my mistake ?

import cv2
import numpy as np


def flipv(imgg):
    for i in range(480):
        img2= np.zeros([480, 640, 3], np.uint8)
        img2[i,:]=imgg[480-i-1,:]

    return img2

img = cv2.imread("foto\\test.jpg", 1)

ads= flipv(img)

cv2.imshow("qw",ads)

cv2.waitKey(0)
cv2.destroyAllWindows()
like image 226
Enes Avatar asked Jul 14 '18 19:07

Enes


People also ask

How do you flip an image in Python?

To flip an Image vertically or horizontally with Python pillow, use transpose() method on the PIL Image object.

How do I mirror in OpenCV?

Flip image with OpenCV: cv2. flip() . Specify the original ndarray as the first argument and a value indicating the directions as the second argument flipCode . The image is flipped according to the value of flipCode as follows: flipcode = 0 : flip vertically.

How do you flip vertically in Python?

To vertically flip an image (flip the image about its vertical axis), we use the flip() function. The flip() function takes in 2 parameters. The first parameter is the image you want to flip. The second parameter is 0 (for vertical flipping).


1 Answers

Simple function to flip images using opencv:

def flip(img,axes):
    if (axes == 0) :
        #horizental flip
        return cv2.flip( img, 0 )
    elif(axes == 1):
        #vertical flip
        return cv2.flip( img, 1 )
    elif(axes == -1):
        #both direction
        return cv2.flip( img, -1 ) 
bflp = flip(img,-1)
plt.imshow(bflp)
like image 112
FatiHe Avatar answered Oct 01 '22 01:10

FatiHe