【问题标题】:The different result of Affine Transform on Python and MatlabPython和Matlab上仿射变换的不同结果
【发布时间】:2018-09-07 05:54:20
【问题描述】:

我用matlab做仿射变换,代码是:

Image=imread('fig2.bmp');
Image=im2double(Image);
Rot=0.001981123573629;                            %rotation parameter
XformScale=[1.022909263199710,1.029291261036412]; %resize parameter
XformTrans=[-0.405901345569081,0.456816768280493];%translation parameter
c = cos(Rot);
s = sin(Rot);
RRot = [ c, -s,  0; ...                           %rotation matrix
         s,  c,  0; ... 
         0,  0,  1  ];

RScale = eye(3);                                  %resize matrix
RScale(1,1) = XformScale(1);
RScale(2,2) = XformScale(2);

RTrans = eye(3);                                  %translation matrix
RTrans(end,1:2) = XformTrans;

%affine transform
FixAll = maketform('affine', RRot*RScale*RTrans);
NewSize = size(LensletImage(:,:,1)) .* XformScale(2:-1:1);
CorrectedImage = imtransform( Image, FixAll,'bilinear', 'YData',[1 NewSize(1)], 'XData',[1 NewSize(2)]);

我可以得到这样的图片enter image description here

然后我尝试使用相同的参数在 Python 中编写相同的代码,代码是:

from PIL import Image
import numpy as np
import math
import cv2

img=np.array(Image.open('fig2.bmp'))
img=np.double(img)/255.0
rows, cols,chan= img.shape
Rot=0.001981123573629                              # rotation parameter
XformScale=[1.022909263199710,1.029291261036412]   # resize parameter
XformTrans=[-0.405901345569081,0.456816768280493]  # translation parameter
#  T is the affine matrix
T=np.array([[math.cos(Rot)*XformScale[0],math.sin(Rot)*XformScale[0],     XformTrans[0]],[-math.sin(Rot)*XformScale[1],math.cos(Rot)*XformScale[1],XformTrans[1]]])

new_row = int(np.ceil(rows * XformScale[1]))        # new image size
new_col = int(np.ceil(cols * XformScale[0]))

Correct_img = cv2.warpAffine(img, T,(new_col,new_row),flags=cv2.INTER_LINEAR) # affine transform

结果图片为enter image description here

但是,当我计算这两张图片之间的 PSNR(峰值信噪比)时,结果只有 60.2748。(它应该是无限的。) 我不知道它们不同的原因,因为它们都使用双线性插值。希望你能帮助我。

我尝试了评论提供的方式,但似乎不起作用。代码是:

img=np.array(Image.open('fig2.bmp'))
img=np.double(img)/255.0
rows, cols,chan= img.shape
Rot=0.001981123573629                              # rotation parameter
XformScale=[1.022909263199710,1.029291261036412]   # resize parameter
XformTrans=[-0.405901345569081,0.456816768280493]  # translation parameter
#  T is the affine matrix
T=np.array([[math.cos(Rot)*XformScale[0],-math.sin(Rot)*XformScale[0],-    XformTrans[0]],[math.sin(Rot)*XformScale[1],math.cos(Rot)*XformScale[1],-XformTrans[1]],[0,0,1]])
Tinv=np.linalg.inv(T)                              # the inverse matrix
nn=Tinv[:2,:]

new_row = int(np.ceil(rows * XformScale[1]))        # new image size
new_col = int(np.ceil(cols * XformScale[0]))

Correct_img2 = cv2.warpAffine(img, nn,(new_col,new_row),flags=cv2.INTER_LINEAR) # affine transform

结果图片是enter image description here。感谢所有cmets!

【问题讨论】:

  • 请评论您的代码——逐行阐明您的目的。此外,如果您使用自定义函数,则需要阐明它们的作用并表明它们按预期工作。例如,您可以显示一些测试结果,或者您可以共享您的脚本并显示您期望它执行的操作。还请说明您的预期结果。当您有使用的图像时,请分享它。更好的是,用一个典型而简单的例子来讨论你的代码。最后但并非最不重要的一点是,请让您的问题的数学清晰,并保持您的问题针对编程问题。
  • 请参阅here 了解如何提问。
  • 非常抱歉,明天我会重新编辑问题并将结果放在上面。谢谢您的评论。
  • 你确定图片是对的吗?在所有 3 个链接中,我只看到白色背景。

标签: python matlab affinetransform cv2


【解决方案1】:

这个函数在Python和Matlab中的区别是:

Matlab : 将源图像的变换矩阵(x,y)映射到目标图像的(x',y')的参数就是变换矩阵的参数。

Python:源图像的变换矩阵(x,y)映射到目标图像的(x',y')的参数是INVERSE变换矩阵的参数。

也许this 可以帮助你。

希望对你有帮助!

【讨论】:

  • 如果问题归结到最后一行,其余的就在那里,那么这个问题写得非常糟糕。
  • 什么意思?
  • 这意味着我很惊讶你能弄清楚 OP 想要什么
  • 我不太确定,但我想他想知道 Python 和 Matlab 上的仿射变换之间的区别。上周遇到了同样的问题
  • 啊,我明白了。我不喜欢这个问题的写法。但我很高兴你能分享你发现的东西。这似乎是可以帮助很多人的事情。
【解决方案2】:

我在Python中重新编写了仿射变换,现在用matlab可以得到同样的结果。如果你想得到代码,请联系我。

matlab 使用的方法: 1、把图片分成几块。 2、然后做仿射变换(Image的边界有一些变化)

感谢您的 cmets!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2022-01-17
    • 2010-12-17
    • 1970-01-01
    • 2016-05-14
    • 2014-05-27
    相关资源
    最近更新 更多