我如何实现一个旋转算法?在Emgu CV
本文关键字:算法 旋转 CV Emgu 一个 何实现 实现 | 更新日期: 2023-09-27 18:11:55
我目前正致力于在c#中实现EmguCV中的算法,这意味着我不想使用EmguCV自带的内置旋转函数。
我已经找到了我想要实现的算法,但我有点卡在如何实现它。主要的问题是,我不知道如何指定我的矩阵的X和Y值,以便能够进行预期的计算。
旋转算法:https://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值。
也许有人能帮我一下。
编辑:有人建议,这里的答案:"我如何获得和设置EmguCV Mat图像的像素值?"将是我的问题的答案。但事实并非如此。我知道我能做数学。Cos和数学。Sin,但是我不知道如何在矩阵中指定X和Y。我没有问题访问数据在我的矩阵
根据附图中的矩阵,如果你想让点(x,y)
围绕点(cx,cy)
旋转:
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]);
}
}