Сшивание изображений не работает должным образом. Деформированное изображение обрезается, и интерполяция невозможна, потому что изображения не пересекаются.
Привет, мне дали домашнее задание, в котором я должен сшить два изображения, снятых разными камерами. Я должен найти матрицу гомографии, а затем деформировать второе изображение, используя эту матрицу. В конце я должен интерполировать два изображения.
К сожалению, написанный мной код работает некорректно. Во время второго деформации изображения я потерял большую часть информации об изображении; многие пиксели являются черными, и не все преобразованное изображение преобразуется.
Я отслеживаю два изображения по четыре пикселя каждое в том же порядке. Ниже вы можете найти фрагмент кода, который я написал.
# Globals
points = []
def show_and_fetch(image, title):
cv2.namedWindow(title, cv2.WINDOW_NORMAL)
cv2.setMouseCallback(title, mouse_callback)
# Show the image
cv2.imshow(title, image)
# Wait for user input to continue
cv2.waitKey(0)
cv2.destroyAllWindows()
# mouse callback function
def mouse_callback(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
points.append([x, y])
def stitching():
"""
This procedure stiches two images
:return:
"""
print "Stitching starts..."
###########################################################################
# Get input information
in_file_1 = utils.get_input(
"Insert 0 to exit, the path to the first image to stitch "
"or empty input to use default image: ", "string",
constants.default_stitching1)
in_file_2 = utils.get_input(
"Insert 0 to exit, the path to the second image to stitch "
"or empty input to use default image: ", "string",
constants.default_stitching2)
image_1 = utils.read_image(in_file_1)
image_2 = utils.read_image(in_file_2)
global points
show_and_fetch(image_1, "Image 1 to Stitch")
image_1_points = np.asarray(points, dtype=np.float32)
points = []
show_and_fetch(image_2, "Image 2 to Stitch")
image_2_points = np.asarray(points, dtype=np.float32)
matrix, mask = cv2.findHomography(image_1_points, image_2_points, cv2.RANSAC, 5)
image_1_warped = cv2.warpPerspective(image_1, matrix, dsize=image_1.shape[0:2])
utils.show_image_and_wait(image_1_warped, 'Image 1 warped', wait=False)
utils.show_image_and_wait(image_1, 'Image 1', wait=False)
utils.show_image_and_wait(image_2, 'Image 2')
if __name__ == "__main__":
stitching()
Я ожидаю, что искаженное изображение будет преобразовано с сохранением большей части информации в пикселях. Затем интерполяция должна применять пересечение двух изображений, которые перекрываются в определенной области.
Например, я хочу вставить эти два изображения: