簡體   English   中英

如何將2d陣列旋轉LESS超過90°,達到最佳近似值?

[英]How can I rotate a 2d array by LESS than 90°, to the best approximation?

假設我有一個以0°旋轉存儲的數組:

0 0 1 0 0
0 0 1 0 0 
1 1 1 0 0 
0 0 0 0 0
0 0 0 0 0 

如果我通過,我希望它以良好的近似值返回,例如30°作為參數,它將是這樣的:

0 0 0 1 0
1 1 0 1 0
0 0 1 0 0 
0 0 0 0 0 
0 0 0 0 0 

45°會

1 0 0 0 1
0 1 0 1 0
0 0 1 0 0 
0 0 0 0 0 
0 0 0 0 0

我知道90°旋轉的解決方案。 但我不認為這會對我有幫助嗎?

我沒有任何示例代碼,因為我目前甚至不知道從哪里開始查找。 如果有任何關鍵詞我可以google指向我的方向我可以適應這個,這也將是偉大的。

Spectre在C#中的代碼解決方案:

    class Rotation
{

    public Rotation() {
        A = new int[xs,ys]{
{0,0,0,9,0,0,0},
{0,0,0,9,0,0,0},
{0,0,0,9,0,0,0},
{9,9,9,9,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
};
        B = new int[xs, ys];

        deg = (float)(Math.PI / 180.0);
    }

    public const int xs = 7; // matrix size
    public const int ys = 7;
    const int x0 = 3; // rotation center cell
    const int y0 = 3;
    readonly float deg; 
    public int[,] A;
    public int[,] B;

    //---------------------------------------------------------------------------

    public void rotcv(float ang) {
        rotcw(Rotation.x0, Rotation.y0, ang);
    }
    private void rotcw(int x0, int y0, float ang) // rotate A -> B by angle ang around (x0,y0) CW if ang>0
    {
        int x, y, ix0, iy0, ix1, iy1, q;
        double xx, yy, fx, fy, c, s;
        // circle kernel
        c = Math.Cos(-ang); s = Math.Sin(-ang);
        // rotate
        for (y = 0; y < ys; y++)
            for (x = 0; x < xs; x++)
            {
                // offset so (0,0) is center of rotation
                xx = x - x0;
                yy = y - y0;
                // rotate (fx,fy) by ang
                fx = ((xx * c) - (yy * s));
                fy = ((xx * s) + (yy * c));
                // offset back and convert to ints and weights
                fx += x0; ix0 = (int)Math.Floor(fx); fx -= ix0; ix1 = ix0 + 1; if (ix1 >= xs) ix1 = ix0;
                fy += y0; iy0 = (int)Math.Floor (fy); fy -= iy0; iy1 = iy0 + 1; if (iy1 >= ys) iy1 = iy0;
                // bilinear interpolation A[fx][fy] -> B[x][y]
                if ((ix0 >= 0) && (ix0 < xs) && (iy0 >= 0) && (iy0 < ys))
                {
                    xx = (A[ix0,iy0]) + ((A[ix1,iy0] - A[ix0,iy0]) * fx);
                    yy = (A[ix0,iy0]) + ((A[ix1,iy0] - A[ix0,iy0]) * fx);
                    xx = xx + ((yy - xx) * fy); q =(int) xx;
                }
                else q = 0;
                B[x,y] = q;
            }
    }
}

測試:

 static void Main(string[] args)
    {
        Rotation rot = new Rotation();

        for (int x = 0; x < Rotation.xs; x++) {
            for (int y = 0; y < Rotation.xs; y++) {
                Console.Write(rot.A[x,y] + " ");
            }
            Console.WriteLine();
        }
        Console.WriteLine();
        float rotAngle = 0;
        while (true)
        {
            rotAngle += (float)(Math.PI/180f)*90;
            rot.rotcv(rotAngle);
            for (int x = 0; x < Rotation.xs; x++)
            {
                for (int y = 0; y < Rotation.xs; y++)
                {
                    Console.Write(rot.B[x, y] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.ReadLine();
        }

    }

想象一下,你的矩陣是一個2D世界,其原點(0,0)是中間的像素。 然后,例如,左上角點的坐標為(-2,2)。 如果將此向量乘以2D旋轉矩陣,您將獲得旋轉世界中的新坐標。 在這里你可以隨意近似(圓形,地板,ciel,......)。

這是旋轉矩陣定義:

 | cos a      -sin a|
 | sin a      cos a |

其中a是你的旋轉角度。 以下是你如何相乘:

https://wikimedia.org/api/rest_v1/media/math/render/svg/50622f9a4a7ba2961f5df5f7e0882983cf2f1d2f

在該圖像中,(x,y)是原始坐標(例如,左上角像素的(-2,2)),(x',y')是新的(近似的)坐標。

在Photoshop,GIMP,...和Imagemagick中,它已經多次用於圖像處理。

您可以使用庫(例如帶有numpy的Python)將矩陣轉換為位圖圖像,ImageMagick將其旋轉x度並將旋轉后的圖像的結果讀回矩陣。 您必須決定如何處理角點,如果要將* n網格旋轉到* n網格中,它們將被截斷。

它可以在幾行中完成,您不必重新發明輪子,如果它不完全落在單元格中,您將在旋轉點周圍獲得灰度值。

迷你圖片: 在此輸入圖像描述 在此輸入圖像描述

使用Python讀取結果:

import matplotlib.image as img
image = img.imread('matrix.png')

print image[:,:,0] # Only reads grayscale information.



# Original
[[ 0.  0.  1.  0.  0.]
 [ 0.  0.  1.  0.  0.]
 [ 1.  1.  1.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]]

# Rotated by 30°
[[ 0.07450981  0.          0.08235294  0.8509804   0.        ]
 [ 0.68627453  0.82745099  0.53725493  0.78039217  0.        ]
 [ 0.          0.50980395  0.97647059  0.22745098  0.        ]
 [ 0.          0.          0.05882353  0.          0.        ]
 [ 0.          0.          0.          0.          0.        ]]

對於Java解決方案, AffineTransform可以為您提供幫助。 是一個例子。

好的,這是承諾的。 第一個C ++代碼:

//---------------------------------------------------------------------------
#include <math.h>
//---------------------------------------------------------------------------
const int xs=7; // matrix size
const int ys=7;
const int x0=3; // rotation center cell
const int y0=3;
const float deg=M_PI/180.0;
int A[xs][ys]=
    {
    {0,0,0,9,0,0,0},
    {0,0,0,9,0,0,0},
    {0,0,0,9,0,0,0},
    {9,9,9,9,0,0,0},
    {0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0},
    };
int B[xs][ys];
//---------------------------------------------------------------------------
void rotcw(int x0,int y0,float ang) // rotate A -> B by angle ang around (x0,y0) CW if ang>0
    {
    int x,y,ix0,iy0,ix1,iy1,q;
    float xx,yy,fx,fy,c,s;
    // circle kernel
    c=cos(-ang); s=sin(-ang);
    // rotate
    for (y=0;y<ys;y++)
     for (x=0;x<xs;x++)
        {
        // offset so (0,0) is center of rotation
        xx=x-x0;
        yy=y-y0;
        // rotate (fx,fy) by ang
        fx=float((xx*c)-(yy*s));
        fy=float((xx*s)+(yy*c));
        // offset back and convert to ints and weights
        fx+=x0; ix0=floor(fx); fx-=ix0; ix1=ix0+1; if (ix1>=xs) ix1=ix0;
        fy+=y0; iy0=floor(fy); fy-=iy0; iy1=iy0+1; if (iy1>=ys) iy1=iy0;
        // bilinear interpolation A[ix0+fx][iy0+fy] -> B[x][y]
        if ((ix0>=0)&&(ix0<xs)&&(iy0>=0)&&(iy0<ys))
            {
            xx=float(A[ix0][iy0])+(float(A[ix1][iy0]-A[ix0][iy0])*fx);
            yy=float(A[ix0][iy0])+(float(A[ix1][iy0]-A[ix0][iy0])*fx);
            xx=xx+((yy-xx)*fy); q=xx;
            } else q=0;
        B[x][y]=q;
        }
    }
//---------------------------------------------------------------------------

這里7x7預覽15度的步驟:

預習

可能需要稍微調整一半的細胞或一些東西(中心根據我的喜好流血太多)

矩陣A是源, B是目標......

您還可以添加閾值...如:

if (q>=5) q=9; else q=0;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM