简体   繁体   中英

OpenCV putText does not work after flipping the image

I'd like to grab images from the camera and flip them left/right so that the view performs like a mirror. However, I also like to add some text to the view, but it turns out that after flipping the image using np.fliplr(frame) , cv.putText does no longer work.

Here is my minimal example using python 3.5.2 :

import numpy as np
import cv2
import platform

if __name__ == "__main__":
    print("python version:", platform.python_version())
    cap = cv2.VideoCapture(0)
    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()

        cv2.putText(frame,'Hello World : Before flip',(100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255),2,cv2.LINE_AA)
        frame = np.fliplr(frame)
        cv2.putText(frame,'Hello World : After flip',(100, 200), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255),2,cv2.LINE_AA)

        # Process the keys
        key = cv2.waitKey(1) & 0xFF
        if key == ord('q'):
            print("quit")
            break
        # show the images
        cv2.imshow('frame',frame)

    cap.release()
    cv2.destroyAllWindows()

Resulting frame w/ flip: 在此处输入图片说明

Resulting frame w/o flip: 在此处输入图片说明

I suspect it's due to cv2.putText is not compatible with np.array which is the return value of np.fliplr(frame) . I suggest that you use frame = cv2.flip(frame, 1) instead.

import numpy as np
import cv2
import platform

if __name__ == "__main__":
    print("python version:", platform.python_version())
    cap = cv2.VideoCapture(0)
    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()

        cv2.putText(frame,'Hello World : Before flip',(100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255),2,cv2.LINE_AA)
        frame = cv2.flip(frame, 1)
        cv2.putText(frame,'Hello World : After flip',(100, 200), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255),2,cv2.LINE_AA)

        # Process the keys
        key = cv2.waitKey(1) & 0xFF
        if key == ord('q'):
            print("quit")
            break
        # show the images
        cv2.imshow('frame',frame)

    cap.release()
    cv2.destroyAllWindows()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM