根据循环值更改 VideoCapture 大小

Change VideoCapture size based on loop value

所以我在下面有这段代码,我期望它会根据循环 i 值实时更改框架的宽度大小,从 1 到 1000,以便在我执行时将其可视化我期望它在 运行.

时更改 windows 宽度大小的代码
import cv2
import numpy as np

vid = cv2.VideoCapture("C:\users\USER\downloads\man.mp4")
while True:
    ret,frame = vid.read()
    for i in range(1,1000):
        frame = cv2.resize(frame,(i,450))  
        size = 16
        # Create motion blur kernel
        kernel_motion_blur = np.zeros((size,size))
        kernel_motion_blur[int((size-1)/2), :] = np.ones(size)    
        kernel_motion_blur = kernel_motion_blur / size
        # Apply motion kernel motion blur
        result = cv2.filter2D(frame, -1, kernel_motion_blur)
        cv2.imshow('Motion Blur Applied',result)
        cv2.imshow('Original',frame)
        if cv2.waitKey(1) == ord('q'):
            break

cv2.destroyAllWindows()
vid.release()

它的工作原理,但问题是当我执行代码时,视频帧出现故障并且视频帧没有显示实际视频中包含的正确图像。那我该如何解决呢?

前后视频

我不确定我是否完全理解这个问题,或者为什么你在 while 循环中有一个 for 循环,但我认为我能够达到你想要的效果。看一看:

import cv2
import numpy as np

# control how fast the image slides left to right
step_size = 10

vid = cv2.VideoCapture("videos\demo.mp4")
while True:
    ret,frame = vid.read()
    if ret == False:
        break
    width = frame.shape[1]
    for n in range(0, width, step_size):
        frame_to_show = frame[:,:n+step_size]
        size = 16

        # Create motion blur kernel
        kernel_motion_blur = np.zeros((size,size))
        kernel_motion_blur[int((size-1)/2), :] = np.ones(size)    
        kernel_motion_blur = kernel_motion_blur / size
        # Apply motion kernel motion blur
        result = cv2.filter2D(frame_to_show, -1, kernel_motion_blur)
        cv2.imshow('Motion Blur Applied',result)
        cv2.imshow('Original',frame_to_show)
        key = cv2.waitKey(1)
        if key == ord('q'):
            break

    cv2.destroyAllWindows()
    vid.release()

或者这可能是您要找的更多内容:

import cv2
import numpy as np

# control how fast the image slides left to right
step_size = 10

vid = cv2.VideoCapture("videos\demo.mp4")
end = 0
while True:
    ret,frame = vid.read()
    if ret == False:
        break

    width = frame.shape[1]
    if end > width:
        end = 0
    end += step_size

    frame_to_show = frame[:,:end]
    size = 16

    # Create motion blur kernel
    kernel_motion_blur = np.zeros((size,size))
    kernel_motion_blur[int((size-1)/2), :] = np.ones(size)    
    kernel_motion_blur = kernel_motion_blur / size
    # Apply motion kernel motion blur
    result = cv2.filter2D(frame_to_show, -1, kernel_motion_blur)
    cv2.imshow('Motion Blur Applied',result)
    cv2.imshow('Original',frame_to_show)
    key = cv2.waitKey(1)
    if key == ord('q'):
        break

cv2.destroyAllWindows()
vid.release()