OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Wed, 02 Jan 2019 06:16:28 -0600Lanczos5 instead of Lanczos4http://answers.opencv.org/question/206261/lanczos5-instead-of-lanczos4/ Is there any possibility to use Lanczos5 instead of Lanczos4 as interpolation in "warpAffine"?felipefetzerWed, 02 Jan 2019 06:16:28 -0600http://answers.opencv.org/question/206261/How can I rewrite this warp-affine using OpenCV?http://answers.opencv.org/question/139463/how-can-i-rewrite-this-warp-affine-using-opencv/I'm trying to optimize [this][1] code, in particular:
bool interpolate(const Mat &im, float ofsx, float ofsy, float a11, float a12, float a21, float a22, Mat &res)
{
bool ret = false;
// input size (-1 for the safe bilinear interpolation)
const int width = im.cols-1;
const int height = im.rows-1;
// output size
const int halfWidth = res.cols >> 1;
const int halfHeight = res.rows >> 1;
float *out = res.ptr<float>(0);
for (int j=-halfHeight; j<=halfHeight; ++j)
{
const float rx = ofsx + j * a12;
const float ry = ofsy + j * a22;
for(int i=-halfWidth; i<=halfWidth; ++i)
{
float wx = rx + i * a11;
float wy = ry + i * a21;
const int x = (int) floor(wx);
const int y = (int) floor(wy);
if (x >= 0 && y >= 0 && x < width && y < height)
{
// compute weights
wx -= x; wy -= y;
// bilinear interpolation
*out++ =
(1.0f - wy) * ((1.0f - wx) * im.at<float>(y,x) + wx * im.at<float>(y,x+1)) +
( wy) * ((1.0f - wx) * im.at<float>(y+1,x) + wx * im.at<float>(y+1,x+1));
} else {
*out++ = 0;
ret = true; // touching boundary of the input
}
}
}
return ret;
}
According to Intel Advisor, this is a very time consuming function. In [this][2] question I asked how I could optimize this, and [someone][3] made me notice that this is warp-affine transformation.
Now, since I'm not the image processing guy, I had to read [this][4] article to understand what a warp-affine transformation is.
To my understanding, given a point `p=(x,y)`, you apply a transformation `A` (a 2x2 matrix) and then translate it by a vector `b`. So the obtained point after the transformation `p'` can be expressed as `p' = A*p+b`. So far so good.
However, I'm a little bit confused on how to apply [`cv::warpAffine()`](http://docs.opencv.org/trunk/d5/df1/group__imgproc__hal__functions.html#ga8a534cca6fb845c9ac77f10c35f67c0c) to this case. First of all, from the function above `interpolate()` I can see only the 4 `A` components (`a11`, `a12`, `a21`, `a22`), while I can't see the 2 `b` components...Are they `ofsx` and `ofy`?
**In addition** notice that this function returns a bool value, which is not returned by `warpAffine` (this boolean value is used [here](https://github.com/perdoch/hesaff/blob/master/affine.cpp) at line 126), so I don't know I could this with the OpenCV function.
But most of all I'm so confused by `for (int j=-halfHeight; j<=halfHeight; ++j)` and `for(int i=-halfWidth; i<=halfWidth; ++i)` and all the crap that happens inside.
I understand that:
// bilinear interpolation
*out++ =
(1.0f - wy) * ((1.0f - wx) * im.at<float>(y,x) + wx * im.at<float>(y,x+1)) +
( wy) * ((1.0f - wx) * im.at<float>(y+1,x) + wx * im.at<float>(y+1,x+1));
Is what [`INTER_LINEAR`](http://docs.opencv.org/3.1.0/da/d54/group__imgproc__transform.html#gga5bb5a1fea74ea38e1a5445ca803ff121ac97d8e4880d8b5d509e96825c7522deb) does, but apart from that I'm totally lost.
So, to test my approach, I tried to do the equivalent of line 131 of [this][5] as:
bool touchesBoundary = interpolate(smoothed, (float)(patchImageSize>>1), (float)(patchImageSize>>1), imageToPatchScale, 0, 0, imageToPatchScale, patch);
Mat warp_mat( 2, 3, CV_32FC1 );
warp_mat.at<float>(0,0) = imageToPatchScale;
warp_mat.at<float>(0,1) = 0;
warp_mat.at<float>(0,2) = (float)(patchImageSize>>1);
warp_mat.at<float>(1,0) = 0;
warp_mat.at<float>(1,1) = imageToPatchScale;
warp_mat.at<float>(1,2) = (float)(patchImageSize>>1);
cv::Mat myPatch;
std::cout<<"Applying warpAffine"<<std::endl;
warpAffine(smoothed, myPatch, warp_mat, patch.size());
std::cout<<"WarpAffineApplied patch size="<<patch.size()<<" myPatch size="<<myPatch.size()<<std::endl;
cv::Mat diff = patch!=myPatch;
if(cv::countNonZero(diff) != 0){
throw std::runtime_error("Warp affine doesn't work!");
}
else{
std::cout<<"It's working!"<<std::endl;
}
And of course at the first time the this is executed, the exception is thrown (so the two methods are not equivalent)...How can I solve this?
Can someone help me please?
[1]: https://github.com/perdoch/hesaff/blob/master/helpers.cpp
[2]: http://stackoverflow.com/questions/43141124/is-it-possible-to-vectorize-this-nested-for-with-sse
[3]: http://stackoverflow.com/a/43142059/4480180
[4]: http://docs.opencv.org/3.1.0/d4/d61/tutorial_warp_affine.html
[5]: https://github.com/perdoch/hesaff/blob/master/affine.cpplovajWed, 12 Apr 2017 03:52:57 -0500http://answers.opencv.org/question/139463/strange warpAffine precision issuehttp://answers.opencv.org/question/62730/strange-warpaffine-precision-issue/I am trying to use OpenCVs warpAffine to transform an image. I use the bilinear interpolation option (set by default). When using a very small transformation (e.g., translation of 0.01 pixel), the transformed image is exactly identical to the original. This is problematic since I need a big precision as further computations are performed on the transformed image.
Example:
Mat img_test = (Mat_<double>(5,5) << 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0);
Mat tfm; Mat transf_img_test;
tfm = Mat::eye(2,3,CV_64F);
tfm.at<double>(0,2) = 0.01;
warpAffine( img_test, transf_img_test, tfm, img_test.size(), INTER_LINEAR);
cout << "Original = " << img_test << endl;
cout << "Transformed = " << transf_img_test << endl;
The two images are exactly identical:
Original = [0, 0, 0, 0, 0;
0, 0, 1, 0, 0;
0, 0, 1, 0, 0;
0, 0, 1, 0, 0;
0, 0, 0, 0, 0]
Transformed = [0, 0, 0, 0, 0;
0, 0, 1, 0, 0;
0, 0, 1, 0, 0;
0, 0, 1, 0, 0;
0, 0, 0, 0, 0]
Note that when I set
tfm.at<double>(0,2) = 0.5
instead, warpAffine correctly transforms (and interpolates) the image.
huloWed, 27 May 2015 12:37:54 -0500http://answers.opencv.org/question/62730/