简而言之,我的问题是如何通过为添加的图像指定特定的坐标,将图像放在另一个图像的上面?我需要根据需要扩展基本图像的“画布”,这样添加的图像就不会被裁剪。
以下是扩展版本:
我的项目是从无人驾驶飞机的视频中提取图片,并用它们绘制一张粗略的地图,将一张照片与最后一张照片对齐。我知道有一些软件我可以使用,比如Agisoft Photoscan,但我的目标是创建一个更轻量级、更粗糙的解决方案。
这是我的计划,我打算用每一帧来做:
estimateRigidTransform
生成转换矩阵,以使curr_photo
与最后一张照片base
对齐curr_image
添加到base
中,将当前图像与最后一个图像(确保这两个图像不发生剪切)一起添加到当前图像。这就是我要问的问题。下面是执行步骤1到步骤4的代码。
import numpy as np
import cv2
base = cv2.imread("images/frame_03563.jpg")
curr_photo = cv2.imread("images/frame_03564.jpg")
height, width = curr_photo.shape[:2]
# Step 1
# which transformation is required to go from curr_photo to base?
transformation = cv2.estimateRigidTransform(curr_photo, base, True)
# Step 2
# add a line to the affine transformation matrix so it can be used by
# perspectiveTransform
three_by_three = np.array([
transformation[0],
transformation[1],
[0, 0, 1]], dtype="float32")
# get corners of curr_photo (to be transformed)
corners = np.array([
[0, 0],
[width - 1, 0],
[width - 1, height - 1],
[0, height - 1]
])
# where do the corners of the image go
trans_corners = cv2.perspectiveTransform(np.float32([corners]), three_by_three)
# get the bounding rectangle for the four corner points (and thus, the transformed image)
bx, by, bwidth, bheight = cv2.boundingRect(trans_corners)
# Step 3
# modify transformation matrix so that the top left of the bounding box is at the origin
transformation[0][2] = transformation[0][2] - bx
transformation[1][2] = transformation[1][2] - by
# Step 4
# transform the image in a window the size of its bounding rectangle (so no cropping)
mod_curr_photo = cv2.warpAffine(curr_photo, transformation, (bwidth, bheight))
# for viewing
cv2.imshow("base", base)
cv2.imshow("current photo", curr_photo)
cv2.imshow("image2 transformed to image 1", mod_curr_photo)
cv2.waitKey()
我还附上了两个样本图像。我用第一个作为基础,但不管用哪种方式都行。
发布于 2017-07-12 00:04:21
https://stackoverflow.com/questions/45050190
复制相似问题