如何使用.set()在python cv2中调整实时视频的大小?

如何使用.set()在python cv2中调整实时视频的大小?,python,opencv,Python,Opencv,通过本教程,我正在学习opencv,我已经完成了第3部分(调整大小和缩放)。到最后,他创建了这个功能,应该更好地用于现场视频。函数不返回任何内容,他也不显示它是如何工作的。函数是change\u res(width,height),我认为应该在中调用它,而循环中的帧大小调整了 import cv2 as cv def rescale_frame(frame, scale): # works for image, video, live video width = int(fra

通过本教程,我正在学习opencv,我已经完成了第3部分(调整大小和缩放)。到最后,他创建了这个功能,应该更好地用于现场视频。函数不返回任何内容,他也不显示它是如何工作的。函数是
change\u res(width,height)
,我认为应该在
中调用它,而
循环中的
帧大小调整了

import cv2 as cv


def rescale_frame(frame, scale):    # works for image, video, live video
    width = int(frame.shape[1] * scale)
    height = int(frame.shape[0] * scale)
    dimensions = (width, height)
    return cv.resize(frame, dimensions, interpolation=cv.INTER_AREA)


def change_res(width, height):      # works only for live video
    capture.set(3, width)
    capture.set(4, height)


capture = cv.VideoCapture(0)    # integer to capture from webcam, path to capture video file

while True:
    isTrue, frame = capture.read()
    frame_resized = rescale_frame(frame, scale=.2)    # this line
    cv.imshow("Video", frame)
    cv.imshow("Video Resized", frame_resized)
    if cv.waitKey(20) & 0xFF == ord("q"):       # press "q" key to exit loop
        break


capture.release()
cv.destroyAllWindows()

感谢@Dan Mašek的评论,我终于理解了。
.set()
方法更改相机的分辨率,它不需要在
循环时处于
,因为
capture=cv.VideoCapture(0)
会一直按default运行,除非调用
capture.release()

import cv2 as cv


def rescale_frame(frame, scale):    # works for image, video, live video
    width = int(frame.shape[1] * scale)
    height = int(frame.shape[0] * scale)
    dimensions = (width, height)
    return cv.resize(frame, dimensions, interpolation=cv.INTER_AREA)


capture = cv.VideoCapture(0)  # integer to capture from webcam, path to capture video file
capture.set(3, 1440)

while True:
    isTrue, frame = capture.read()
    frame_resized = rescale_frame(frame, scale=.2)
    cv.imshow("video with set", frame)
    cv.imshow("Video Resized", frame_resized)
    if cv.waitKey(20) & 0xFF == ord("q"):       # press "q" key to exit loop
        break


capture.release()   # stop capturing the image/video
cv.destroyAllWindows()      # close windows
所以,我想要一个小窗口和一个大窗口,显示网络摄像头捕捉到的内容。目前,此代码捕获视频,使其成为2560x1440(使用
.set
),这是一个巨大的视频,然后它获取这些帧,并使用
重新缩放\u frame
功能重新缩放它们,使其变小。
我发现这个
.set()
方法对于单个视频更有效,但是如果我想要两个视频,我应该显示默认的小视频,然后使用
重新缩放\u frame
使其更大。

到底是什么阻碍了你将调用放到你认为谨慎的地方,看看它是否有效?在while循环之前只使用一次,看看capturingformat是否有变化?为什么在这里问?有什么问题吗?
capture.set(3,宽度)
--嗯,我想键入
CAP\u PROP\u FRAME\u width
太费力了。老实说,任何给你这样的神奇数字的示例代码的教学资源可能都是最好避免的。@PatrickArtner我用frame_resized=change_res(frame[0],frame[0])cv.imshow(“Video resized”,frame_resized)试过了,但不起作用。我甚至不知道该把什么放在括号里,而且在我看来,函数不返回任何东西似乎很荒谬。你在捕获设备上设置了一个选项,为什么它应该返回任何东西。在while循环之前尝试
change_res(700200)
——这不是逐帧执行的操作,而是拍摄设备的设置……您必须使用相机支持的帧大小。700x200不太可能是其中之一。