Home > Net >  crop frames of a video using opencv
crop frames of a video using opencv

Time:12-02

i want to crop each of the frames of this video and save all the cropped images to use them as input for a focus stacking software but my approach:

cap = cv2.VideoCapture(r"C:\Users\HP\Downloads\VID_20221128_112556.mp4")
ret, frames = cap.read()
count=0
for img in frames:
    stops.append(img)
    cv2.imwrite("../stack/croppedframe%d.jpg" % count,img[500:1300,100:1000])
    print(count)
    count  = 1

Throws this error:

error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:801: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'

what can i do?

CodePudding user response:

If you take the frames variable with for loop it will give you the image on the y-axis.if you use while loop the code will work. You can try the example below.

cap = cv2.VideoCapture(r"C:\Users\HP\Downloads\VID_20221128_112556.mp4")
ret, frames = cap.read()
count=0
while(ret):
    stops.append(frames)
    cv2.imwrite("../stack/croppedframe%d.jpg" % count,frames[500:1300,100:1000])
    print(count)
    count  = 1
    ret, frames = cap.read()
  • Related