简体   繁体   English

为什么我的 CS50 过滤器边缘代码不适用于 check50?

[英]why is my CS50 filter edges code not working with check50?

My cs50 filter edges function is not working, it compiles ok but when i run check50 the first test (edges correctly filters middle pixel) us correct while the others are incorrect just by the last value, like this:我的 cs50 过滤器边缘 function 不工作,它编译正常,但是当我运行 check50 时,第一个测试(边缘正确过滤中间像素)我们是正确的,而其他的只是最后一个值不正确,如下所示:

:( edges correctly filters pixel on edge expected "213 228 255\n", not "213 228 140\n" :( 边缘正确过滤边缘上的像素,预期为“213 228 255\n”,而不是“213 228 140\n”

However, when I print the gx and gy for the red, green and blue alone, and the value of the squareroot, none of the values for the colors match.但是,当我单独打印红色、绿色和蓝色的 gx 和 gy 以及平方根的值时,colors 的值都不匹配。

now, this is my code for edges现在,这是我的边缘代码

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    int sr = 0;
    int sb = 0;
    int sg = 0;
    int yr = 0;
    int yb = 0;
    int yg = 0;
    struct RGBTRIPle
    {
        int rgbtRed;
        int rgbtGreen;
        int rgbtBlue;
    };
    struct RGBTRIPle copia[height][width];
    struct RGBTRIPLe
    {
        int rgbtRed;
        int rgbtGreen;
        int rgbtBlue;
    };
    struct RGBTRIPLe copia2[height][width];
    //Implementing Gx
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            sr = 0;
            sb = 0;
            sg = 0;
            for (int m = i - 1; m <= i + 1; m++)
            {
                for (int c = j - 1; c <= j + 1; c++)
                {

                    if (m >= 0 && m < height && c >= 0 && c < width)
                    {

                        if (c == j - 1)
                        {
                            if (m == i - 1 || m == i + 1)
                            {
                                sr += -1 * image[m][c].rgbtRed;
                                sb += -1 * image[m][c].rgbtBlue;
                                sg += -1 * image[m][c].rgbtGreen;
                            }
                            else
                            {
                                sr += -2 * image[m][c].rgbtRed;
                                sb += -2 * image[m][c].rgbtBlue;
                                sg += -2 * image[m][c].rgbtGreen;
                            }

                        }
                        if (c == j + 1)
                        {
                            if (m == i - 1 || m == i + 1)
                            {
                                sr += image[m][c].rgbtRed;
                                sb += image[m][c].rgbtBlue;
                                sg += image[m][c].rgbtGreen;
                            }
                            else
                            {
                                sr += 2 * image[m][c].rgbtRed;
                                sb += 2 * image[m][c].rgbtBlue;
                                sg += 2 * image[m][c].rgbtGreen;
                            }

                        }
                        else //c = j
                        {
                            sr += 0 * image[m][c].rgbtRed;
                            sb += 0 * image[m][c].rgbtBlue;
                            sg += 0 * image[m][c].rgbtGreen;
                        }

                    }
                }
            }
            copia[i][j].rgbtRed = sr;
            copia[i][j].rgbtGreen = sg;
            copia[i][j].rgbtBlue = sb;
        }

    }
    //Implementing Gy
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            yr = 0;
            yb = 0;
            yg = 0;
            for (int m = i - 1; m <= i + 1; m++)
            {
                for (int c = j - 1; c <= j + 1; c++)
                {

                    if (m >= 0 && m < height && c >= 0 && c < width)
                    {

                        if (m == i - 1)
                        {
                            if (c == j - 1 || c == j + 1)
                            {
                                yr += -1 * image[m][c].rgbtRed;
                                yb += -1 * image[m][c].rgbtBlue;
                                yg += -1 * image[m][c].rgbtGreen;
                            }
                            else
                            {
                                yr += -2 * image[m][c].rgbtRed;
                                yb += -2 * image[m][c].rgbtBlue;
                                yg += -2 * image[m][c].rgbtGreen;
                            }

                        }
                        if (m == i + 1)
                        {
                            if (c == j + 1 || c == j - 1)
                            {
                                yr += image[m][c].rgbtRed;
                                yb += image[m][c].rgbtBlue;
                                yg += image[m][c].rgbtGreen;
                            }
                            else
                            {
                                yr += 2 * image[m][c].rgbtRed;
                                yb += 2 * image[m][c].rgbtBlue;
                                yg += 2 * image[m][c].rgbtGreen;
                            }

                        }
                        else //c = j
                        {
                            yr += 0 * image[m][c].rgbtRed;
                            yb += 0 * image[m][c].rgbtBlue;
                            yg += 0 * image[m][c].rgbtGreen;
                        }
                    }
                }
            }
            copia2[i][j].rgbtRed = yr;
            copia2[i][j].rgbtGreen = yg;
            copia2[i][j].rgbtBlue = yb;
        }
    }
    //Implementing math operation to calculate resulting color
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int r = 0;
            int g = 0;
            int b = 0;
            image[i][j].rgbtRed = (int) round(sqrt((copia[i][j].rgbtRed * copia[i][j].rgbtRed) + (copia2[i][j].rgbtRed *
                                                   copia2[i][j].rgbtRed)));
            image[i][j].rgbtGreen = (int) round(sqrt((copia[i][j].rgbtGreen * copia[i][j].rgbtGreen) + (copia2[i][j].rgbtGreen *
                                                copia2[i][j].rgbtGreen)));
            image[i][j].rgbtBlue = (int) round(sqrt((copia[i][j].rgbtBlue * copia[i][j].rgbtBlue) + (copia2[i][j].rgbtBlue *
                                                    copia2[i][j].rgbtBlue)));
            r = image[i][j].rgbtRed;
            g = image[i][j].rgbtGreen;
            b = image[i][j].rgbtBlue;

            if (image[i][j].rgbtRed > 255)
            {
                image[i][j].rgbtRed = 255;
            }
            if (image[i][j].rgbtGreen > 255)
            {
                image[i][j].rgbtGreen = 255;
            }
            if (image[i][j].rgbtBlue > 255)
            {
                image[i][j].rgbtBlue = 255;
            }
        }
    }
    return;
}

The problem you describe arises when you store round(sqrt((copia[i][j].rgbtRed * copia[i][j].rgbtRed) + (copia2[i][j].rgbtRed *copia2[i][j].rgbtRed)));当您存储round(sqrt((copia[i][j].rgbtRed * copia[i][j].rgbtRed) + (copia2[i][j].rgbtRed *copia2[i][j].rgbtRed))); into the variable image[i][j].rgbtRed (or any other variant thereof).进入变量image[i][j].rgbtRed (或其任何其他变体)。 This is because when calculating sqrt(gx^2 + gy^2) you are getting a number above 255. For example, you may get the integer value 395 after rounding.这是因为在计算 sqrt(gx^2 + gy^2) 时,您得到的数字大于 255。例如,四舍五入后您可能会得到 integer 值 395。 To store that value in to image[i][j].rgbtRed , C will store the value of 395 % 255, or 140, because the image cannot store values greater than 255, by definition.为了将该值存储到image[i][j].rgbtRed中,C 将存储 395 % 255 或 140 的值,因为根据定义,图像不能存储大于 255 的值。

This means that your if statements are useless, because the respective color values will never be greater than 255:这意味着您的 if 语句是无用的,因为相应的颜色值永远不会大于 255:

 if (image[i][j].rgbtRed > 255)
 {
     image[i][j].rgbtRed = 255;
 }
 if (image[i][j].rgbtGreen > 255)
 {
     image[i][j].rgbtGreen = 255;
 }
 if (image[i][j].rgbtBlue > 255)
 {
     image[i][j].rgbtBlue = 255;
 }

To solve this problem you have to cap the value before storing them into the image.要解决这个问题,您必须在将它们存储到图像之前限制值。 A simple implementation of this would be by making a function called cap that returns 255 if an input is above 255.:一个简单的实现是通过创建一个名为 cap 的 function 如果输入高于 255,则返回 255:

int cap(int rgb)
{
    if (rgb > 255)
    {
        return 255;
    }
    else
    {
        return rgb;
    }
}

You can then use this function in the following manner, which will solve your problem completely:然后您可以通过以下方式使用此 function,这将彻底解决您的问题:

image[i][j].rgbtRed =  cap(round(sqrt((copia[i][j].rgbtRed * copia[i][j].rgbtRed) + (copia2[i][j].rgbtRed * copia2[i][j].rgbtRed))));
image[i][j].rgbtGreen = cap(round(sqrt((copia[i][j].rgbtGreen * copia[i][j].rgbtGreen) + (copia2[i][j].rgbtGreen * copia2[i][j].rgbtGreen))));
image[i][j].rgbtBlue = cap(round(sqrt((copia[i][j].rgbtBlue * copia[i][j].rgbtBlue) + (copia2[i][j].rgbtBlue * copia2[i][j].rgbtBlue))));

This will also shorten your code, make it look cleaner, and avoid unnecessary repetition.这也将缩短您的代码,使其看起来更干净,并避免不必要的重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我们不在 Cs50 pset4 滤镜的灰度函数中使用指针? - Why we don't use a pointer in Cs50 pset4 filter's grayscale function? CS50 [一般 - 但与 PSET4 恢复相关] 为什么 clang 说我的标识符未为函数声明? - CS50 [general - but related to PSET4 Recover] Why does clang say my identifiers are undeclared for a function? 目前正在使用 CS50,无法理解为什么会这样? - Currently take CS50, having trouble understanding why this works? Tideman 中的投票功能(CS50 的 pset 3) - Vote function in Tideman (pset 3 of CS50) C - 在函数中使用未声明的标识符。 CS50 - C - use of undeclared identifier in a function. CS50 RUNOFF PSET3 - bool is_tie(int min) 函数在 Check50 中出现错误无法删除 - RUNOFF PSET3 - bool is_tie(int min) function gives an error in Check50 unable to remove CS50 PSET6:解析功能不会更改指针内容 - CS50 PSET6: Parse function doesn't change pointers content 为什么我的 function 只返回 R 中长度为 930-50 的向量? - Why my function is only returning a vector of length 930-50 in R? 为什么我的过滤器无法正常工作? - Why is my filter not working as expected? 如何将 50 行 50 列的二维数组编码到一个函数中,该函数为元素随机分配一个星号? - How can I code a two-dimensional array of 50 rows and 50 columns into a function which randomly assigns an asterisk to an element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM