【问题标题】:How to output a sequence of images in openCV python如何在openCV python中输出一系列图像
【发布时间】:2019-06-16 16:41:21
【问题描述】:

我正在尝试对来自视频序列的图像执行图像差分,但我不知道如何输出结果图像帧。

我曾尝试使用 cv2.imwrite 但出现以下错误

OpenCV(3.4.1) C:\Miniconda3\conda-bld\opencv-suite_1533128839831\work\modules\imgcodecs\src\loadsave.cpp:678: error: (-2) could not find a writer for the specified extension in function cv::imwrite_
import cv2

# Compute the frame differences
def frame_diff(prev_frame, cur_frame, next_frame):
# Difference between the current frame and the next frame
diff_frames_1 = cv2.absdiff(next_frame, cur_frame)

# Difference between the current frame and the previous frame
diff_frames_2 = cv2.absdiff(cur_frame, prev_frame)

return cv2.bitwise_and(diff_frames_1, diff_frames_2)

# Define a function to get the current frame from the video
def get_frame(cap, scaling_factor):
# Read the current frame from the video capture object
    ret, frame = cap.read()

# Resize the image
    frame = cv2.resize(frame, None, fx=scaling_factor, 
            fy=scaling_factor, interpolation=cv2.INTER_AREA)

# Convert to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

return gray

# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
if __name__=='__main__':
# Define the video capture object
    cap = cv2.VideoCapture('clip.mp4')

# Check if camera opened successfully
    if (cap.isOpened()== False): 
    print("Error opening video stream or file")

# Read until video is completed
    while(cap.isOpened()):
    # Capture frame-by-frame
        ret, frame = cap.read()
        if ret == True:

    # Define the scaling factor for the images
            scaling_factor = 0.5

    # Grab the current frame
        prev_frame = get_frame(cap, scaling_factor) 

    # Grab the next frame
        cur_frame = get_frame(cap, scaling_factor) 

    # Grab the frame after that
        next_frame = get_frame(cap, scaling_factor)

    # Write the frame difference
        cv2.imwrite('Object Movement', frame_diff(prev_frame, 
            #cur_frame, next_frame))

    # Update the variables
        prev_frame = cur_frame
        cur_frame = next_frame 

    # Grab the next frame
        next_frame = get_frame(cap, scaling_factor)

    # Press Q on keyboard to  exit
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break

  # Break the loop
        else: 
            break


# When everything done, release the video capture object
cap.release()

# Closes all the frames
cv2.destroyAllWindows()

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    问题出在:

    # Write the frame difference
    cv2.imwrite('Object Movement', frame_diff(prev_frame, cur_frame, next_frame))
    

    错误消息说文件扩展名丢失,确实您使用的文件名没有指定扩展名,尝试修改/添加并检查结果。

    # Write the frame difference
    cv2.imwrite('Object_Movement.png', frame_diff(prev_frame, cur_frame, next_frame))
    

    【讨论】:

    • 非常感谢。有效。另一个问题是,是否可以为视频序列中的每两个连续图像编写一个差异图像序列,以便可以研究每个结果差异图像?
    • 抱歉,我不确定我是否完全理解了您的新问题……您对“序列”一词的理解是什么?你想把两张连续的图片粘在一起吗?
    • 我的意思是,不是获得一个输出图像,而是可以获得一系列图像。例如,第 1 帧和第 2 帧、第 2 帧和第 3 帧之间的差异等等。抱歉回复晚了。
    • 但是您已经这样做了:只需在每个循环中更改输出名称即可获得所需的所有图像
    • 感谢您的热情回复。
    猜你喜欢
    • 2015-01-16
    • 2014-01-29
    • 2018-06-20
    • 2021-06-14
    • 2018-03-14
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多