【问题标题】:Flipping images from left to right and from top to bottom从左到右和从上到下翻转图像
【发布时间】:2014-12-31 03:54:47
【问题描述】:

我做了两个函数,一个是从左到右翻转图像,另一个是从上到下翻转图像。但是由于某种原因,当我加载图像时,图像没有任何反应。

这是从左到右翻转的代码。

void flip_horizontal( uint8_t array[], 
              unsigned int cols, 
              unsigned int rows )
{
    unsigned int left = 0;
    unsigned int right = cols;
    for(int r = 0; r < rows; r++)
    {
    while(left != right && right > left)
        {
        int temp = array[r * cols+ left];
        array[(r * cols) + left] = array[(r * cols) + cols - right];
        array[(r * cols) + cols - right] = temp;
        right++;
        left++;
        } 
    }
}

这就是上下翻转的代码。

void flip_vertical( uint8_t array[], 
            unsigned int cols, 
            unsigned int rows )
{
    unsigned int top = 0;
    unsigned int bottom = rows;
    for(int r = 0; r < cols; r++)
    {
    while(top != bottom && bottom > top)
        {
        int temp = array[r * rows+ top];
        array[(r * rows) + top] = array[(r * rows) + rows - bottom];
        array[(r * rows) + rows - bottom] = temp;
        bottom++;
        top++;
        } 
    }
}

【问题讨论】:

  • 如果这是一个家庭作业问题,你应该在标签中标记它。
  • 公平点。让我纠正一下;如果这是家庭作业,则应在标题中注明。

标签: c arrays flip


【解决方案1】:

尝试将这些移动到您的 for 循环中:

unsigned int left = 0;
unsigned int right = cols;

unsigned int top = 0;
unsigned int bottom = rows;

否则,您只会翻转第一行/列。

您的索引方式还有一些其他问题,但我不会破坏修复这些问题的乐趣:-)

【讨论】:

    猜你喜欢
    • 2014-05-30
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多