Skip to content

Instantly share code, notes, and snippets.

@prashdash112
Created October 30, 2020 06:15
Show Gist options
  • Save prashdash112/9978363edbea8d7549045553f8460f44 to your computer and use it in GitHub Desktop.
Save prashdash112/9978363edbea8d7549045553f8460f44 to your computer and use it in GitHub Desktop.
Image stitching for vehicles from a video file
import cv2
import os
from glob2 import glob
os.chdir('C:\\Users\\Prashant\\Desktop\\tensorflow notebooks\\task') # change the directory for folder creation
try:
if not os.path.exists('files'):
os.makedirs('files') #files folder created
except OSError:
print ('Error: Creating file directory')
vid=cv2.VideoCapture(r'C:\Users\Prashant\Downloads\stitch.avi') #captures the video file from the location
# frame
current_frame = 0
while(True):
# reading from frame
retval,frame = vid.read() #tuple unpacking
#cv2.waitKey(100)
if retval:
name = './files/frame{}'.format(current_frame) + '.jpg'
print ('Creating-->' + name)
# writing the extracted images
cv2.imwrite(name, frame)
# incrementing frames to get total number of frames
current_frame += 1
else:
break
# Release all space and windows once done
vid.release()
cv2.destroyAllWindows()
# grab the paths to the input images and initialize our images list
path='C:\\Users\\Prashant\\Desktop\\tensorflow notebooks\\task\\files'
print("loading images---->")
images = []
for im in glob(path+'\\frame*.jpg'):
image = cv2.imread(im) #converting the images to image array
images.append(image)
# initialize OpenCV's image sticher object and then perform the image
print("Stitching images...")
stitcher =cv2.Stitcher.create()
(status, stitched) = stitcher.stitch(images)
if __name__=='__main__':
if status==cv2.Stitcher_OK:
cv2.imshow(stitched)
cv2.waitKey()
cv2.destroyAllWindows()
else:
print('Error while performing stiching:',status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment