C# 用矩阵规范化C语言中的变换图像#
考虑下面的两幅图像(分别为原始图像和转换图像)。三个蓝色正方形(标记)用于定向 原始图像:C# 用矩阵规范化C语言中的变换图像#,c#,.net,image,matrix,transformation,C#,.net,Image,Matrix,Transformation,考虑下面的两幅图像(分别为原始图像和转换图像)。三个蓝色正方形(标记)用于定向 原始图像: 我们知道宽度,高度 我们知道所有三个标记的(x,y)坐标 转换图像: 我们可以检测所有三个标记的(x,y)坐标 因此,我们可以计算旋转角度、(x,y)平移量和(x,y)比例因子 现在,我想使用System.Drawing.Graphics对象执行旋转变换、平移变换和缩放变换。问题是,生成的图像与原始图像不同 关于堆栈溢出,我被告知应用转换的顺序并不重要,但我的观察结果不同。下面是一些生成原始图像
- 我们知道宽度,高度
- 我们知道所有三个标记的(x,y)坐标
- 我们可以检测所有三个标记的(x,y)坐标
- 因此,我们可以计算旋转角度、(x,y)平移量和(x,y)比例因子
public static void GenerateImages ()
{
int width = 200;
int height = 200;
string filename = "";
System.Drawing.Bitmap original = null; // Original image.
System.Drawing.Bitmap transformed = null; // Transformed image.
System.Drawing.Graphics graphics = null; // Drawing context.
// Generate original image.
original = new System.Drawing.Bitmap(width, height);
graphics = System.Drawing.Graphics.FromImage(original);
graphics.Clear(System.Drawing.Color.MintCream);
graphics.DrawRectangle(System.Drawing.Pens.Red, 0, 0, original.Width - 1, original.Height - 1);
graphics.FillRectangle(System.Drawing.Brushes.Blue, 10, 10, 20, 20);
graphics.FillRectangle(System.Drawing.Brushes.Blue, original.Width - 31, 10, 20, 20);
graphics.FillRectangle(System.Drawing.Brushes.Blue, original.Width - 31, original.Height - 31, 20, 20);
filename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Original.png");
original.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
graphics.Dispose();
// Generate transformed images.
transformed = new System.Drawing.Bitmap(width, height);
graphics = System.Drawing.Graphics.FromImage(transformed);
graphics.Clear(System.Drawing.Color.LightBlue);
graphics.ScaleTransform(0.5F, 0.7F); // Add arbitrary transformation.
graphics.RotateTransform(8); // Add arbitrary transformation.
graphics.TranslateTransform(100, 50); // Add arbitrary transformation.
graphics.DrawImage(original, 0, 0);
filename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Transformed.png");
transformed.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
graphics.Dispose();
transformed.Dispose();
original.Dispose();
System.Diagnostics.Process.Start(filename);
}
我可以在这里看到两个潜在问题:
- 由于变换会一个接一个地应用,因此它们会使最初计算的值变得无用
- 图形对象在(0,0)坐标处应用旋转,我应该在这里做一些不同的事情。不知道是什么
- 翻译(100,50)
- 刻度(0.5,0.7)
- 刻度(0.5,0.7)
- 翻译(100,50)
这意味着,通过先缩放,然后平移,缩放也会缩放平移量,这就是为什么在两种情况下,图片的左上角为(50,35),平移X的一半,平移Y的.7。第二种可能性很可能存在。虽然您被告知转换的顺序并不重要,但在一般情况下并非如此。缩放和平移都是交换的,并且是相互交换的。旋转仅在某些限制条件下是可交换的-尤其是必须围绕图像的某个点旋转,而不是围绕图像所在的坐标系旋转。很抱歉,我无法帮你处理代码。@ChrisWalton:谢谢。我想我需要某种线性函数来计算一个向量相对于另一个向量,而不是以这种方式应用变换。我正在查看,看看它是否是我所需要的。刚刚试用了你的代码,它通过改变缩放和平移的顺序,即使不旋转也能产生不同的结果。@Xantix:没错!我不熟悉转换,所以不确定我的方法是错误的还是代码实现是错误的。我需要帮助计算旋转角度和缩放角度。我从原始图像和新图像中了解点。请引导我。