wk-labs/lab5/zad2.py
2021-11-29 20:19:05 +01:00

52 lines
1.5 KiB
Python

import cv2 as cv
import numpy as np
if __name__ == '__main__':
boat_1 = cv.imread("../img/boat_1.jpg", cv.IMREAD_COLOR)
boat_2 = cv.imread("../img/boat_2.jpg", cv.IMREAD_COLOR)
boat_1_g = cv.cvtColor(boat_1, cv.COLOR_BGR2GRAY)
boat_2_g = cv.cvtColor(boat_2, cv.COLOR_BGR2GRAY)
orb_alg = cv.ORB_create(nfeatures=1000)
key_points_1, descriptors_1 = orb_alg.detectAndCompute(boat_1_g, None)
key_points_2, descriptors_2 = orb_alg.detectAndCompute(boat_2_g, None)
matcher = cv.DescriptorMatcher_create(cv.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
matches = matcher.match(descriptors_1, descriptors_2, None)
matches = list(matches)
matches.sort(key=lambda x: x.distance, reverse=False) # sort by distance
num_good_matches = int(len(matches) * 0.05) # save 5%
matches = matches[:num_good_matches]
qIdx = matches[0].queryIdx
tIdx = matches[0].trainIdx
point_1 = key_points_1[qIdx].pt
point_2 = key_points_2[tIdx].pt
print(point_1)
print(point_2)
cv.circle(boat_1, np.int32(point_1), 10, [0, 255, 255], -1)
cv.circle(boat_2, np.int32(point_2), 10, [0, 255, 255], -1)
print(boat_1.shape)
print(boat_2.shape)
common_area_width = boat_1.shape[1] - int(point_1[0])
to_remove = common_area_width + int(point_2[0])
cropped_boat_2 = boat_2[:, to_remove:boat_2.shape[1], :]
panorama = np.concatenate((boat_1, cropped_boat_2), axis=1)
cv.imshow('panorama', panorama)
cv.waitKey(0)
cv.destroyAllWindows()