# warpAffine issues

Hi,

I want to calculate manually the new coordinates of points using a transformation matrix, and then copy some values of the original image to their new coordinates in another one. But i don't have the same result by using warpAffine, mine is obvsiously wrong but i don't know why. I used the formula given in the documentation http://opencv.willowgarage.com/documentation/cpp/imgproc_geometric_image_transformations.html#warpAffine

Here is my code:

for (int i = 0; i < depthMap->height;i++)
{
for (int j = 0; j < depthMap->width;j++)
{

int newJ = (int)(m11*j + m12*i + m13);
int newI = (int)(m21*j + m22*i + m23);

if (newI >= 0 && newI < depth->height && newJ >= 0 && newJ < depth->width)
setDepth(depth, newI, newJ, getDepth(depthMap, i ,j));

else
setDepth(depth, newI, newJ, 0);
}
}


m_xx are the components of my matrix (floats). The two images obtained with warpAffine and this method are similar but no pixels fits where it should be.

edit retag close merge delete

Sort by » oldest newest most voted

The problem is that you are trying to treat newI and newJ as integer values while they are fractional. So every pixel of transformed image should be interpolation of number of pixels of original image. Note also that there number of interpolation methods in wrapAffine. Default one is linear interpolation. But you can choose other methods as well.

Another problem in your code is that the loops is over all pixels of original image. This is wrong for many reasons. Simplest of them (but not the only one) is that size of destination image may be different from size of original image. If destination image is bigger, some of its pixels will not be initialized. If destination image is smaller, you will initialize each pixel number of times. Instead the loop should be over all pixels of destination image. For each pixel you should calculate inverse transform to coordinates of original image, and take the values of relevant pixels.

more

Official site

GitHub

Wiki

Documentation