gpt4 book ai didi

python - 翻转图像后,OpenCV putText不起作用

转载 作者:行者123 更新时间:2023-12-02 16:48:36 28 4
gpt4 key购买 nike

我想从相机中抓取图像并将其左右翻转,以使 View 的表现像一面镜子。但是,我也想在 View 中添加一些文本,但是事实证明,在使用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

最佳答案

我怀疑是由于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()

关于python - 翻转图像后,OpenCV putText不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54952873/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com