使用OpenCV自带的Stitcher类来完成两张图片的拼接很简单,只要直接调用就可以,下面给大家展示一段代码。
import time
import cv2
import os
import sys
def main():
# read input images
imgs = []
path = 'E:/picture/room/'
i = 0
for (root, dirs, files) in os.walk(path):
images = [f for f in files]
print(images)
for i in range(0, len(images)):
curImg = cv2.imread(path + images[i])
imgs.append(curImg)
# 这一行需要注意
stitcher = cv2.createStitcher(False)
status, result_image = stitcher.stitch(imgs)
if status != cv2.Stitcher_OK:
print("Can't stitch images, error code = %d" % status)
sys.exit(-1)
cv2.imwrite("E:/picture/room/output.jpg", result_image)
cv2.waitKey(10)
if __name__ == '__main__':
start = time.time()
main()
end = time.time()
cv2.destroyAllWindows()
注意:
在OpenCV 3.x版本中是cv2.createStitcher(),而在OpenCV 4.x中是cv2.Stitcher_create()函数,这需要注意。
输入图片:
输出结果图片:
完!