简体   繁体   English

如何根据 tkinter window 从 cv2 调整视频 stream 的大小

[英]How to resize video stream from cv2 according to tkinter window

Here is a code in which I just want to show the stream from a video camera on a Tkinter label.这是一个代码,我只想在 Tkinter label 上的摄像机中显示 stream。 Now the problem is when someone tries to resize the Tkinter window, the stream should resize accordingly maintaining the same aspect ratio.现在的问题是,当有人试图调整 Tkinter window 的大小时,stream 应该相应地调整大小,保持相同的纵横比。 I tried some tweaks but those did not work out.我尝试了一些调整,但没有成功。 Below is my code.下面是我的代码。

root.bind( "<Configure>", resize )

width = cap. get(cv2. CAP_PROP_FRAME_WIDTH )
height = cap. get(cv2. CAP_PROP_FRAME_HEIGHT )
k = int(compute_hcf(int(width),int(height)))
w_ratio = width//k
h_ratio = height//k

def resize(event):
    h = root.winfo_height()
    w = root.winfo_width()
    k = min(h,w)
    print(k)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, k//w_ratio)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, k//h_ratio)

This gives me the following error while trying to resize the window.在尝试调整 window 的大小时,这给了我以下错误。

[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (436) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback [警告:0] 全局 C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (436) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB 终止异步回调

If anyone can help, Thank you.如果有人可以帮忙,谢谢。

So after reading your code, it seems you are overcomplicating the issue by trying to create an event that resizes it for you.因此,在阅读了您的代码之后,您似乎通过尝试创建一个为您调整其大小的事件来使问题过于复杂。

So I've gone back to basics and just created a webcam stream that will resize whenever you resize the window while maintaining the aspect ratio.所以我回到了基础,刚刚创建了一个网络摄像头 stream,只要您调整 window 的大小,它就会调整大小,同时保持纵横比。

Here is some code that I've found and modified, it doesn't use tkinter , but it's not that important because you can incorporate this in wherever you see fit.这是我找到并修改的一些代码,它不使用tkinter ,但这并不重要,因为您可以将其合并到任何您认为合适的地方。

The only major issue I have with this, is when resizing you may find that your window will freeze, but only for a few seconds, it will come back after a while, you wont actually freeze the image, just window control.我唯一遇到的主要问题是,在调整大小时,您可能会发现 window 会冻结,但只有几秒钟,它会在一段时间后恢复,您实际上不会冻结图像,只是 window 控件。

You may need to use python multithreading , I actually might try and do that this evening because I find this quite interesting.您可能需要使用python multithreading ,实际上我今晚可能会尝试这样做,因为我觉得这很有趣。

import cv2

def main():

    windowName = "Top"
    cv2.namedWindow(windowName)
    cap = cv2.VideoCapture(0)

    cap.set(3, 650)
    cap.set(4, 750)
    
    if cap.isOpened():
        ret, frame = cap.read()
    else:
        ret = False

    while ret:
        ret, frame = cap.read()
        cv2.imshow(windowName, frame)
        if cv2.waitKey(1) == 27:
            break

    cv2.destroyAllWindow()

    cap.release()   
if __name__== "__main__":

    main() 

But in terms of actual functionality, this will let you stream your camera and resize the window without it cropping out, maintaining the same aspect ratio.但就实际功能而言,这将使您的相机 stream 并调整 window 的大小而不会裁剪,保持相同的纵横比。

if you have any questions let me know如果您有任何问题,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM