简体   繁体   中英

How to output a sequence of images in openCV python

I am trying to perform image differencing on images from a video sequence, but I don't know how can output the resulting image frames.

I have tried to use cv2.imwrite but I get the following error

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()

The issue is there:

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

Error message says the file extension is missing, indeed filename you're using has no extension specified, try to modify/add it and check the result.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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