Ask Your Question

Apply quadratic function to an image

asked 2017-08-28 09:08:26 -0500

lock042 gravatar image


I have a quadratic function I would like to apply to my image. The transformation has the following form:

  • x' = A + Bx + Cy + Dxx + Exy + Fyy
  • y' = G + Hx + Iy + Jxx + Kxy + Lyy

For a linear transformation:

  • x' = A + Bx + Cy
  • y' = D + Ex + Fy

it was easy because I could use WarpAffine by putting the coefficients in a 3x2 matrix. Moreother, is it easy to get the value of rotation angle and scale ?

Again, with the linear transformation, rotation angle is easily computed with atan2(B, C). Scale is (sqrt(BB + CC) and shifts are A and D.

edit retag flag offensive close merge delete


@LBerger provided a sample code in this post ( don't forget to upvote the question and the answer if you find it useful)

sturkmen gravatar imagesturkmen ( 2017-08-28 10:40:34 -0500 )edit

It's not the same...the question is about a quadratic deformation of the image, the answer you linked creates a quadratic brightness function...

kbarni gravatar imagekbarni ( 2017-08-28 13:39:10 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-08-28 10:29:07 -0500

kbarni gravatar image

updated 2017-08-29 03:49:25 -0500

If you have the formula and the parameters, you can use it directly on the image.

for(int y=0;y<src.rows;y++)
    for(int x=0;x<src.cols;x++) {

or, even better, compute the inverse function: x=M+Nx'+Oy'+Px'x'+Qx'y'+Ry'y'... and do the same by iterating xp and yp.

Note: check if xp and yp are in the image before setting them.

If it works well, you can to parallelize the outer loop using TBB to make it very fast.

Concerning the rotation and scale: they are still affine operations, so they affect the x and y terms. So you can get their values using the B and C parameters.

edit flag offensive delete link more


Thank you for your answers. But I'm afraid that is a too easy approach. Indeed, I think I would need interpolation algorithm in this case. This is the reason I would expect a function doing that. A good think could be to transform my matrix to an homographic matrix, but I don't know how to do it.

lock042 gravatar imagelock042 ( 2017-08-28 15:16:44 -0500 )edit

It's simple to interpolate. You go with the inverse formula (calculate x,y from x',y'), and instead of,y) use the following linear interpolation function:


where xi=(int)x and xf=x-xi

kbarni gravatar imagekbarni ( 2017-08-29 03:48:45 -0500 )edit

Oh gosh .... I have to find the the way to compute the inverse formula. Thank you.

lock042 gravatar imagelock042 ( 2017-08-29 04:20:50 -0500 )edit

Question Tools

1 follower


Asked: 2017-08-28 09:08:26 -0500

Seen: 251 times

Last updated: Aug 29 '17