0

My images are mostly white. My code returns 255 for RGB without resetting. So either my int is wrong or my loops are wrong but I can't see it.

Here is my logic:

edges function loops through every pixel and runs the edgePixels function. If it's a validpixel, meaning it's not on the corner or edges then it goes through the Sobel Operator. Otherwise, it's just 0.

My edgePixels function doesn't seem to reset the Gx/Gy values once the function returns. And it doesn't seem to go back to the edges function to run the next pixel. Or at least that's what happens when I try to debug it.

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

bool validPixel(int i, int j, int height, int width)
{

    return i >= 0 && i < height && j >= 0 && j < width;
}

// Detect edges
RGBTRIPLE edgePixels(int i, int j, int height, int width, RGBTRIPLE image[height][width])
{
    // Creating sobel operator arrays
    int sobel_x[3][3] = {{-1,0,1},{-2,0,2},{-1,0,1}};
    int sobel_y[3][3] = {{-1,-2,-1},{0,0,0},{1,2,1}};

    // Creating Gx variables
    int GxRed = 0;
    int GxGreen = 0;
    int GxBlue = 0;

    // Creating Gy variables
    int GyRed = 0;
    int GyGreen = 0;
    int GyBlue = 0;

    int GxGyRed = 0;
    int GxGyGreen = 0;
    int GxGyBlue = 0;

    // loop through each pixel around the pixel
    for (int di = -1; di <= 1; di++)
    {
        for (int dj = -1; dj <= 1; dj++)
        {
            int new_i = i + di;
            int new_j = j + dj;

            // loop through sobel operator
            if (validPixel(new_i, new_j, height, width))
            {
                for (int sx = 0; sx < 2; sx++)
                {
                    for (int sy = 0; sy < 2; sy++)
                    {
                        {
                            GxRed += image[new_i][new_j].rgbtRed * sobel_x[sx][sy];
                            GxGreen += image[new_i][new_j].rgbtGreen * sobel_x[sx][sy];
                            GxBlue += image[new_i][new_j].rgbtBlue * sobel_x[sx][sy];

                            GyRed += image[new_i][new_j].rgbtRed * sobel_y[sx][sy];
                            GyGreen += image[new_i][new_j].rgbtGreen * sobel_y[sx][sy];
                            GyBlue += image[new_i][new_j].rgbtBlue * sobel_y[sx][sy];
                        }
                    }
                }
            }
            else if (!validPixel(new_i, new_j, height, width))
            {
                GxRed += 0;
                GxGreen += 0;
                GxBlue += 0;

                GyRed += 0;
                GyGreen += 0;
                GyBlue += 0;
            }

            GxGyRed = round(sqrtf(pow(GxRed, 2) + pow(GyRed, 2)));
            GxGyRed = (GxGyRed > 255 ? 255 : GxGyRed);

            GxGyGreen = round(sqrtf(pow(GxGreen, 2) + pow(GyGreen, 2)));
            GxGyGreen = (GxGyGreen > 255 ? 255 : GxGyGreen);

            GxGyBlue = round(sqrtf(pow(GxBlue, 2) + pow(GyBlue, 2)));
            GxGyBlue = (GxGyBlue > 255 ? 255 : GxGyBlue);
        }
    }
    RGBTRIPLE edgedPixels;
    edgedPixels.rgbtRed = GxGyRed;
    edgedPixels.rgbtGreen = GxGyGreen;
    edgedPixels.rgbtBlue = GxGyBlue;

    return edgedPixels;
}


void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copyImageEdge[height][width];

    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            copyImageEdge[h][w] = edgePixels(h, w, height, width, image);
        }
    }

    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            image[h][w] = copyImageEdge[h][w];
        }
    }
}

You must log in to answer this question.

Browse other questions tagged .