OpenCV putText在翻转imag后不起作用

2024-10-03 13:23:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从相机抓取图像,并将其左右翻转,这样视图的效果就像一面镜子。不过,我也喜欢在视图中添加一些文本,但结果是使用np.fliplr(frame)翻转图像后,cv.putText就不再工作了。在

下面是我使用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()

带翻转的结果帧: enter image description here

不翻转的结果帧: enter image description here


Tags: 图像import视图helloworldifversionnp
1条回答
网友
1楼 · 发布于 2024-10-03 13:23:34

我怀疑是因为cv2.putTextnp.array不兼容,后者是np.fliplr(frame)的返回值。我建议您改用frame = cv2.flip(frame, 1)。在

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()

相关问题 更多 >