Создание мозаики со сшивкой с использованием нескольких изображений с воздуха - PullRequest
0 голосов
/ 06 июля 2019

Я пытаюсь создать мозаику, используя воздушные изображения улиц.Я написал на языке 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
...