我正在尝试对视频序列中的图像进行图像区分,但是我不知道如何输出结果的图像帧。
我尝试使用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()
答案 0 :(得分:0)
问题在那里:
# 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))