cv::resize and cv::warpAffine produce different results (Bugfix #4133)


Added by Yida Wang about 10 years ago. Updated almost 10 years ago.


Status:Cancelled Start date:2013-08-12
Priority:High Due date:
Assignee:Roman Donchenko % Done:

100%

Category:imgproc, video
Target version:-
Affected version:branch '2.4' (2.4-dev) Operating System:Any
Difficulty:Hard HW Platform:Any
Pull request:

Description

Code to reproduce:

 1Mat tst(1, 2, CV_8U);
 2tst.at<uchar>(0,0) = 0;
 3tst.at<uchar>(0,1) = 128;
 4
 5Mat transf(2,3, CV_32F);
 6transf.at<float>(0, 0) = 5;
 7transf.at<float>(0, 1) = 0;
 8transf.at<float>(0, 2) = 0;
 9transf.at<float>(1, 0) = 0;
10transf.at<float>(1, 1) = 1;
11transf.at<float>(1, 2) = 0;
12
13Mat r1,r2;
14resize(tst, r1, Size(), 5, 1, INTER_LINEAR);
15warpAffine(tst, r2, transf, r1.size(), INTER_LINEAR, BORDER_REPLICATE);
16
17cout << "r1: " << r1 << endl;
18cout << "r2: " << r2 << endl;

Ouput:

r1: [0, 0, 0, 26, 51, 77, 102, 128, 128, 128]
r2: [0, 24, 52, 76, 104, 128, 128, 128, 128, 128]

The problem lies in the affine Metrix, it is wrongly designed, to get a fine affine Metrix, you need to set up three points on the source and destination images to compute an affine transformation using:

transf = getAffineTransform( srcTri, dstTri );

and the source code should be rewrite as:
transf.at<float>(1, 1) = 0;
transf.at<float>(1, 1) = 1;

So,
New Ouput:

r1: [0, 0, 0, 26, 51, 77, 102, 128, 128, 128]
r2: [0, 0, 0, 24, 52, 76, 104, 128, 128, 128]


Associated revisions

Revision b98a54fb
Added by Maksim Shabunin over 9 years ago

Merge pull request #4133 from ruslo:interface.sample

History

Updated by Aly Osama about 10 years ago

Could you run this testcase and tell me your result

Mat tst(2, 2, CV_8U);
tst.at<uchar>(0,0) = 1;
tst.at<uchar>(0,1) = 4;
tst.at<uchar>(1,0) = 4;
tst.at<uchar>(1,1) = 7;

Mat transf(2,3, CV_32F);
transf.at<float>(0, 0) = 2;
transf.at<float>(0, 1) = 0;
transf.at<float>(0, 2) = 0;
transf.at<float>(1, 0) = 0;
transf.at<float>(1, 1) = 2;
transf.at<float>(1, 2) = 0;

Updated by Yida Wang about 10 years ago

r2: [1, 3, 4, 4, 4, 4, 4, 4, 4, 4;
3, 4, 6, 6, 6, 6, 6, 6, 6, 6]

Updated by Aly Osama about 10 years ago

it should be as r1 so the problem is at wrapAffine function

r1:
[ 1, 2, 3, 4;
2, 2, 4, 5;
3, 4, 5, 6;
4, 5, 6, 7]

Updated by Ankit Dhiman about 10 years ago

I tried using warpPerspective function.
Mat tst(2, 2, CV_8U);
tst.at<uchar>(0,0) = 1;
tst.at<uchar>(0,1) = 4;
tst.at<uchar>(1,0) = 4;
tst.at<uchar>(1,1) = 7;
Mat transf1(3,3, CV_32F);
transf1.at<float>(0, 0) = 2;
transf1.at<float>(0, 1) = 0;
transf1.at<float>(0, 2) = 0;
transf1.at<float>(1, 0) = 0;
transf1.at<float>(1, 1) = 2;
transf1.at<float>(1, 2) = 0;
transf1.at<float>(2, 0) = 0;
transf1.at<float>(2, 1) = 0;
transf1.at<float>(2, 2) = 1;
Mat r1,r3;
resize(tst, r1, Size(), 2, 2, INTER_LINEAR);
warpPerspective(tst,r3,transf1,r1.size(), INTER_LINEAR,BORDER_REPLICATE);

Output : [1 3 4 4 ;
3 4 6 6;
4 6 7 7;
4 6 7 7]
If I perform silmilar steps in Matlab. Output by imresize and imtransform are same. Has this issue been resolved?

Updated by Vadim Pisarevsky almost 10 years ago

not sure about the essence of the bug report

  • Status changed from Open to Cancelled

Also available in: Atom PDF