使用航拍多个图像创建带拼接的马赛克

时间:2019-07-06 08:14:58

标签: opencv image-stitching opencv-stitching mosaic-plot

我正在尝试使用空中街道图像创建马赛克。 我用python语言编写了一种拼接算法,但是在此实现中,从右到左拼接了两个图像,我该如何在所有空间方向上做到这一点?

我想重新创建类似的结果:https://johnscottrailton.files.wordpress.com/2015/08/4817954535_d3476dc1e1_o.jpg

def compute_sift(img1, img2):

MIN_MATCH_COUNT = 4

gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
denoise1 = cv2.GaussianBlur(gray1, (5, 5), 0)
denoise2 = cv2.GaussianBlur(gray2, (5, 5), 0)


# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(denoise1, None)
kp2, des2 = sift.detectAndCompute(denoise2, None)

# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)

# Apply ratio test and save the keypoints index
good = []
i = 0
index_keypoints = []
for m, n in matches:
    if m.distance < 0.3 * n.distance:
        good.append(m)
        index_keypoints.append(i)
    i = i+1

# cv.drawMatchesKnn expects list of lists as matches.
draw_params = dict(matchColor=(0, 255, 0),  # draw matches in green color
                   singlePointColor=None,
                   flags=2)

img3 = cv2.drawMatches(img1, kp1, img2, kp2, good, None, **draw_params)
plt.imshow(img3), plt.show()

if len(good) >= MIN_MATCH_COUNT:
    src = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1,1,2)
    dst = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1,1,2)

    H, masked = cv2.findHomography(src, dst, cv2.RANSAC, 5.0)
    # find_transform = transform.SimilarityTransform()
    # H = find_transform.estimate(src, dst)
    # print(type(H))

    h, w = gray1.shape
    pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
    dst = cv2.perspectiveTransform(pts, H)
    #plt.imshow(dst), plt.show()

    gray2 = cv2.polylines(gray2, [np.int32(dst)], True, 255, 3, cv2.LINE_AA)
    #plt.imshow(gray2), plt.show()
else:
    print("Not enough matches are found - %d/%d" % (len(good), MIN_MATCH_COUNT))

dst = cv2.warpPerspective(img1, H, (img2.shape[1] + img1.shape[1], img2.shape[0]))
#plt.imshow(dst), plt.show()
dst[0:img2.shape[0], 0:img2.shape[1]] = img2
plt.imshow(dst), plt.show()
#cv2.imwrite("dst.jpg", dst)
return dst

0 个答案:

没有答案