如何在全屏中正确显示OpenCV肖像模式?

发布于 2025-01-29 16:44:37 字数 2517 浏览 4 评论 0原文

我试图通过python中的OpenCV稳定地显示景观肖像模式。

我正在使用一个小的 2.8“ TFT屏幕 ,分辨率为 240 x 320 ,该分辨率附加到运行Debian Buster。 我使用的网络摄像头具有以下支持的分辨率:

640x480 352x288 320x240 176x144 160x120

使用 cv2.wnd_prop_fullscreen 屏幕以灰色边缘在顶部和底部开始。

cap = cv2.VideoCapture(0)  
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)  
cv2.namedWindow('default', cv2.WND_PROP_FULLSCREEN)  
cv2.setWindowProperty('default', cv2.WND_PROP_FULLSCREEN,
                      cv2.WINDOW_FULLSCREEN)  

while True:
    ret, frame = cap.read() 
    if viewMode == "normal": 
        cv2.imshow("default", frame)

然后,我使用 cv2.Rotate_90_clockwise 旋转它以获得景观模式。

    elif viewMode == "landscape": 
        frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) 
        cv2.imshow("default", frame)

它可以通过适当的缩放效果,而无需在x和y轴上拉伸。

要在全屏中获得肖像模式,我使用 cv2.window_freeratio 。但是它以Y轴为侧面,并且显示不好。

    elif viewMode == "portrait": 
        cv2.namedWindow('freeratio', cv2.WINDOW_FREERATIO)
        cv2.setWindowProperty(
            'freeratio', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
        cv2.imshow("freeratio", frame)

在这种情况下,显示肖像模式的正确方法是什么,而不会在两个轴上进行适当的缩放?

这是下面我的完整代码:

import cv2 

# viewMode = "normal"  
# viewMode = "landscape"  
viewMode = "portrait" 

cap = cv2.VideoCapture(0)  
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)  
cv2.namedWindow('default', cv2.WND_PROP_FULLSCREEN)  
cv2.setWindowProperty('default', cv2.WND_PROP_FULLSCREEN,
                      cv2.WINDOW_FULLSCREEN)  

while True:
    ret, frame = cap.read() 
    if viewMode == "normal": 
        cv2.imshow("default", frame)
    elif viewMode == "landscape": 
        frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) 
        cv2.imshow("default", frame)
    elif viewMode == "portrait": 
        cv2.namedWindow('freeratio', cv2.WINDOW_FREERATIO)
        cv2.setWindowProperty(
            'freeratio', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
        cv2.imshow("freeratio", frame)

    if cv2.waitKey(5) & 0xFF == 27:
        break

cap.release()

注意: 我尝试了在其他类似帖子/线程上发现的大多数建议,但无济于事。调整/裁剪或手动设定分辨率无济于事。它要么给我带来底部和底部的灰色边界,要么被划掉。网络摄像头/Debian中是否有任何造成问题的设置?

任何帮助/建议将不胜感激。

I am trying to corectly show Landscape and Portrait mode with OpenCv in Python.

I am using a small 2.8" TFT screen with a resolution of 240 x 320 attached to a micro computer that is running Debian buster.
The webcam I am using has the following supported resolutions:

640x480 352x288 320x240 176x144 160x120

With the cv2.WND_PROP_FULLSCREEN the screen starts with grey edges at top and bottom.

cap = cv2.VideoCapture(0)  
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)  
cv2.namedWindow('default', cv2.WND_PROP_FULLSCREEN)  
cv2.setWindowProperty('default', cv2.WND_PROP_FULLSCREEN,
                      cv2.WINDOW_FULLSCREEN)  

while True:
    ret, frame = cap.read() 
    if viewMode == "normal": 
        cv2.imshow("default", frame)

Then I rotate it with cv2.ROTATE_90_CLOCKWISE to get the Landscape mode.

    elif viewMode == "landscape": 
        frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) 
        cv2.imshow("default", frame)

It works fine with proper scaling without stretching in both X and Y axes.

To get the Portrait mode in full screen, I use cv2.WINDOW_FREERATIO. But it streches in Y axis and the display is not good.

    elif viewMode == "portrait": 
        cv2.namedWindow('freeratio', cv2.WINDOW_FREERATIO)
        cv2.setWindowProperty(
            'freeratio', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
        cv2.imshow("freeratio", frame)

What is the correct way to show Portrait mode in this case without streching and maintaining proper scaling in both axes?

Here's my full code below:

import cv2 

# viewMode = "normal"  
# viewMode = "landscape"  
viewMode = "portrait" 

cap = cv2.VideoCapture(0)  
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)  
cv2.namedWindow('default', cv2.WND_PROP_FULLSCREEN)  
cv2.setWindowProperty('default', cv2.WND_PROP_FULLSCREEN,
                      cv2.WINDOW_FULLSCREEN)  

while True:
    ret, frame = cap.read() 
    if viewMode == "normal": 
        cv2.imshow("default", frame)
    elif viewMode == "landscape": 
        frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) 
        cv2.imshow("default", frame)
    elif viewMode == "portrait": 
        cv2.namedWindow('freeratio', cv2.WINDOW_FREERATIO)
        cv2.setWindowProperty(
            'freeratio', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
        cv2.imshow("freeratio", frame)

    if cv2.waitKey(5) & 0xFF == 27:
        break

cap.release()

Note: I have tried most suggestions I found on other similar posts/threads but to no avail. Resizing/cropping or manually setting the resolution does not help. It either gives me the greyed borders on topand bottom or streched out. Could it be any setting in the webcam/Debian that's causing the issue?

Any help/suggestions is appreciated.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文