1

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!

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Xufu Sun
  • 31
  • 2
  • Please comment your code -- clarifying your purposes from lines to lines. Also if you use custom functions, you need to clarify what they do and show that they work as intended. You can show some test results or you can share your script and show what you expect it to do, for example. Please also clarify your expected results. When you have an image you work with, please share it. Better yet, discuss your code with a typical and simple example. Last but not least, please make the mathematics of your question crystal clear and keep your question specific to programming issues. – Argyll Mar 28 '18 at 13:58
  • See [here](https://stackoverflow.com/help/mcve) for how to ask questions. – Argyll Mar 28 '18 at 14:00
  • I am so sorry,I will re-edit the question tomorrow and put the result on.Thank you for your comment. – Xufu Sun Mar 28 '18 at 14:36
  • are you sure the images are right? All I see is white background nothing else in all 3 links. – Argyll Mar 28 '18 at 15:47

2 Answers2

2

The difference between this function in Python and Matlab is :

Matlab : the parameters of the transform matrix mapping (x,y) of the source image to (x',y') in the destination image are the parameters of the transform matrix.

Python : the parameters of the transform matrix mapping (x,y) of the source image to (x',y') in the destination image are the parameters of the INVERSE transform matrix.

Maybe this could help you.

Hope it helps !

AdriBento
  • 589
  • 5
  • 16
  • If the issue comes down to the very last line, and the rest are just there, this question is VERY poorly written. – Argyll Mar 28 '18 at 14:01
  • What do you mean ? – AdriBento Mar 28 '18 at 14:05
  • it means I am surprised you can figure out what the OP wants – Argyll Mar 28 '18 at 14:14
  • I'm not totally sure but I think he want to know the difference between the affine transform on Python and Matlab. Had the same problem last week – AdriBento Mar 28 '18 at 14:17
  • 1
    Ah I see. I don't like how the question is written. But I am glad you are sharing what you found. It seems like the type of things that can help a lot of people. – Argyll Mar 28 '18 at 14:20
  • Sorry,this is my fist time to ask question on this website.You are right, I do not understand the difference between the affine transform on Python and Matlab.I have tried your solution ,but it seems not work. So can you give me an example of your affine transform?Thank you very much! In my country,it is already midnight,so i will re-edit my question tomorrow. – Xufu Sun Mar 28 '18 at 14:35
  • Mine was the same than in https://stackoverflow.com/questions/17056209/python-pil-affine-transformation – AdriBento Mar 28 '18 at 15:11
  • I have re-edited the questions and put the code on.Can you help me to inspect where is wrong?Thank you! – Xufu Sun Mar 28 '18 at 15:18
  • Maybe you can try to use `transform` instead of `warpAffine` – AdriBento Mar 28 '18 at 15:31
0

I have re-writed the affine transform in Python.Now it can get the same result campeared with matlab.If you want to get the code,Please cantact with me.

The methords matlab use: 1、Divide the image into some pieces. 2、Then do the affine transform(There are some changes at the boundaries of the Image)

Thank you for your comments!

Xufu Sun
  • 31
  • 2