需要一些关于cs50滤镜灰度的初步指导。

问题描述 投票:0回答:1

我想解决滤镜灰度的问题,在开始之前我很苦恼。

对于灰度,我们得到的公式是将RGB的值平均,然后用这个值作为每个像素的新RGB值。我如何在平均后更新像素中的值?

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

还有,当我不知道除了 "高度-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));
     }
}
filter cs50 grayscale
1个回答
0
投票
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.