首页 > 解决方案 > 需要cs50滤镜灰度的一些初始方向

问题描述

我正在尝试解决过滤器灰度的问题,并且在开始之前我正在苦苦挣扎。

对于灰度,我们给出的公式是平均 RGB 的值,然后将该值用作每个像素的新 RGB 值。平均后如何更新像素中的值?

我在想我可以创建一个 Replace 的布尔值并将其设置为 false,然后在替换后将其设置为 true,但现在我认为我忽略了一些更简单的东西。

当我不知道除了'height -1'行之外还有多少行时,我如何将图像视为一个数组。

作业视频将示例像素格式描述为:

image[2][3].rgtbBlue
image[2][3].rgbtGreen
image[2][3].rgbtRed

那么这是否意味着

((BYTE.rgbtBlue + BYTE.rgbtGreen + BYTE.rgbtRed) /3)

无法获得平均值?

这行得通吗?

for (int i = 0; i < height -1; i ++)
{
   for (int j =0; j < height; j++)
    {
        image[i][j].rgbtBlue = round(((BYTE.rgbtBlue+ BYTE.rgbtGreen + BYTE.rgbtRed) /3));
        image[i][j].rgbtGreen = round(((BYTE.rgbtBlue+ BYTE.rgbtGreen + BYTE.rgbtRed) /3));
        image[i][j].rgbtRed = round(((BYTE.rgbtBlue+ BYTE.rgbtGreen + BYTE.rgbtRed) /3));
     }
}

标签: cfiltercs50grayscale

解决方案


void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
// Locate the pixel
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
// Find the average pixel color
            float avgpixel = round((image[j][i].rgbtBlue + image[j][i].rgbtGreen + image[j][i].rgbtRed) / 3.00);
// Replace the grayscale pixel to all colors
            image[j][i].rgbtBlue = avgpixel;
            image[j][i].rgbtGreen = avgpixel;
            image[j][i].rgbtRed = avgpixel;
        }
    }
    return;
}

推荐阅读