# USAGE
# python image_stitching.py --images images/scottsdale --output output.png --crop 1
# 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 '0', 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 image
if args["crop"] > 0:
# create a 10 pixel border surrounding the stitched image
print("[INFO] cropping...")
stitched = cv2.copyMakeBorder(stitched, 2, 2, 2, 2,
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 0 (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 image
cnts = 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 region
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)
# 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 region
minRect = mask.copy()
sub = mask.copy()
# keep looping until there are no non-zero pixels left in the
# subtracted image
while 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 left
minRect = cv2.erode(minRect, None)
sub = cv2.subtract(minRect, thresh)
# find contours in the minimum rectangular mask and then
# extract the bounding box (x, y)-coordinates
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)
# use the bounding box coordinates to extract the our final
# stitched image
stitched = stitched[y:y + h, x:x + w]
# write the output stitched image to disk
cv2.imwrite(args["output"], stitched)
# display the output stitched image to our screen
cv2.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+OpenCV对多张图片进行全景图像拼接源码+项目使用说明(课程设计大作业).zip 【图片全景拼接】消除鬼影,消除裂缝 对多张图片进行基于SIFT的特征检测算法,如果符合最小拼接要求大的关键点matchKeypoints数量,使用OpenCV-Python自带的stitching方法进行全景拼接 【使用方法】 python image_stitching.py --images images/scottsdale --output output.png --crop 1 其中images/scottsdale为待拼接图像所在文件夹,output.png为处理拼接保存后的图像;这里使用了相对路径,因为在工程根目录下运行了终端。不确定在根目录最好使用完整的绝对路径。 --crop 1为是否裁剪黑色边框,缺省则不裁剪。
资源推荐
资源详情
资源评论
收起资源包目录
基于Python+OpenCV对多张图片进行全景图像拼接源码+项目使用说明(课程设计大作业).zip (18个子文件)
image_stitching.py 4KB
.gitattributes 66B
output.png 1.03MB
image_stitching_simple.py 2KB
img
20190902002033.png 523KB
20190902002008.png 525KB
20190902001913.png 222KB
output2.png 410KB
output3.png 807KB
项目使用说明.md 1KB
images
newimages
16.png 639KB
26.png 676KB
scottsdale
IMG_1788-2.jpg 184KB
IMG_1787-2.jpg 152KB
IMG_1786-2.jpg 157KB
new
1_0.jpg 48KB
2_0.jpg 63KB
output1.png 1.22MB
共 18 条
- 1
Make程序设计
- 粉丝: 5725
- 资源: 3570
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页