如何使用目标图像,地标坐标,最小二乘接近和旋转矩阵旋转2D图像?
我有两个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论