错误:8.13802e + 06在'unsigned char'类型的可表示值范围之外]]

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

接近结束,我遇到了我无法解决的问题。也许你们可以之一:

以下代码的编译工作正常,但是当我启动程序时,出现此错误消息:

helpers.c:228:42:运行时错误:8.13802e + 06超出了'unsigned char'类型的可表示值的范围]]

该代码是对图像进行逐块模糊处理的函数,但是最开始的像素[0] [0]不能获得正确的平均值,我不知道为什么会收到该错误消息。

    // Blur image
    void blur(int height, int width, RGBTRIPLE image[height][width])
    {
        int i;
        int j;
        int m;
        int n;
        int averageRed;
        int averageBlue;
        int averageGreen;
        RGBTRIPLE average[height][width];




// For each row of the image...
    for (i = 0; i < height; i++)
    {
        //...take each pixel.
        for (j = 0; j < width; j++)


                //If current height equals 0 AND current width equals 0..   
                if (i == 0 && j == 0)
                {   
                    //..take 2 rows of the picture..
                    for (m = i; m <= i + 1; m++)
                    {   
                        //..and take 2 pixels of each row.
                        for (n = j; n <= j + 1; n++)
                        {
                        //Sum up the rgb-values for each of the 2 pixel of the 2 rows.
                        averageRed = averageRed + image[m][n].rgbtRed;
                        averageGreen = averageGreen + image[m][n].rgbtGreen;                                        
   -> The error line averageBlue = averageBlue + image[m][n].rgbtBlue;
                        }
                    }
                    //Save the average of the values in a separate array after the 2x2 pixel-block
                    average[i][j].rgbtRed = round((float)averageRed / 4);
                    average[i][j].rgbtGreen = round((float)averageGreen / 4);
                    average[i][j].rgbtBlue = round((float)averageBlue / 4);

                    //Set average-variables to 0
                    averageRed = 0;
                    averageGreen = 0;
                    averageBlue = 0;
                }


        //From each row of the image...
        for (i = 0; i < height; i++)
        {
            //...take each pixel..
            for (j = 0; j < width; j++)
            {
                //...and update the original value with the temporary stored value.
                image[i][j].rgbtRed = average[i][j].rgbtRed;
                image[i][j].rgbtGreen = average[i][j].rgbtGreen;
                image[i][j].rgbtBlue = average[i][j].rgbtBlue;
            }
        }

    }

提前感谢您的提示!

接近结束,我遇到了我无法解决的问题。也许您可以其中之一:下列代码的编译工作正常,但是在我启动程序时收到以下错误消息:helpers.c:228:...

c runtime blur cs50
1个回答
0
投票

另一个facepalm-answer:-)..

解决方案非常简单。这只是缺少平均红色/绿色/蓝色变量的初始化。

© www.soinside.com 2019 - 2024. All rights reserved.