OpenCV-Python图像拼接方法
生活随笔
收集整理的這篇文章主要介紹了
OpenCV-Python图像拼接方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
什么是圖像拼接?
圖1:
圖2:
image_stitching_simple.py
# import the necessary packages from imutils import paths import numpy as np import argparse import imutils import cv2# construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--images", type=str, required=True,help="path to input directory of images to stitch") ap.add_argument("-o", "--output", type=str, required=True,help="path to the output image") args = vars(ap.parse_args())# grab the paths to the input images and initialize our images list print("[INFO] loading images...") imagePaths = sorted(list(paths.list_images(args["images"]))) images = []# loop over the image paths, load each one, and add them to our images to stitch list for imagePath in imagePaths:image = cv2.imread(imagePath)images.append(image)# initialize OpenCV's image stitcher object and then perform the image stitching print("[INFO] stitching images...") stitcher = cv2.createStitcher() if imutils.is_cv3() else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images)# if the status is '3', then OpenCV successfully performed image stitching if status == 0:# write the output stitched image to diskcv2.imwrite(args["output"], stitched)# display the output stitched image to our screencv2.imshow("Stitched", stitched)cv2.waitKey(0)# otherwise the stitching failed, likely due to not enough keypoints) being detected else:print("[INFO] image stitching failed ({})".format(status))# python image_stitching_simple.py --images ./images/4/ --output ./images/4/output.png使用方法:python image_stitching_simple.py --images ./images/1/ --output ./images/1/output.png
拼接結果:
image_stitching.py
# import the necessary packages from imutils import paths import numpy as np import argparse import imutils import cv2# construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--images", type=str, required=True,help="path to input directory of images 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") args = vars(ap.parse_args())# grab the paths to the input images and initialize our images list print("[INFO] loading images...") imagePaths = sorted(list(paths.list_images(args["images"]))) images = []# loop over the image paths, load each one, and add them to our images to stich list for imagePath in imagePaths:image = cv2.imread(imagePath)images.append(image)# initialize OpenCV's image sticher object and then perform the image stitching print("[INFO] stitching images...") stitcher = cv2.createStitcher() if imutils.is_cv3() else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images)# if the status is '3', then OpenCV successfully performed image stitching if status == 0:# check to see if we supposed to crop out the largest rectangular region from the stitched imageif args["crop"] > 0:# create a 10 pixel border surrounding the stitched imageprint("[INFO] cropping...")stitched = cv2.copyMakeBorder(stitched, 10, 10, 10, 10,cv2.BORDER_CONSTANT, (0, 0, 0))# convert the stitched image to grayscale and threshold it# such that all pixels greater than zero are set to 255# (foreground) while all others remain 3 (background)gray = cv2.cvtColor(stitched, cv2.COLOR_BGR2GRAY)thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]# find all external contours in the threshold image then find# the *largest* contour which will be the contour/outline of# the stitched imagecnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)cnts = imutils.grab_contours(cnts)c = max(cnts, key=cv2.contourArea)# allocate memory for the mask which will contain the# rectangular bounding box of the stitched image regionmask = np.zeros(thresh.shape, dtype="uint8")(x, y, w, h) = cv2.boundingRect(c)cv2.rectangle(mask, (x, y), (x + w, y + h), 255, -1)# create two copies of the mask: one to serve as our actual# minimum rectangular region and another to serve as a counter# for how many pixels need to be removed to form the minimum# rectangular regionminRect = mask.copy()sub = mask.copy()# keep looping until there are no non-zero pixels left in the# subtracted imagewhile cv2.countNonZero(sub) > 0:# erode the minimum rectangular mask and then subtract# the thresholded image from the minimum rectangular mask# so we can count if there are any non-zero pixels leftminRect = cv2.erode(minRect, None)sub = cv2.subtract(minRect, thresh)# find contours in the minimum rectangular mask and then# extract the bounding box (x, y)-coordinatescnts = 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)# use the bounding box coordinates to extract the our final# stitched imagestitched = stitched[y:y + h, x:x + w]# write the output stitched image to diskcv2.imwrite(args["output"], stitched)# display the output stitched image to our screencv2.imshow("Stitched", stitched)cv2.waitKey(0)# otherwise the stitching failed, likely due to not enough keypoints) # being detected else:print("[INFO] image stitching failed ({})".format(status))# python image_stitching.py --images ./images/1/ --output ./images/1/output_crop.png --crop 1使用方法:python image_stitching.py --images ./images/1/ --output ./images/1/output_crop.png --crop 1
拼接結果:
總結
以上是生活随笔為你收集整理的OpenCV-Python图像拼接方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pat 1058 A+B in Hogw
- 下一篇: 腾讯iphone面试题(转)