Ask Your Question
2

warpAffine issues

asked 2012-12-08 07:10:33 -0600

bouletfrites gravatar image

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2012-12-08 17:07:34 -0600

Michael Burdinov gravatar image

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.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-12-08 07:10:33 -0600

Seen: 1,510 times

Last updated: Dec 08 '12