我的 Edges (pset4) CS50 代码仅返回黑屏

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

这是我当前的代码,它现在返回一个主要的白色图像。 我添加了一个临时变量来存储图像中的更改并将其附加到pixel_with_edges。我将这个传递给新图像[行][列] 我还纠正了循环以及与颜色总和相关的循环

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE pixel_with_edges[height][width];
    RGBTRIPLE temporary[height][width];

    for (int row = 0; row < height; row++)
    {
        for (int column = 0; column < width; column++)
        {
            temporary[row][column] = image[row][column];
            pixel_with_edges[row][column] = determineEdges(row, column, height, width, image, temporary);
            image[row][column] = pixel_with_edges[row][column];
        }
    }
}




//i and j are the current pixels
RGBTRIPLE determineEdges (int i, int j, int height, int width, RGBTRIPLE image[height][width], RGBTRIPLE temp[height][width])
{

    float sum_surround_redX = 0, sum_surround_greenX = 0, sum_surround_blueX = 0, sum_surround_redY = 0, sum_surround_greenY = 0, sum_surround_blueY = 0;

    int Gx[3][3] = {
         {-1, 0, 1},
         {-2, 0, 2},
         {-1, 0, 1}
         };
    int Gy [3][3]= {
         {-1, -2, -1},
         {0,   0,  0},
         {1,   2,  1}
         };

    //ident throughout the circundant pixels
    for (int row_idx =  i - 1; row_idx <= i + 1; row_idx++)
    {
        for (int column_idx = j - 1; column_idx <= j + 1; column_idx++)
        {

            //checks for outta boundaries pixels, if so, skips. That avoid segmentation fault
            //*****establishes 0 (black) to the pixels outta boundaries next to the current pixel
            if (row_idx < 0 || column_idx < 0 || row_idx >= height || column_idx >= width)
            {
                continue;
            }


            //sums the RGB surrounding pixels according to the leverage established in Gx and Gy
            sum_surround_redX += Gx[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtRed;
            sum_surround_greenX += Gx[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtGreen;
            sum_surround_blueX += Gx[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtBlue;


            sum_surround_redY += Gy[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtRed;
            sum_surround_greenY += Gy[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtGreen;
            sum_surround_blueY += Gy[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtBlue;
        }
            }
            
        //assigns a new value for the pixels RGB according to square of Gx^2 + Gy^2
        float newRed = round(sqrt (pow(sum_surround_redX, 2) + pow(sum_surround_redY, 2)));
        float newGreen = round(sqrt (pow(sum_surround_greenX, 2) + pow(sum_surround_greenY, 2)));
        float newBlue = round(sqrt (pow(sum_surround_blueX, 2) + pow(sum_surround_blueY, 2)));

                //garantees that no color has higher assigns than 255, and attach the new pixels values of RGB to a RGBTRIPLE struct
                if(newRed > 255)
                {
                    newRed = 255;
                }
                if(newGreen > 255)
                {
                    newGreen = 255;
                }
                if(newBlue > 255)
                {
                    newBlue = 255;
                }

                temp[i][j].rgbtRed = newRed;
                temp[i][j].rgbtGreen = newGreen;
                temp[i][j].rgbtBlue = newBlue;

    return temp[i][j];

}

这是我当前的代码,它现在返回一个主要的白色图像。 我添加了一个临时变量来存储图像中的更改并将其附加到pixel_with_edges。我将这个传递给新图像[行][列] 我还纠正了循环以及与颜色总和相关的循环

c image debugging cs50
1个回答
0
投票

您的代码中存在多个问题。

您似乎明白必须制作图像的临时副本来存储结果,以避免弄乱原始图像。

正如评论中已经提到的,您忘记了事后实际使用该副本。

pixel_with_edges
的目的是什么?离开该功能后它就会消失。

您还忘记了代码中途不应该弄乱原始图像。您将一些中间结果存储在

image[i][j]
中,这会在处理所有数据之前更改数据。这会影响以下像素。

此外,循环完全关闭。 您的函数

determineEdges
应该计算一个像素并将其返回给调用者。 相反,您将循环遍历所有相邻像素,并为每个像素再次循环遍历相同的 9 个像素。 这 4 个循环应该只是 2 个循环。 您应该在这些循环内计算结果,但只能在之后计算。 这个结果不应该存储在数组中,而应该存储在可以返回的结构中。

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