Как вывести последовательность изображений в openCV python - PullRequest
0 голосов
/ 16 июня 2019

Я пытаюсь выполнить различие изображения на изображениях из видеопоследовательности, но я не знаю, как можно вывести получившиеся кадры изображения.

Я пытался использовать 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()

1 Ответ

0 голосов
/ 16 июня 2019

Проблема есть:

# 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))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...