MATLAB imwarp vs C++ warpAffine

asked 2017-12-16 09:56:57 -0600

mr.ao gravatar image

I am trying to convert following MATLAB code into c++ code by Opencv library

Matlab Code:

scale_x = 0.99951;
scale_y = 0.99951;
RsrcImg= imref2d(size(im1));
T=[scale_x 0 0;0 scale_y 0;0 0 1];
tformEstimate = affine2d(T);
destImg =imwarp(im1,tformEstimate,'Interp','cubic','OutputView',RsrcImg);

C++ Code:

double scale_x = 0.99951;
double scale_y = 0.99951;
Mat tformEstimate=Mat::zeros(2,3,CV_64FC1);
tformEstimate.at<double>(0,0)=scale_x;
tformEstimate.at<double>(1,1)=scale_y;
warpAffine(im1,destImg,tformEstimate,im1.size(),INTER_CUBIC);

but I get completely different PSNR values! is there any way to make both codes work with the same result?

INPUT:

im1=[19656,20452,21048,21020,20564;
20740,21332,21964,20932,20220;
21688,21832,20236,21520,21948;
19780,20572,21056,20748,20088;
20560,21188,20608,22136,20736]

Matlab output:

[19656.4613056001,20452.8984647534,21048.6416903208,21019.5009522438,0;
20741.1425789163,21333.2772155087,21963.2997618553,20930.5373907996,0; 
21687.3205560205,21830.7204384573,20235.1172001277,21521.5360776160,0;
19779.1003189859,20572.0033036149,21056.4880106032,20747.6616956745,0;
0,0,0,0,0]

C++ output:

[19656.00000  20452.00000  21048.00000  21020.00000  20564.00000  
20740.00000  21332.00000  21964.00000  20932.00000  20220.00000  
21688.00000  21832.00000  20236.00000  21520.00000  21948.00000  
19780.00000  20572.00000  21056.00000  20748.00000  20088.00000  
20560.00000  21188.00000  20608.00000  22136.00000  20736.00000]

I have seen MATLAB vs C++ vs OpenCV - imresize without success.

Thank you for your help.

edit retag flag offensive close merge delete

Comments

1

What does it mean 0.999551? original image is 5 rows and 5 columns what do you expect in destination image? Why does matlab give you 0 in last row and last column?

LBerger gravatar imageLBerger ( 2017-12-16 11:44:53 -0600 )edit

i don't have matlab, but your c++ tformEstimate seems to be lacking a

tformEstimate.at<double>(1,2)=1;
berak gravatar imageberak ( 2017-12-16 12:10:10 -0600 )edit

@LBerger I have 25 images that scale by scaleDecreaseStep = 0.00049 ratio in a for loop i=0:24 double scale_x = 1.0-(scaleDecreaseStepi); double scale_y = 1.0-(scaleDecreaseStepi); my orginal images size are 2500 * 2500 pixel, and in these images, matlab give me 0 in two last rows and two last column and Opencv give me negetive values!!

matlab give me 0 in last row and last column because FillValues 'FillValues' — Value used for output pixels outside image boundaries numeric scalar or array Value used for output pixels outside the input image boundaries, specified as the comma-separated pair consisting of 'FillValues' and a numeric array. Fill values are used for output pixels when the corresponding inverse transformed location in the input image is completely

mr.ao gravatar imagemr.ao ( 2017-12-16 13:16:02 -0600 )edit

@berak No!

mr.ao gravatar imagemr.ao ( 2017-12-16 13:20:31 -0600 )edit