在本教程中,我正在学习https://www.youtube.com/watch?v=oXlwWbU8l2o&ab_channel=freeCodeCamp.org的opencv,并且我已经学到了第3部分(调整大小和重新缩放)。在接近尾声的时候,他创建了这个函数,这对于实时视频来说应该更好。这个函数没有返回任何东西,他也没有展示它是如何工作的。这个函数是change_res(width, height),我认为应该在frame_resized的while循环中调用它。
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()发布于 2021-05-28 15:21:37
多亏了@Dan Ma while ek的评论,我终于理解了。.set()方法改变了相机的分辨率,它不需要在while循环中,因为除非调用capture.release(),否则capture = cv.VideoCapture(0)永远都是默认的。
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),然后获取这些帧并使用rescale_frame函数对其进行重新缩放以使其更小。我发现这种.set()方法对于单个视频更有效,但如果我想要两个视频,我应该显示为默认的小视频,然后使用rescale_frame将其放大。
https://stackoverflow.com/questions/67721086
复制相似问题