C语言 错误错误:应返回标识符或"(";在我的代码结尾[已关闭]

sbtkgmzw  于 2023-02-07  发布在  其他
关注(0)|答案(1)|浏览(103)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
7小时前关闭。
Improve this question

#include "helpers.h"
#include <math.h>

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int row = 0; row < height; row++)
    {
        for (int col = 0; col < width; col++)
        {
            int avg = round((image[row][col].rgbtRed + image[row][col].rgbtGreen + image[row][col].rgbtBlue) / 3.0);
            image[row][col].rgbtRed = image[row][col].rgbtGreen = image[row][col].rgbtBlue = avg;
        }
    }
    return;
}

不太清楚我错过了什么。帮助非常感谢
尝试创建灰度过滤器

cetgtptt

cetgtptt1#

您的代码可以干净地编译。
但是,您的灰度算法是不正确的。正确的公式是:

gray = 0.299*red + 0.587*green + 0.114*blue

我们可以通过乘以1将其转换为整数,表示为1000/1000:

gray = (299*red + 587*green + 114*blue) / 1000

下面是一个重构的版本:

// gray = 0.299*red + 0.587*green + 0.114*blue

// Convert image to grayscale
void
grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int row = 0; row < height; ++row) {
        RGBTRIPLE *cur = image[row];

        for (int col = 0; col < width; ++col, ++cur) {
            int avg = (299 * cur->rgbtRed);
            avg += (587 * cur->rgbtGreen);
            avg += (114 * cur->rgbtGreen);
            avg /= 1000;
            cur->rgbtRed = cur->rgbtGreen = cur->rgbtBlue = avg;
        }
    }
}

相关问题