1

my code is running well on a dedicated hardcoded version of check50 edges middle pixel, but as soon as I use it for the check50 it gives an error. Debug50 did calculate exactly what it should have done. Any idea?

Code in Helpers.c

// Generating temp image
RGBTRIPLE tmp_image[height][width];

// Creating the arrays for the Sobel Operator
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 }
};

// Declares counter for the positive and negative integers for the final operation
RGBTRIPLE x_count;
RGBTRIPLE y_count;

// Going thru pixel by pixel
for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {

        // Clearing all counts
        x_count.rgbtRed = 0;
        x_count.rgbtGreen = 0;
        x_count.rgbtBlue = 0;
        y_count.rgbtRed = 0;
        y_count.rgbtGreen = 0;
        y_count.rgbtBlue = 0;

        // Checking the 3x3 dimension
        for (int x = -1; x < 2; x++)
        {
            for (int y = -1; y < 2; y++)
            {

                if ((i + y) < 0 || (i + y) == height || (j + x) < 0 || (j + x) == width)
                {
                    continue;
                }

                // Adding 3x3 array to x/y_count
                x_count.rgbtRed += image[i + y][j + x].rgbtRed * Gx[y + 1][x + 1];
                x_count.rgbtGreen += image[i + y][j + x].rgbtGreen * Gx[y + 1][x + 1];
                x_count.rgbtBlue += image[i + y][j + x].rgbtBlue * Gx[y + 1][x + 1];

                y_count.rgbtRed += image[i + y][j + x].rgbtRed * Gy[y + 1][x + 1];
                y_count.rgbtGreen += image[i + y][j + x].rgbtGreen * Gy[y + 1][x + 1];
                y_count.rgbtBlue += image[i + y][j + x].rgbtBlue * Gy[y + 1][x + 1];

            }
        }

        // Sobel Operator on temp Image
        tmp_image[i][j].rgbtRed = round(fmin(255, sqrt((x_count.rgbtRed * x_count.rgbtRed) + (y_count.rgbtRed * y_count.rgbtRed))));
        tmp_image[i][j].rgbtGreen = round(fmin(255, sqrt((x_count.rgbtGreen * x_count.rgbtGreen) + (y_count.rgbtGreen * y_count.rgbtGreen))));
        tmp_image[i][j].rgbtBlue = round(fmin(255, sqrt((x_count.rgbtBlue * x_count.rgbtBlue) + (y_count.rgbtBlue * y_count.rgbtBlue))));


    }
}

// Copying the TempPicture to the original

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        image[i][j] = tmp_image[i][j];
    }
}

return;

Code in test file

#include <math.h>
#include <stdio.h>

typedef struct
{
    int rgbtBlue;
    int rgbtGreen;
    int rgbtRed;
}
RGBTRIPLE;

int main(void)
{

    int height = 3;
    int width = 3;
    RGBTRIPLE image[height][width];

    image[0][0].rgbtRed = 25;
    image[0][0].rgbtGreen = 10;
    image[0][0].rgbtBlue = 0;

    image[0][1].rgbtRed = 30;
    image[0][1].rgbtGreen = 10;
    image[0][1].rgbtBlue = 0;

    image[0][2].rgbtRed = 80;
    image[0][2].rgbtGreen = 60;
    image[0][2].rgbtBlue = 40;

    image[1][0].rgbtRed = 90;
    image[1][0].rgbtGreen = 30;
    image[1][0].rgbtBlue = 20;

    image[1][1].rgbtRed = 100;
    image[1][1].rgbtGreen = 40;
    image[1][1].rgbtBlue = 30;

    image[1][2].rgbtRed = 90;
    image[1][2].rgbtGreen = 70;
    image[1][2].rgbtBlue = 80;

    image[2][0].rgbtRed = 40;
    image[2][0].rgbtGreen = 20;
    image[2][0].rgbtBlue = 20;

    image[2][1].rgbtRed = 30;
    image[2][1].rgbtGreen = 10;
    image[2][1].rgbtBlue = 30;

    image[2][2].rgbtRed = 10;
    image[2][2].rgbtGreen = 40;
    image[2][2].rgbtBlue = 50;

    // Generating temporare image
    RGBTRIPLE tmp_image[height][width];

    // Creating the arrays for the Sobel Operator
    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 }
    };

    // Declares counter for the positive and negative integers for the final operation
    RGBTRIPLE x_count;
    RGBTRIPLE y_count;

    // Going thru pixel by pixel
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {

            // Clearing all counts
            x_count.rgbtRed = 0;
            x_count.rgbtGreen = 0;
            x_count.rgbtBlue = 0;
            y_count.rgbtRed = 0;
            y_count.rgbtGreen = 0;
            y_count.rgbtBlue = 0;

            // Checking the 3x3 dimension
            for (int x = -1; x < 2; x++)
            {
                for (int y = -1; y < 2; y++)
                {

                    if ((i + y) < 0 || (i + y) == height || (j + x) < 0 || (j + x) == width)
                    {
                        continue;
                    }

                    // Adding 3x3 array to x/y_count
                    x_count.rgbtRed += image[i + y][j + x].rgbtRed * Gx[y + 1][x + 1];
                    x_count.rgbtGreen += image[i + y][j + x].rgbtGreen * Gx[y + 1][x + 1];
                    x_count.rgbtBlue += image[i + y][j + x].rgbtBlue * Gx[y + 1][x + 1];

                    y_count.rgbtRed += image[i + y][j + x].rgbtRed * Gy[y + 1][x + 1];
                    y_count.rgbtGreen += image[i + y][j + x].rgbtGreen * Gy[y + 1][x + 1];
                    y_count.rgbtBlue += image[i + y][j + x].rgbtBlue * Gy[y + 1][x + 1];

                }
            }

            // Sobel Operator on temp Image
            tmp_image[i][j].rgbtRed = round(fmin(255, sqrt((x_count.rgbtRed * x_count.rgbtRed) + (y_count.rgbtRed * y_count.rgbtRed))));
            tmp_image[i][j].rgbtGreen = round(fmin(255, sqrt((x_count.rgbtGreen * x_count.rgbtGreen) + (y_count.rgbtGreen * y_count.rgbtGreen))));
            tmp_image[i][j].rgbtBlue = round(fmin(255, sqrt((x_count.rgbtBlue * x_count.rgbtBlue) + (y_count.rgbtBlue * y_count.rgbtBlue))));


        }
    }

    // Copying the TempPicture to the original

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j] = tmp_image[i][j];
        }
    }

    return 1;
}

Result of the test code was exactly as expected. But not at check50. What's going on?

Middle pixel from hardcode:

{rgbtBlue = 210, rgbtGreen = 150, rgbtRed = 60}

enter image description here

1

Found. RGBTRIPBLE doesn't support Float.

3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .