因此,我已经完成了我的edges功能,并且无法解决为什么它失败的问题。任何帮助将不胜感激!边缘功能应该用于检测位图中的边缘。 这是我的edge函数:
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Copy of image so that we calculate based on the original values of RGB
RGBTRIPLE copy[height][width];
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
copy[i][j] = image[i][j];
}
}
// Stores RGB values of each pixel in grid
RGBTRIPLE grid[3][3];
// Iterate over every pixel and alter its RGB values
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
reset(grid);
// Includes current pixel in center of 3 x 3 grid
grid[1][1] = copy[i][j];
// Checks if left edge exists and if so, assigns to left edge in 3 x 3 grid
if (j != 0)
{
grid[1][0] = copy[i][j - 1];
}
// Checks if right edge exists
if (j + 1 != width)
{
grid[1][2] = copy[i][j + 1];
}
// Checks if top edge exists
if (i != 0)
{
grid[0][1] = copy[i - 1][j];
}
// Checks if bottom edge exists
if (i + 1 != height)
{
grid[2][1] = copy[i + 1][j];
}
// Checks if top left corner exists
if (i != 0 && j != 0)
{
grid[0][0] = copy[i - 1][j - 1];
}
// Checks if top right corner exists
if (i != 0 && j + 1 != width)
{
grid[0][2] = copy[i - 1][j + 1];
}
// Checks if bottom left corner exists
if (i + 1 != height && j != 0)
{
grid[2][0] = copy[i + 1][j - 1];
}
// Checks if bottom right corner exists
if (i + 1 != height && j + 1 != width)
{
grid[2][2] = copy[i + 1][j + 1];
}
// Store products of x version and y version
RGBTRIPLE XX[3][3], YY[3][3];
duplicate(grid, XX);
duplicate(grid, YY);
// Multiplies each pixel in grid by corresponding values for x version
multiply(&XX[0][0], -1);
multiply(&XX[2][0], -1);
multiply(&XX[1][0], -2);
multiply(&XX[1][2], 2);
multiply(&XX[0][2], 1);
multiply(&XX[2][0], 1);
multiply(&XX[0][1], 0);
multiply(&XX[1][1], 0);
multiply(&XX[2][1], 0);
// Multiplies each pixel in grid by corresponding values for y version
multiply(&YY[0][0], -1);
multiply(&YY[0][2], -1);
multiply(&YY[0][1], -2);
multiply(&YY[2][1], 2);
multiply(&YY[2][0], 1);
multiply(&YY[2][2], 1);
multiply(&YY[1][0], 0);
multiply(&YY[1][1], 0);
multiply(&YY[1][2], 0);
// Stores sum of the products for x and y version
RGBTRIPLE Gx, Gy;
Gx.rgbtRed = Gx.rgbtGreen = Gx.rgbtBlue = Gy.rgbtRed = Gy.rgbtGreen = Gy.rgbtBlue = 0;
sum(&Gx, XX);
sum(&Gy, YY);
set_pixel(Gx, Gy, &image[i][j]);
}
}
}
以下是支持边的功能:
// Sets new RGB values for a pixel
void set_pixel(RGBTRIPLE Gx, RGBTRIPLE Gy, RGBTRIPLE *pixel)
{
int red, green, blue;
red = (int) round(sqrt((Gx.rgbtRed * Gx.rgbtRed) + (Gy.rgbtRed * Gy.rgbtRed)));
green = (int) round(sqrt((Gx.rgbtGreen * Gx.rgbtGreen) + (Gy.rgbtGreen * Gy.rgbtGreen)));
blue = (int) round(sqrt((Gx.rgbtBlue * Gx.rgbtBlue) + (Gy.rgbtBlue * Gy.rgbtBlue)));
if (red > 255)
{
red = 255;
}
if (green > 255)
{
green = 255;
}
if (blue > 255)
{
blue = 255;
}
pixel->rgbtRed = red;
pixel->rgbtGreen = green;
pixel->rgbtBlue = blue;
}
// Adds the products for each colour together
void sum(RGBTRIPLE *sum, RGBTRIPLE g[3][3])
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
sum->rgbtRed += g[i][j].rgbtRed;
sum->rgbtGreen += g[i][j].rgbtGreen;
sum->rgbtBlue += g[i][j].rgbtBlue;
}
}
}
// Copies 3 x 3 grid into XX or YY
void duplicate(RGBTRIPLE grid[3][3], RGBTRIPLE c[3][3])
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
c[i][j] = grid[i][j];
}
}
}
// Resets 3 x 3 grid all to 0
void reset(RGBTRIPLE grid[3][3])
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
grid[i][j].rgbtRed = grid[i][j].rgbtGreen = grid[i][j].rgbtBlue = 0;
}
}
}
// Multiplies the amount of each colour by a given factor
void multiply(RGBTRIPLE *x, int factor)
{
x->rgbtRed *= factor;
x->rgbtGreen *= factor;
x->rgbtBlue *= factor;
}
有关更多信息,这是check50所说的: https://submit.cs50.io/check50/081a89d81042ad121b7f63d2af8deaf8225cca71