Ask Your Question
0

Apply quadratic function to an image

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

lock042 gravatar image

Hello.

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

Comments

@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 -0600 )edit
1

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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

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

kbarni gravatar image

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

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++) {
        xp=A+B*x+C*y+D*x*x+E*x*y+F*y*y;
        yp=G+H*x+I*y+J*x*x+K*x*y+L*y*y;
        dst.at<uchar>(xp,yp)=src.at<uchar>at(x,y);
    }

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

Comments

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 -0600 )edit

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

(src.at<uchar>(xi,yi)*(1-xf)+src.at<uchar>(xi+1,yi)*xf)*(1-yf)+(src.at<uchar>(xi,yi+1)*(1-xf)+src.at<uchar>(xi+1,yi+1)*xf)*yf

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

kbarni gravatar imagekbarni ( 2017-08-29 03:48:45 -0600 )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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 577 times

Last updated: Aug 29 '17