我尝试用tif图片进行拼接,但它不起作用,而且它不是来自代码:
import cv2
image_paths=['LFMstar_6.png','LFMstar_7.png']
# initialized a list of images
imgs = []
for i in range(len(image_paths)):
imgs.append(cv2.imread(image_paths[i]))
#imgs[i]=cv2.resize(imgs[i],(0,0),fx=0.4,fy=0.4)
# this is optional if your input images isn't too large
# you don't need to scale down the image
# in my case the input images are of dimensions 3000x1200
# and due to this the resultant image won't fit the screen
# scaling down the images
# showing the original pictures
cv2.imshow('1',imgs[0])
cv2.imshow('2',imgs[1])
stitchy=cv2.Stitcher.create()
(dummy,output)=stitchy.stitch(imgs)
if dummy != cv2.STITCHER_OK:
# checking if the stitching procedure is successful
# .stitch() function returns a true value if stitching is
# done successfully
print("stitching ain't successful")
else:
print('Your Panorama is ready!!!')
我尝试使用 jpg 图像,它工作得很好,但不适合我的...也许我的图片之间的重叠不起作用?我的 tif 图片目前为 140x140 像素。我将图片转换为 RGB 和 jpg 但它没有改变任何东西。这是我的图像示例。
谢谢你
回答你的问题:你的图像没有足够的重叠,或者没有包含足够的“关键点”,OpenCV 无法弄清楚它们如何组合在一起。
通过稍微修改您的代码,您可以(一点点)更多地了解该过程失败的原因。具体来说,您应该使用
stitch
函数提供的返回代码(在您使用的 代码中称为
dummy
。
检查返回值通常是一个很好的做法。 OpenCV 通常不会引发 Python 异常,因此这是了解何时出现问题的唯一方法。import cv2
err_dict = {
cv2.STITCHER_OK: "STITCHER_OK",
cv2.STITCHER_ERR_NEED_MORE_IMGS: "STITCHER_ERR_NEED_MORE_IMGS",
cv2.STITCHER_ERR_HOMOGRAPHY_EST_FAIL: "STITCHER_ERR_HOMOGRAPHY_EST_FAIL",
cv2.STITCHER_ERR_CAMERA_PARAMS_ADJUST_FAIL: "STITCHER_ERR_CAMERA_PARAMS_ADJUST_FAIL",
}
image_paths = ['LFMstar_6.jpg', 'LFMstar_7.jpg']
imgs = [cv2.imread(p) for p in image_paths]
stitchy = cv2.Stitcher.create()
(retval, output) = stitchy.stitch(imgs)
if retval != cv2.STITCHER_OK:
print(f"stitching falied with error code {retval} : {err_dict[retval]}")
exit(retval)
else:
print('Your Panorama is ready!!!')
success = cv2.imwrite("result.jpg", output)
if not success:
print("but could not be saved to `result.jpg`")
使用您提供的图像,错误代码为 1,为 STITCHER_ERR_NEED_MORE_IMGS
。由于某种原因,OpenCV 无法在图像之间找到足够明确的共同特征(“关键点”),以便将它们拼接在一起。这可能是由于图像尺寸小,或者相对线性的设计,或者平坦的颜色,如果不访问缝合器的内部图像处理管道,真的很难知道。