CS50 PSET4过滤器交换结构

时间:2020-02-05 16:33:50

标签: c struct cs50

在做滤镜的反射部分时,我遇到了一些困难。本质上,该结构是

File "<ipython-input-75-8d5b35da10c9>", line 1
    confidences = [{c: p for ((c, p*100)) in list(zip(classes, probs))} for probs in preds]
                            ^
SyntaxError: can't assign to operator

并且我一直在尝试通过实现此功能来反映图像。

typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE; 

代码可以正常编译,但最终产品与输入图像相同。试图运行debug50,我发现我的问题出在我交换RGBTRIPLE值的方式上。任何帮助都会很好。谢谢!

1 个答案:

答案 0 :(得分:2)

您需要做的是反转阵列。

为什么?因为您正在水平反射图像,所以您希望图像的左侧成为图像的右侧。

想象一下,您有这个数组,并且想要反转它:

int count = 5;
int numbers[count] = {0, 1, 2, 3, 4};

// middle here should be 2.5 but it will be 2 because we cast it to int
int middle = count / 2;
// Reverse array
for (int i = 0; i < middle; i++)
{
    // when i is 0, numbers[i] is 0, numbers[count - 1 - i] is 4
    temp = numbers[i];
    numbers[i] = numbers[count - i - 1];
    numbers[count - i - 1] = temp;
}

您应该在功能上执行相同的操作

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    // The middle index
    int middle = width / 2;
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < middle; j++)
        {
            // Swap the left side element with right side element
            RGBTRIPLE temp = image[i][j];
            image[i][j] = image[i][width - j - 1];
            image[i][width - j - 1] = temp;
        }
    }
    return;
}