python三张照片画面拼接

#将左图命名为1.jpg,中图命名为2.jpg,右图命名为3.jpg,存入py文件同目录下
#运行程序即可出现拼接图
#按下esc程序退出
import cv2
import numpy as np
import imutils

def cvshow(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def sift_kp(image):
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    sift = cv2.xfeatures2d.SIFT_create()
    kp, des = sift.detectAndCompute(image, None)
    kp_image = cv2.drawKeypoints(gray_image, kp, None)
    return kp_image, kp, des


def get_good_match(des1, des2):
    bf = cv2.BFMatcher()
    matches = bf.knnMatch(des1, des2, k=2)  # des1为模板图,des2为匹配图
    matches = sorted(matches, key=lambda x: x[0].distance / x[1].distance)
    good = []
    for m, n in matches:
        if m.distance < 0.75 * n.distance:
            good.append(m)
    return good

def drawMatches(imageA, imageB, kpsA, kpsB, matches, status):
    # 初始化可视化图片,将A、B图左右连接到一起
    (hA, wA) = imageA.shape[:2]
    (hB, wB) = imageB.shape[:2]
    vis = np.zeros((max(hA, hB), wA + wB, 3), dtype="uint8")
    vis[0:hA, 0:wA] = imageA
    vis[0:hB, wA:] = imageB

    # 联合遍历,画出匹配对
    for ((trainIdx, queryIdx), s) in zip(matches, status):
        # 当点对匹配成功时,画到可视化图上
        if s == 1:
            # 画出匹配对
            ptA = (int(kpsA[queryIdx][0]), int(kpsA[queryIdx][1]))
            ptB = (int(kpsB[trainIdx][0]) + wA, int(kpsB[trainIdx][1]))
            cv2.line(vis, ptA, ptB, (0, 255, 0), 1)

    # 返回可视化结果
    return vis


# 全景拼接
def siftimg_rightlignment(img_right, img_left):
    _, kp1, des1 = sift_kp(img_right)
    _, kp2, des2 = sift_kp(img_left)
    goodMatch = get_good_match(des1, des2)
    # 当筛选项的匹配对大于4对时:计算视角变换矩阵
    if len(goodMatch) > 4:
        # 获取匹配对的点坐标
        ptsA = np.float32([kp1[m.queryIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
        ptsB = np.float32([kp2[m.trainIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
        ransacReprojThreshold = 4
        H, status = cv2.findHomography(ptsA, ptsB, cv2.RANSAC, ransacReprojThreshold)
        #  该函数的作用就是先用RANSAC选择最优的四组配对点,再计算H矩阵。H为3*3矩阵

        # 将图片右进行视角变换,result是变换后图片
        result = cv2.warpPerspective(img_right, H, (img_right.shape[1] + img_left.shape[1], img_right.shape[0]))
        #cvshow('result_medium', result)
        # 将图片左传入result图片最左端
        result[0:img_left.shape[0], 0:img_left.shape[1]] = img_left
        return result
      
# 特征匹配+全景拼接
import numpy as np
import cv2

# 读取拼接图片(注意图片左右的放置)
# 是对右边的图形做变换
img_right = cv2.imread('3.jpg')#有图
img_left = cv2.imread('2.jpg')#中图
img=cv2.imread('1.jpg')#左图
img=cv2.resize(img,(640,640))
img=imutils.rotate(img, 180)


img_right = cv2.resize(img_right,(640,480))
# 保证两张图一样大
img_left = cv2.resize(img_left,(640,480))


kpimg_right, kp1, des1 = sift_kp(img_right)
kpimg_left, kp2, des2 = sift_kp(img_left)

# 同时显示原图和关键点检测后的图
#cvshow('img_left',np.hstack((img_left,kpimg_left)))
#cvshow('img_right',np.hstack((img_right,kpimg_right)))
goodMatch = get_good_match(des1, des2)

all_goodmatch_img= cv2.drawMatches(img_right, kp1, img_left, kp2, goodMatch, None, flags=2)

# goodmatch_img自己设置前多少个goodMatch[:10]
goodmatch_img = cv2.drawMatches(img_right, kp1, img_left, kp2, goodMatch[:10], None, flags=2)


#cvshow('Keypoint Matches1', all_goodmatch_img)
#cvshow('Keypoint Matches2', goodmatch_img)


# 把图片拼接成全景图
result=siftimg_rightlignment(img_right,img_left)#两摄拼接图
#cvshow('result',result)
result=cv2.resize(result,(1920,640))
result= imutils.rotate(result, 180)



#cvshow('result',result)
kpimg_left, kp2, des2 = sift_kp(result)
kpimg_right, kp1, des1 = sift_kp(img)

# 同时显示原图和关键点检测后的图
#cvshow('img_left',np.hstack((img_left,kpimg_left)))
#cvshow('img_right',np.hstack((img_right,kpimg_right)))
goodMatch = get_good_match(des1, des2)

all_goodmatch_img= cv2.drawMatches(img, kp1, result, kp2, goodMatch, None, flags=2)

# goodmatch_img自己设置前多少个goodMatch[:10]
goodmatch_img = cv2.drawMatches(img, kp1, result, kp2, goodMatch[:10], None, flags=2)


#cvshow('Keypoint Matches1', all_goodmatch_img)
#cvshow('Keypoint Matches2', goodmatch_img)


# 把图片拼接成全景图
result_out=siftimg_rightlignment(img,result)
result_out =imutils.rotate(result_out, 180)
cvshow('result_out',result_out)#三摄拼接图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_cv_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值