2016-10-07 190 views
1

我目前正致力于在C#中实现EmguCV中的算法,这意味着我不想使用EmguCV附带的旋转函数中的构建。如何在Emgu CV中实现“旋转算法”?

我已经找到了我想要实现的算法,但是我有点被卡住了如何实现它。主要的问题是,我不知道如何指定我的矩阵的X和Y值来完成预期的计算。

旋转算法: http://i.stack.imgur.com/hQMxF.jpg

现在我的代码看起来是这样的:

static void Main(string[] args) { 
     Mat image = CvInvoke.Imread("C:\\Users\\Leon\\Desktop\\a.jpg", LoadImageType.Grayscale); 

     int height = image.Height; 
     int width = image.Width; 

     //Convert to Matrix 
     Matrix<Byte> matrix = new Matrix<Byte>(image.Rows, image.Cols, image.NumberOfChannels); 
     image.CopyTo(matrix); 

     Matrix<Byte> newMatrix = new Matrix<Byte>(image.Rows, image.Cols, image.NumberOfChannels); 
     image.CopyTo(newMatrix); 

     for (int i = 0; i < matrix.Rows-1; i++) 
     { 
      for (int j = 0; j < matrix.Cols-1; j++) 
      { 

      } 
     } 

     CvInvoke.Imshow("abc", matrix); 
     CvInvoke.WaitKey(0); 

    } 

但正如我所说,我在为如何实现算法疑问。我的计划是旋转“矩阵”中的像素并将它们存储在“newMatrix”中,但我不知道如何指定矩阵的X和Y值。

也许有人可以帮助我在这里。

编辑: 有人提出这个答案在这里:“How can I get and set pixel values of an EmguCV Mat image?”将是我的问题的答案。但事实并非如此。我知道我可以做Math.Cos和Math.Sin,但我不知道如何在我的矩阵中指定X和Y.我在访问Matrix中的数据时没有问题。

+0

的[我怎样才能获取和设置EmguCV垫图像的像素值,可能的复制? ](http://stackoverflow.com/questions/32255440/how-can-i-get-and-set-pixel-values-of-an-emgucv-mat-image) – slawekwin

回答

0

如果你想旋转的一些点(cx,cy)附加图像中给出的矩阵点(x,y)

class Program { 
    /** 
    * @param x coordinate of point want to rotate 
    * @param y coordinate of point want to rotate 
    * @param cx x coordinate of point you want to rotate about 
    * @param cy y coordinate of point you want to rotate about 
    * @return the result of rotation {x,y} 
    */ 
    static double[] rotate(double x, double y, double cx, double cy, double angle) { 
    double cos_a = Math.Cos(angle); 
    double sin_a = Math.Sin(angle); 

    // move to origin 
    x -= cx; 
    y -= cy; 

    // rotate and then move back 
    return new double[] { 
     x*cos_a - y*sin_a + cx, 
     x*sin_a + y*cos_a + cy 
    }; 
    } 


    static void Main(string[] args) { 
    double x = 1; 
    double y = 0; 
    double a = Math.PI/2; 

    double[] r = rotate(x, y, 0, 0, a); 
    Console.WriteLine("new x = " + r[0]); 
    Console.WriteLine("new y = " + r[1]); 
    } 
} 
+0

我试图硬编码旋转公式但结果有点不对。也许有人知道什么是错的? – Leon