1

I'm currently working on implementing an algorithm in EmguCV in C#, meaning that I don't want to use the build in rotate function that comes with EmguCV.

I've already found the algorithm that I want to implement, but I'm kinda stuck as how to implement it. The main problem is, that I don't know how the specify the X and Y values of my Matrix to be able to do the intended calculations.

Rotation Algorithm: https://i.stack.imgur.com/hQMxF.jpg

Right now my Code looks like this:

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);

    }

But as I said, I'm in doubt of as how to implement the algorithm. My plan was to rotate the pixels in "matrix" and store them in "newMatrix" but I do not know how to specify the X and Y values of my matrix.

Maybe someone can help me out here.

EDIT: There has been suggested that this answer here: "How can I get and set pixel values of an EmguCV Mat image?" will be an answer to my question. But it is not. I know that I can do Math.Cos and Math.Sin but I do not know how to specify X and Y in my Matrix. I don't have problems accessing the Data in my Matrix.

Community
  • 1
  • 1
Leon
  • 96
  • 5
  • Possible duplicate of [How can I get and set pixel values of an EmguCV Mat image?](http://stackoverflow.com/questions/32255440/how-can-i-get-and-set-pixel-values-of-an-emgucv-mat-image) – slawekwin Oct 07 '16 at 07:40

1 Answers1

0

If you're trying to rotate a point (x,y) about some point (cx,cy) given the matrix in the attached image:

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]);
  }
}
Bobas_Pett
  • 591
  • 5
  • 10
  • I've tried to hardcode the rotation formula but the result is kinda not right. Maybe someone knows what's wrong? – Leon Oct 12 '16 at 08:41