Java rotation of pixel array

后端 未结 2 1413
陌清茗
陌清茗 2020-12-18 12:55

I have tried to make an algorithm in java to rotate a 2-d pixel array(Not restricted to 90 degrees), the only problem i have with this is: the end result leaves me with dots

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-18 13:20

    To avoid holes you can:

    1. compute the source coordinate from destination

      (just reverse the computation to your current state) it is the same as Douglas Zare answer

    2. use bilinear or better filtering

    3. use less then single pixel step

      usually 0.75 pixel is enough for covering the holes but you need to use floats instead of ints which sometimes is not possible (due to performance and or missing implementation or other reasons)

    Distortion

    if your image get distorted then you do not have aspect ratio correctly applied so x-pixel size is different then y-pixel size. You need to add scale to one axis so it matches the device/transforms applied. Here few hints:

    • Is the source image and destination image separate (not in place)? so Main.pixels and pixels are not the same thing... otherwise you are overwriting some pixels before their usage which could be another cause of distortion.

    • Just have realized you have cos,cos and sin,sin in rotation formula which is non standard and may be you got the angle delta wrongly signed somewhere so

    Just to be sure here an example of the bullet #1. (reverse) with standard rotation formula (C++):

    float c=Math.cos(-rotation);
    float s=Math.sin(-rotation);
    int x0=Main.width/2;
    int y0=Main.height/2;
    int x1=      width/2;
    int y1=      height/2;
    for (int a=0,y=0; y < Main.height; y++)
     for (int     x=0; x < Main.width; x++,a++)
      {
      // coordinate inside dst image rotation center biased
      int xp=x-x0;
      int yp=y-y0;
      // rotate inverse
      int xx=int(float(float(xp)*c-float(yp)*s));
      int yy=int(float(float(xp)*s+float(yp)*c));
      // coordinate inside src image
      xp=xx+x1;
      yp=yy+y1;
      if ((xp>=0)&&(xp=0)&&(yp

提交回复
热议问题