【发布时间】:2010-10-16 09:36:00
【问题描述】:
尝试 1 和 2:
注意:删除了减少问题大小的首次尝试。有关以前的尝试,请参阅社区 wiki。
尝试 3:
根据模糊华夫饼的示例,我已经实现了以下内容,但似乎无法正常工作。有什么想法我可能做错了吗?
ImageMatrix ImageMatrix::GetRotatedCopy(VDouble angle)
{
// Copy the specifications of the original.
ImageMatrix &source = *this;
ImageMatrix &target = CreateEmptyCopy();
double centerX = ((double)(source.GetColumnCount()-1)) / 2;
double centerY = ((double)(source.GetRowCount()-1)) / 2;
// Remember: row = y, column = x
for (VUInt32 y = 0; y < source.GetRowCount(); y++)
{
for (VUInt32 x = 0; x < source.GetColumnCount(); x++)
{
double dx = ((double)x) - centerX;
double dy = ((double)y) - centerY;
double newX = cos(angle) * dx - sin(angle) * dy + centerX;
double newY = cos(angle) * dy + sin(angle) * dx + centerY;
int ix = (int)round(newX);
int iy = (int)round(newY);
target[x][y][0] = source[ix][iy][0];
}
}
return target;
}
有了这个原型矩阵...
1 2 1
0 0 0
-1 -2 -1
...prototype.GetRotatedCopy(0)(正确)...
1 2 1
0 0 0
-1 -2 -1
...prototype.GetRotatedCopy(90)(不正确)...
-2 0 0
-2 0 2
0 0 2
...prototype.GetRotatedCopy(180)(不正确 - 但有点合乎逻辑?)...
0 -1 -2
1 0 -1
2 1 0
...prototype.GetRotatedCopy(270)(不正确 - 为什么这与 0 旋转相同?)...
1 2 1
0 0 0
-1 -2 -1
解决办法:
正如 Mark Ransom 所指出的,我应该使用弧度,而不是度数;我已将我的代码调整如下:
ImageMatrix ImageMatrix::GetRotatedCopy(VDouble degrees)
{
// Copy the specifications of the original.
ImageMatrix &source = *this;
ImageMatrix &target = CreateEmptyCopy();
// Convert degree measurement to radians.
double angle = degrees / 57.3;
// ... rest of code as in attempt #3 ...
感谢大家的帮助!
1 2 1
0 0 0
-1 -2 -1
1 2 1
0 0 0
-1 -2 -1
-1 0 1
-2 0 2
-1 0 1
-1 -2 -1
0 0 0
1 2 1
1 0 -1
2 0 -2
1 0 -1
【问题讨论】:
-
您的输出是指 newRow 和 newColumn(不是 targetRow 和 targetColumn)吗?
-
您是否有可能使用度数而不是弧度来表示角度?仔细阅读模糊华夫饼的答案。
-
啊,是的 - 刚刚用方法调用更新了我的问题。我会尝试使用弧度。
-
旁注:最好将 sin(angle) 和 cos(angle) 移出循环,因为它们不会改变。
标签: math image image-processing