cs50 PSET4 - 过滤我的代码中的疑问和问题

发布于 2025-01-15 21:57:07 字数 3468 浏览 3 评论 0原文

在 CS50 的 PSET4 上工作了一段时间,它需要创建四个函数,对输入图像应用各种效果。灰度、棕褐色、反射和模糊。

当我第一次完成代码时,当我检查它时,几乎没有任何代码可以工作。我通过基本上无意识地添加一堆变量来解决其中的大部分问题,这些变量由于某种原因解决了我的许多问题。

有人可以解释为什么原始代码不起作用并且仅实现了近似值吗?

这是原来的,然后更新的:

void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int x = 0; x < height; x++) {
        for (int y = 0; y < width; y++) {
            BYTE avg = round((image[x][y].rgbtBlue + image[x][y].rgbtRed + image[x][y].rgbtGreen) / 3);
            image[x][y].rgbtBlue = avg;
            image[x][y].rgbtRed = avg;
            image[x][y].rgbtGreen = avg;
            }
    }
    return;
    }
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for (int x = 0; x < height; x++) {
        for (int y = 0; y < width; y++) {
                BYTE originalRed = image[x][y].rgbtRed;
                BYTE originalGreen = image[x][y].rgbtGreen;
                BYTE originalBlue = image[x][y].rgbtBlue;
    
                image[x][y].rgbtRed = .393 * originalRed + .769 * originalGreen + .189 * originalBlue;
                image[x][y].rgbtGreen = .349 * originalRed + .686 * originalGreen + .168 * originalBlue;
                image[x][y].rgbtBlue = .272 * originalRed + .534 * originalGreen + .131 * originalBlue;
                if (image[x][y].rgbtRed > 255) {
                    image[x][y].rgbtRed = 255;
                }
                if (image[x][y].rgbtGreen > 255) {
                    image[x][y].rgbtGreen = 255;
                }
                if (image[x][y].rgbtBlue > 255) {
                    image[x][y].rgbtBlue = 255;
                }
            }
         }
        return;
    }

void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int x = 0; x < height; x++) {
        for (int y = 0; y < width; y++) {
            int red = image[x][y].rgbtRed;
            int green = image[x][y].rgbtGreen;
            int blue = image[x][y].rgbtBlue;

            int avg = round(((float)red + (float)green + (float)blue) / 3);
            image[x][y].rgbtBlue = avg;
            image[x][y].rgbtRed = avg;
            image[x][y].rgbtGreen = avg;
            }
    }
    return;
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for (int x = 0; x < height; x++) {
        for (int y = 0; y < width; y++) {
            int originalRed = image[x][y].rgbtRed;
            int originalGreen = image[x][y].rgbtGreen;
            int originalBlue = image[x][y].rgbtBlue;

            int sepiaRed = round(.393 * originalRed + .769 * originalGreen + .189 * originalBlue);
            int sepiaGreen = round(.349 * originalRed + .686 * originalGreen + .168 * originalBlue);
            int sepiaBlue = round(.272 * originalRed + .534 * originalGreen + .131 * originalBlue);

            if (sepiaRed > 255) {
                image[x][y].rgbtRed = 255;
            }
            else{
                image[x][y].rgbtRed = sepiaRed;
            }
            if (sepiaGreen > 255) {
                image[x][y].rgbtGreen = 255;
            }
            else{
                image[x][y].rgbtGreen = sepiaGreen;
            }
            if (sepiaBlue > 255) {
                image[x][y].rgbtBlue = 255;
            }
            else{
                image[x][y].rgbtBlue = sepiaBlue;
            }
        }
     }
    return;
}

Have been working for some time on the PSET4 of CS50, it requires one to create four functions that apply various effects to an input image. grayscale, sepia, reflect, and blur.

When I first finished the code, barely any of it worked when I checked it. Much of it I fixed through essentially mindlessly adding a bunch of variables which for some reason had fixed many of my problems.

Could someone explain why the original code didn't work and achieved mere approximations?

Here is the original and then updated:

void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int x = 0; x < height; x++) {
        for (int y = 0; y < width; y++) {
            BYTE avg = round((image[x][y].rgbtBlue + image[x][y].rgbtRed + image[x][y].rgbtGreen) / 3);
            image[x][y].rgbtBlue = avg;
            image[x][y].rgbtRed = avg;
            image[x][y].rgbtGreen = avg;
            }
    }
    return;
    }
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for (int x = 0; x < height; x++) {
        for (int y = 0; y < width; y++) {
                BYTE originalRed = image[x][y].rgbtRed;
                BYTE originalGreen = image[x][y].rgbtGreen;
                BYTE originalBlue = image[x][y].rgbtBlue;
    
                image[x][y].rgbtRed = .393 * originalRed + .769 * originalGreen + .189 * originalBlue;
                image[x][y].rgbtGreen = .349 * originalRed + .686 * originalGreen + .168 * originalBlue;
                image[x][y].rgbtBlue = .272 * originalRed + .534 * originalGreen + .131 * originalBlue;
                if (image[x][y].rgbtRed > 255) {
                    image[x][y].rgbtRed = 255;
                }
                if (image[x][y].rgbtGreen > 255) {
                    image[x][y].rgbtGreen = 255;
                }
                if (image[x][y].rgbtBlue > 255) {
                    image[x][y].rgbtBlue = 255;
                }
            }
         }
        return;
    }

void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int x = 0; x < height; x++) {
        for (int y = 0; y < width; y++) {
            int red = image[x][y].rgbtRed;
            int green = image[x][y].rgbtGreen;
            int blue = image[x][y].rgbtBlue;

            int avg = round(((float)red + (float)green + (float)blue) / 3);
            image[x][y].rgbtBlue = avg;
            image[x][y].rgbtRed = avg;
            image[x][y].rgbtGreen = avg;
            }
    }
    return;
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for (int x = 0; x < height; x++) {
        for (int y = 0; y < width; y++) {
            int originalRed = image[x][y].rgbtRed;
            int originalGreen = image[x][y].rgbtGreen;
            int originalBlue = image[x][y].rgbtBlue;

            int sepiaRed = round(.393 * originalRed + .769 * originalGreen + .189 * originalBlue);
            int sepiaGreen = round(.349 * originalRed + .686 * originalGreen + .168 * originalBlue);
            int sepiaBlue = round(.272 * originalRed + .534 * originalGreen + .131 * originalBlue);

            if (sepiaRed > 255) {
                image[x][y].rgbtRed = 255;
            }
            else{
                image[x][y].rgbtRed = sepiaRed;
            }
            if (sepiaGreen > 255) {
                image[x][y].rgbtGreen = 255;
            }
            else{
                image[x][y].rgbtGreen = sepiaGreen;
            }
            if (sepiaBlue > 255) {
                image[x][y].rgbtBlue = 255;
            }
            else{
                image[x][y].rgbtBlue = sepiaBlue;
            }
        }
     }
    return;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文