''' https://www.pyimagesearch.com/2018/12/17/image-stitching-with-opencv-and-python/ ''' from imutils import paths import numpy as np import argparse import imutils import cv2
ap = argparse.ArgumentParser() ap.add_argument("-i", "--input", type=str, required=True, help="path to input directory of input to stitch") ap.add_argument("-o", "--output", type=str, required=True, help="path to the output image") ap.add_argument("-c", "--crop", type=int, default=0, help="whether to crop out largest rectangular region") ap.add_argument("-t", "--type", type=str, default="d", help="type of the input, 'd' is directory, 'v' is video") args = vars(ap.parse_args())
images = []
if args["type"] == "d": print("[INFO] loading images...") imagePaths = sorted(list(paths.list_images(args["input"]))) for imagePath in imagePaths: image = cv2.imread(imagePath) images.append(image) print("[INFO] stitching images...") elif args["type"] =="v": cap=cv2.VideoCapture(args["input"]) Num = 0 while Num < cap.get(cv2.CAP_PROP_FRAME_COUNT): Num += 1 if Num % 1 == 0: ret,frame=cap.read() images.append(frame)
stitcher = cv2.createStitcher() if imutils.is_cv3() else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images)
if status == 0: if args["crop"] > 0: print("[INFO] cropping...") stitched = cv2.copyMakeBorder(stitched, 10, 10, 10, 10, cv2.BORDER_CONSTANT, (0, 0, 0)) gray = cv2.cvtColor(stitched, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) c = max(cnts, key=cv2.contourArea) mask = np.zeros(thresh.shape, dtype="uint8") (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(mask, (x, y), (x + w, y + h), 255, -1) minRect = mask.copy() sub = mask.copy() while cv2.countNonZero(sub) > 0: minRect = cv2.erode(minRect, None) sub = cv2.subtract(minRect, thresh) cnts = cv2.findContours(minRect.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) c = max(cnts, key=cv2.contourArea) (x, y, w, h) = cv2.boundingRect(c) stitched = stitched[y:y + h, x:x + w] cv2.imwrite(args["output"], stitched) cv2.imshow("Stitched", stitched) cv2.waitKey(0)
else: print("[INFO] image stitching failed ({})".format(status))
|