Opencv旋转相机模式
Opencv rotate camera mode
我想使用中断按钮将相机视图顺时针旋转 90 度。但是当我单击一次并松开按钮时,按钮的状态会恢复到默认状态。结果,在单击按钮时,相机会旋转一个实例,然后在 while 循环中返回到默认模式。如何解决这个问题?
这是我的片段:
import cv2
cap = cv2. VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('null', frame)
if (button):
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow('null', frame)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
如有任何帮助,我们将不胜感激。
import cv2
cap = cv2. VideoCapture(0)
button_is_pressed = false;
while True:
ret, frame = cap.read()
if (button):
button_is_pressed = not button_is_pressed
if(button_is_pressed)
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow('null', frame)
cap.release()
我会尝试这样的事情,当你按下按钮时,它会改变变量的状态 button_is_pressed,同时保持不变,它会继续旋转图像。
你应该使用额外的变量 - rotate = False
- 来保持这个状态
rotate = False
while True:
ret, frame = cap.read()
# without imshow()
if button_a.is_pressed():
rotate = True
if rotate:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
# outside `if`
cv2.imshow('null', frame)
这样它将 rotate
从 False
更改为 True
但永远不会从 True
更改为 False
编辑:
如果下一次按下要再旋转 90 度(至 180 度),则需要更复杂的代码。它会检查前一个循环中的 is_pressed()
是否为 False
并且在当前循环中是否为 True
- 到 运行 它只在长时间按下按钮时检查一次。
像这样。
我用普通的space
测试了一下
import cv2
cap = cv2.VideoCapture(0)
rotate = 0
is_pressed = False
previous = False
current = False
while True:
ret, frame = cap.read()
# without imshow()
current = is_pressed
if (previous is False) and (current is True):
rotate = (rotate + 90) % 360
print(rotate)
previous = current
if rotate == 90:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
elif rotate == 180:
frame = cv2.rotate(frame, cv2.ROTATE_180)
elif rotate == 270:
frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
# outside `if`
cv2.imshow('null', frame)
key = cv2.waitKey(40) & 0xff
if key == 27:
break
is_pressed = (key == 32)
cv2.destroyAllWindows()
cap.release()
我想使用中断按钮将相机视图顺时针旋转 90 度。但是当我单击一次并松开按钮时,按钮的状态会恢复到默认状态。结果,在单击按钮时,相机会旋转一个实例,然后在 while 循环中返回到默认模式。如何解决这个问题?
这是我的片段:
import cv2
cap = cv2. VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('null', frame)
if (button):
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow('null', frame)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
如有任何帮助,我们将不胜感激。
import cv2
cap = cv2. VideoCapture(0)
button_is_pressed = false;
while True:
ret, frame = cap.read()
if (button):
button_is_pressed = not button_is_pressed
if(button_is_pressed)
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow('null', frame)
cap.release()
我会尝试这样的事情,当你按下按钮时,它会改变变量的状态 button_is_pressed,同时保持不变,它会继续旋转图像。
你应该使用额外的变量 - rotate = False
- 来保持这个状态
rotate = False
while True:
ret, frame = cap.read()
# without imshow()
if button_a.is_pressed():
rotate = True
if rotate:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
# outside `if`
cv2.imshow('null', frame)
这样它将 rotate
从 False
更改为 True
但永远不会从 True
更改为 False
编辑:
如果下一次按下要再旋转 90 度(至 180 度),则需要更复杂的代码。它会检查前一个循环中的 is_pressed()
是否为 False
并且在当前循环中是否为 True
- 到 运行 它只在长时间按下按钮时检查一次。
像这样。
我用普通的space
测试了一下
import cv2
cap = cv2.VideoCapture(0)
rotate = 0
is_pressed = False
previous = False
current = False
while True:
ret, frame = cap.read()
# without imshow()
current = is_pressed
if (previous is False) and (current is True):
rotate = (rotate + 90) % 360
print(rotate)
previous = current
if rotate == 90:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
elif rotate == 180:
frame = cv2.rotate(frame, cv2.ROTATE_180)
elif rotate == 270:
frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
# outside `if`
cv2.imshow('null', frame)
key = cv2.waitKey(40) & 0xff
if key == 27:
break
is_pressed = (key == 32)
cv2.destroyAllWindows()
cap.release()