图像配准,代码哪里出错了

from imutils import paths
import numpy as np
import imutils
import cv2

images = 'C:\learn\python\images\scottsdale'  # 输入图片文件夹路径
output = 'C:\learn\python\output.png'  # 输出图片名称
crop = True
imagePaths = sorted(list(paths.list_images(images)))
images = []

# 读取文件夹图片
for imagePath in imagePaths:
    image = cv2.imread(imagePath)
    images.append(image)

# 创建缝合器
stitcher = cv2.Stitcher_create()
(status, stitched) = stitcher.stitch(images)

# status:状态,0代表成功
if status == 0:
    # 裁剪图像
    if crop:
        # 在拼图周围添加2像素
        stitched = cv2.copyMakeBorder(stitched, 2, 2, 2, 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作为不断腐蚀的矩形
        # sub作为阈值图像和minRect的插值来进行判断
        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(output, stitched)

else:
    print("拼接失败,错误码:{}".format(status))

img

cv2.copyMakeBorder()函数的调用存在问题,第26行改成

stitched = cv2.copyMakeBorder(stitched, 2, 2, 2, cv2.BORDER_CONSTANT, value=(0, 0, 0))
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632
  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7706627
  • 这篇博客你也可以参考下:图像翻转代码实现
  • 除此之外, 这篇博客: 图像增强算法中的 代码 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • import cv2
    import numpy as np
    from matplotlib import pyplot as plt
    
    img = cv2.imread('gu.jpg',0)
    
    laplacian = cv2.Laplacian(img,cv2.CV_64F)
    sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
    sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
    
    sobelx = np.uint8(np.absolute(sobelx))
    sobely = np.uint8(np.absolute(sobely))
    sobelCom = cv2.bitwise_or(sobelx,sobely)
    
    C = img+laplacian
    E = cv2.blur(sobelCom,(5,5))
    F = C*E
    
    mina = np.min(F)
    maxa = np.max(F)
    #print(mina,maxa)
    F = np.uint8(255*(F-mina)/(maxa-mina))
    G = img+F
    H = cv2.pow(G/255.0,0.5)
    
    plt.subplot(2,4,1),plt.imshow(img,cmap='gray')
    plt.title('A = original'),plt.xticks([]),plt.yticks([])
    
    plt.subplot(2,4,2),plt.imshow(laplacian,cmap='gray')
    plt.title('B = laplacian'),plt.xticks([]),plt.yticks([])
    
    plt.subplot(2,4,3),plt.imshow(C,cmap='gray')
    plt.title('C = add a and b'),plt.xticks([]),plt.yticks([])
    
    plt.subplot(2,4,4),plt.imshow(sobelCom,cmap='gray')
    plt.title('D = sobel'),plt.xticks([]),plt.yticks([])
    
    plt.subplot(2,4,5),plt.imshow(E,cmap='gray')
    plt.title('E = blur sobel'),plt.xticks([]),plt.yticks([])
    
    plt.subplot(2,4,6),plt.imshow(F,cmap='gray')
    plt.title('F = C*E'),plt.xticks([]),plt.yticks([])
    
    plt.subplot(2,4,7),plt.imshow(G,cmap='gray')
    plt.title('G = add a and f'),plt.xticks([]),plt.yticks([])
    
    plt.subplot(2,4,8),plt.imshow(H,cmap='gray')
    plt.title('H = mi lv'),plt.xticks([]),plt.yticks([])
    
    plt.show()
    
  • 您还可以看一下 颜廷吉老师的软件架构师成长之路课程中的 动态代理模式小节, 巩固相关知识点