I use Matlab to make Affine Transfom,the code is :
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)]);
And I can get the picture like thisenter image description here
Then I try to write the same code in Python using the same parameters ,the code is :
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
The result picture is enter image description here
But,when i calculate the PSNR(Peak signal noise ratio) between these two pictures,the result is only 60.2748.(It should be infinite.) I do not know the reason why they are different as they both use bilinear interpolation.Hope you can help me .
I tried the way provided by the comment,but it seems do not work.The code is :
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
And the result picture is enter image description here.Thank for all your comments!