如何使用目标图像,地标坐标,最小二乘接近和旋转矩阵旋转2D图像?

发布于 2025-01-20 17:08:40 字数 1045 浏览 3 评论 0原文

我有两个2D图像,一个是源图像,另一个是目标图像。我需要旋转源图像以使用Python(Scikit& numpy)匹配目标图像。我对每个图像有3个地标坐标,如下所示:

image1_points = [(12,16),(7,4),(25,20)]
image2_points = [(15,22),(1,22),(25,10)]

我相信以下步骤是所需的:

  • 使用3个地标坐标使用最小二乘方法创建旋转矩阵,
  • 使用旋转矩阵将theta
  • 将Theta转换为程度
  • 转换为度(为角度)使用Theta apply_angle方法具有角度,以旋转

我一直在尝试使用这些点的图像和最小二乘方法来计算线性变换矩阵,该线性转换矩阵将点从源转换为目标图像。

我知道我需要创建一个旋转矩阵,但是从未采用代数,我有点迷路。我已经完成了很多阅读,并尝试使用Scipy的内置procrustes进行下面的仿射转换(这可能都是错误的)。

m1, m2, d = scipy.spatial.procrustes(target_points, source_points)
a = np.dot(m1.T, m2, out=None) / norm(m1)**2

#separate x and y for the sake of convenience
ref_x = m2[::2]
ref_y = m2[1::2]

x = m1[::2]
y = m1[1::2]

b = np.sum(x*ref_y - ref_x*y) / norm(m1)**2

scale = np.sqrt(a**2+b**2)
theta = atan(b / max(a.all(), 10**-10)) #avoid dividing by 0

degrees = cos(radians(theta))

apply_angle(source_img, degrees)

但是,这并没有给我我期望的结果。它给了我一个学位,大约在1个学位,我期望在72左右的学位。我怀疑该学位是将图像旋转为角度参数所需的程度。

任何帮助将不胜感激。谢谢你!

I have two 2d images, one is the source image and the other is a target image; I need to rotate the source image to match the target image using python (scikit & numpy). I have 3 landmark coordinates for each image, as follows:

image1_points = [(12,16),(7,4),(25,20)]
image2_points = [(15,22),(1,22),(25,10)]

I believe the following steps are what's needed:

  • Create rotation matrix using least squares approach using the 3 landmark coordinates
  • Use the rotation matrix to get theta
  • Convert theta to degrees (for the angle)
  • Use the apply_angle method with the angle to rotate the image

I've been trying to use these points and the least squares approach to compute a linear transformation matrix that transforms points from the source to the target image.

I know I need to create a rotation matrix, but having never taken algebra I'm a bit lost. I've done lots of reading, and tried using scipy's built-in procrustes to do an affine transformation below (which may be all wrong).

m1, m2, d = scipy.spatial.procrustes(target_points, source_points)
a = np.dot(m1.T, m2, out=None) / norm(m1)**2

#separate x and y for the sake of convenience
ref_x = m2[::2]
ref_y = m2[1::2]

x = m1[::2]
y = m1[1::2]

b = np.sum(x*ref_y - ref_x*y) / norm(m1)**2

scale = np.sqrt(a**2+b**2)
theta = atan(b / max(a.all(), 10**-10)) #avoid dividing by 0

degrees = cos(radians(theta))

apply_angle(source_img, degrees)

However, this is not giving me the result I would expect. It's giving me a degree around 1, where I would expect a degree around 72. I suspect that the degree is what's needed to rotate the image as the angle parameter.

Any help would be hugely appreciated. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文