Rotate image math (C#)

前端 未结 6 2209
醉话见心
醉话见心 2020-12-05 08:39

I have an image with two points, aligned something like this:

|----------------|
|                |
|    .           |
|                |
|          .     |
         


        
6条回答
  •  既然无缘
    2020-12-05 08:53

    Performing a general 2D transformation involves solving a pair of eqautions with 6 unknowns.

    'x = xA + yB + C

    'y = xD + yE + D

    Given 3 corresponding points, you will have 6 knowns and the system can be solved. You only have 4 points in this case, since you don't care about shear, but you could imagine introducing a 3rd point at 90 degrees to the line formed by the other two points. Creating a rotated image is then ( pseudo codedily ) just something like:

    for ( y = 0; y < height; y++ )
     for ( x = 0; x < width; x++ )
      {
        newx = x*A + y*B + C;
        newy = x*D + y*D + E;
        newimage(x,y ) = oldimage( newx, newy );
      }
    }
    

    If performance is important, the multiplies in the inner loop can be optimised away by noting that y*B only changes in the outer look and that newx,newy change by constants A and D in the inner loop.

提交回复
热议问题