CS50x 模糊 - 反馈

CS50x Blur - feedback

我目前正在寻求有关我的代码的反馈。我目前正在尝试模糊我目前正在学习的课程提供的图像。 我已经能够遍历每个独立的像素,如果它超出范围,则跳过它并继续进行第二次和第四次迭代。 我目前正在寻求有关我做错了什么的指导,以便继续修复它。

您还会找到终端的附件,其中包含我不断收到的错误。 Photo

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int redAvg = 0, greenAvg = 0, blueAvg = 0, count = 0;

    for( int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            for (int pl = i - 1; pl <= i + 1; pl++)
            {
                for (int pw = i - 1; pw <= j + 1; pw++)
                {
                    redAvg += image[i][j].rgbtRed;
                    blueAvg += image[i][j].rgbtGreen;
                    greenAvg += image[i][j].rgbtBlue;
                    count++;

                    if (pl < 0)
                    {
                        continue;
                    }
                    if (pw < 0)
                    {
                        continue;
                    }
                }
            }
            redAvg /= count;
            blueAvg /= count;
            greenAvg /= count;

            if (redAvg > 255)
            {
                redAvg = 255;
            }
            if (blueAvg > 255)
            {
                blueAvg = 255;
            }
            if (greenAvg > 255)
            {
                greenAvg = 255;
            }
        }
    }

您在循环中使用了错误的索引并且没有在每次迭代中重置变量。

for( int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
        // Reset for current pixel
        redAvg = blueAvg = greenAvg = 0;
        count = 0;
        for (int pl = i - 1; pl <= i + 1; pl++) {
            for (int pw = j - 1; pw <= j + 1; pw++) {
                // Ensure not out of bounds
                if (pl >= 0 && pl < height && pw >= 0 && pw < width) {
                    redAvg += image[pl][pw].rgbtRed;
                    blueAvg += image[pl][pw].rgbtGreen;
                    greenAvg += image[pl][pw].rgbtBlue;
                    count++;
                }
            }
        }
        // Set the pixel - assuming no pixel value in original image > 255
        image[i][j].rgbtRed = redAvg / count;
        image[i][j].rgbtGreen = greenAvg / count;
        image[i][j].rgbtBlue = blueAvg / count;
    }
}