我正在做一个照片拼接应用程序,一个简单的解决方案是扫描通过位图,将位图分割成小方块,并用小图像替换每个方块。但为了提高结果图像的质量,我希望从中心而不是从左上角扫描位图。有没有什么现有的算法可以解决这个问题?
例如:
在传统方法中,我们从上到下扫描2-D阵列:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16但我想从中心到边界螺旋扫描:
16 15 14 13
5 4 3 12
6 1 2 11
7 8 9 10发布于 2012-11-22 22:10:51
bool between(int x, int low, int high) {
return low <= x && x <= high;
}
// we use this constant array to help tweaking the (row,col) coordinate
const int D[4][2] = {
{0, 1}, // 0 - right
{1, 0}, // 1 - down
{0, -1}, // 2 - left
{-1, 0} // 3 - up
};
int a[n][n]; // suppose the array is n times n in size
int row = 0, col = 0, dir = 0; // initial direction is "0 - right"
for (int m = n*n; m >= 1; m--) {
a[row][col] = m;
int old_row = row, old_col = col; // remember current coordinate
row += D[dir][0];
col += D[dir][1];
if (!(between(row,0,n-1) && between(col,0,n-1))) { // have to move back
// move back
row = old_row;
col = old_col;
// change direction
dir++;
dir %= 4;
// move again
row += D[dir][0];
col += D[dir][1];
}
}发布于 2012-11-22 18:32:20
解决这个问题的一种可能是,考虑向后绘制螺旋。
从(0,0)点开始,然后转到(0,y) -> (x,y) -> (x,0) -> (1,0)。剩下的是一个更小的矩形。只要剩余部分的高度/宽度大于2,就可以这样做。
现在您有了一个大小为(x,2)或(2,y)的矩形,它是开始绘制的中心矩形。为简单起见,我假设您有一个大小为(x,2)的矩形。你从左下角开始。向右绘制x步,然后向上绘制1步。然后你增加你的台阶的宽度或高度。
现在的问题是,如何获得大小为(x,2)的第一个矩形?假设你有一个大小为(w,h)的图片,使用w > h,然后你的第一个矩形是(w-h+2,2),开始的坐标是(w/2-(w-h+2)/2,h/2)。
示例:给定一个矩形w=8,h=4。中心矩形是w=6,h=2。从位置(1,2)开始。
画图将是:6向右,1向上,6向左,2向下,7向右,3向上,7向左,完成。
https://stackoverflow.com/questions/13509489
复制相似问题