OPENCV旋转相机模式

发布于 2025-01-28 07:18:36 字数 460 浏览 2 评论 0原文

我想通过中断按钮将相机视图顺时针旋转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()

任何帮助将不胜感激。

I want to rotate the camera view by 90 degree clockwise with an interrupt button. But the button's state goes back to it's default state when I click once and unpress the button. As a result, on clicking the button, the camera rotates for an instance and then goes back to the default mode in the while loop. How to solve this issue?

Here is my snippet:

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

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染年凉城似染瑾 2025-02-04 07:18:36

您应该使用额外的变量 - rowate = 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)

这种方式保留此状态false> false true true true true 但永远不要从true false


编辑:

如果下一个按下应该旋转90度(至180),则需要更复杂的代码。它将检查上一个循环中的is_presse()是否为false,并且在当前循环中为true - 仅在按下按钮长时间按下按钮时仅运行一次时间。

这样的东西。

我使用普通空格对其进行测试。

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

You should use extra variable - rotate = False - to keep this state

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)

This way it changes rotate from False to True but never from True to False


EDIT:

If next press should rotate another 90 degrees (to 180) then it would need more complex code. It would check if is_pressed() in previous loop was False and in current loop is True - to run it only once when button is pressed long time.

Something like this.

I used normal space to test it.

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()
唔猫 2025-02-04 07:18:36
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的状态,而仍然没有变化,它会持续旋转图像。

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

I would try something like this, when you press the button it changes the state of variable button_is_pressed, while still unchanged it keeps rotating image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文