python opencv图像拼接_Python+opencv 图像拼接

2.由于不是Python的,所以简单做了一些翻译转成Python+opencv的实现

3.修改了原来的特征点检测算法为ORB(由于sift和surf的专利问题)

4.结果

927261-20181121140050633-560080557.png

5.源码

import numpy as np

import cv2 as cv

from matplotlib import pyplot as plt

import matplotlib.gridspec as gridspec

GOOD_POINTS_LIMITED = 0.99

src = 'photoes\\homograph\\w1.jpg'

des = 'photoes\\homograph\\w2.jpg'

img1_3 = cv.imread(src,1)# 基准图像

img2_3 = cv.imread(des,1)# 拼接图像

orb = cv.ORB_create()

kp1, des1 = orb.detectAndCompute(img1_3,None)

kp2, des2 = orb.detectAndCompute(img2_3,None)

bf = cv.BFMatcher.create()

matches = bf.match(des1,des2)

matches = sorted(matches, key = lambda x:x.distance)

goodPoints =[]

for i in range(len(matches)-1):

if matches[i].distance < GOOD_POINTS_LIMITED * matches[i+1].distance:

goodPoints.append(matches[i])

# goodPoints = matches[:20] if len(matches) > 20 else matches[:]

print(goodPoints)

img3 = cv.drawMatches(img1_3,kp1,img2_3,kp2,goodPoints, flags=2,outImg=None )

src_pts = np.float32([kp1[m.queryIdx].pt for m in goodPoints]).reshape(-1, 1, 2)

dst_pts = np.float32([kp2[m.trainIdx].pt for m in goodPoints]).reshape(-1, 1, 2)

M, mask = cv.findHomography( dst_pts,src_pts, cv.RHO)

# 获取原图像的高和宽

h1,w1,p1 = img2_3.shape

h2,w2,p2 = img1_3.shape

h = np.maximum(h1,h2)

w = np.maximum(w1,w2)

_movedis = int(np.maximum(dst_pts[0][0][0],src_pts[0][0][0]))

imageTransform = cv.warpPerspective(img2_3,M,(w1+w2-_movedis,h))

M1 = np.float32([[1, 0, 0], [0, 1, 0]])

h_1,w_1,p = img1_3.shape

dst1 = cv.warpAffine(img1_3,M1,(w1+w2-_movedis, h))

dst = cv.add(dst1,imageTransform)

dst_no = np.copy(dst)

dst_target = np.maximum(dst1,imageTransform)

fig = plt.figure (tight_layout=True, figsize=(8, 18))

gs = gridspec.GridSpec (6, 2)

ax = fig.add_subplot (gs[0, 0])

ax.imshow(img1_3)

ax = fig.add_subplot (gs[0, 1])

ax.imshow(img2_3)

ax = fig.add_subplot (gs[1, :])

ax.imshow(img3)

ax = fig.add_subplot (gs[2, :])

ax.imshow(imageTransform)

ax = fig.add_subplot (gs[3, :])

ax.imshow(dst1)

ax = fig.add_subplot (gs[4, :])

ax.imshow(dst_no)

ax = fig.add_subplot (gs[5, :])

ax.imshow(dst_target)

ax.set_xlabel ('The smooth method is SO FAST !!!!')

plt.show()

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值