1
我正在研究类中的函数来存储图像。这个函数的目的是使用2D矩阵执行线性变换(我不打算支持翻译)。我想要做的方式是测试变换原始图像角点的坐标,并使用极值来计算所得变换图像的必要尺寸。反转2D矩阵的问题
一个我有新图像,我想循环遍历新图像中的所有像素,并使用逆变换计算新像素应该从原始图像获取其颜色的位置(使用双线性插值)。
我开始写下面的函数。它还没有对图像做任何事情。问题是,当我通过变换函数传递一组坐标,然后通过反函数传递这些新坐标时,我希望得到原始值。但是我没有回到原来的价值。
Image Image::matrix_transform(float m11, float m12, float m21, float m22) const
{
float det = m11 * m22 - m12 * m21;
img_assert(det, "Matrix not invertible.");
//inverse matrix values
float im11 = m22/det;
float im22 = m11/det;
float im21 = -m21/det;
float im12 = -m12/det;
//transformation
const auto t = [m11, m12, m21, m22](float& x, float& y)
{
x = x * m11 + y * m12;
y = x * m21 + y * m22;
};
//inverse
const auto ti = [im11, im12, im21, im22](float& x, float& y)
{
x = x * im11 + y * im12;
y = x * im21 + y * im22;
};
float x00 = 0.0f, y00 = 0.0f;
float x11 = w, y11 = h;
float x10 = 0.0f, y10 = h;
float x01 = w, y01 = 0.0f;
std::cout << x00 << " " << y00 << std::endl;
std::cout << x01 << " " << y01 << std::endl;
std::cout << x10 << " " << y10 << std::endl;
std::cout << x11 << " " << y11 << std::endl;
t(x00, y00);
t(x11, y11);
t(x10, y10);
t(x01, y01);
ti(x00, y00);
ti(x11, y11);
ti(x10, y10);
ti(x01, y01);
std::cout << x00 << " " << y00 << std::endl;
std::cout << x01 << " " << y01 << std::endl;
std::cout << x10 << " " << y10 << std::endl;
std::cout << x11 << " " << y11 << std::endl;
return *this;
}
这里是我如何调用函数(简单的旋转矩阵)。
img.matrix_transform(cos(angle), -sin(angle), sin(angle), cos(angle));
这里是输出我得到...
0 0
2500 0
0 1655
2500 1655
0 0
1975.95 -722.386
-743.978 629.455
1231.97 -92.9314
我期待的顶级组坐标相匹配的底部。
谢谢。这很简单,但让我陷入了一个小时的困惑。 – chasep255