请立即判断一下问题所在.请找到最后一个要实现的函数,即边缘检测函数.下面的代码不能正常工作,只检测第一个像素的边缘.
void edges(int height, int width, RGBTRIPLE image[height][width])
{
//creating a copy of image
RGBTRIPLE copy[height][width];
//copying original image to copy
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
copy[i][j] = image[i][j];
}
}
//Initializing value of x & y kernels
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}};
//itirating over height and width
int sxr, sxb, sxg, syr, syb, syg;
sxr = sxb = sxg = syr = syb = syg = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++)
{
//skips if the pixel is outside the boundary
if(i + k - 1 < 0 || i + k - 1 > height || j + l - 1 < 0 || j + l - 1 > width)
{
continue;
}
// if the pixel is inside the boundary
sxr += ((copy[i - 1 + k][j - 1 + l].rgbtRed) * (Gx[k][l]));
sxb += ((copy[i - 1 + k][j - 1 + l].rgbtBlue) * (Gx[k][l]));
sxg += ((copy[i - 1 + k][j - 1 + l].rgbtGreen) * (Gx[k][l]));
syr += ((copy[i - 1 + k][j - 1 + l].rgbtRed) * (Gy[k][l]));
syb += ((copy[i - 1 + k][j - 1 + l].rgbtBlue) * (Gy[k][l]));
syg += ((copy[i - 1 + k][j - 1 + l].rgbtGreen) * (Gy[k][l]));
}
}
int srtR = round(sqrt(sxr * sxr + syr * syr));
int srtB = round(sqrt(sxb * sxb + syb * syb));
int srtG = round(sqrt(sxg * sxg + syg * syg));
//corner case of RGB values > 255
if (srtR > 255)
{
srtR = 255;
}
if (srtB > 255)
{
srtB = 255;
}
if (srtG > 255)
{
srtG = 255;
}
copy[i][j].rgbtRed = srtR;
copy[i][j].rgbtBlue = srtB;
copy[i][j].rgbtGreen = srtG;
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = copy[i][j].rgbtRed;
image[i][j].rgbtBlue = copy[i][j].rgbtBlue;
image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
}
}
}
在此代码中,Check50是this
此外,我发现下面的代码可以正常工作
void edges(int height, int width, RGBTRIPLE image[height][width])
{
//creating a copy of image
RGBTRIPLE copy[height][width];
//copying original image to copy
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
copy[i][j] = image[i][j];
}
}
//Initializing value of x & y kernels
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}};
//itirating over height and width
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
float sxr, sxb, sxg, syr, syb, syg;
sxr = sxb = sxg = syr = syb = syg = 0;
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++)
{
//skips if the pixel is outside the boundary
if(i + k - 1 < 0 || i + k - 1 >= height || j + l - 1 < 0 || j + l - 1 >= width)
{
continue;
}
// if the pixel is inside the boundary
sxr += ((copy[i - 1 + k][j - 1 + l].rgbtRed) * (Gx[k][l]));
sxb += ((copy[i - 1 + k][j - 1 + l].rgbtBlue) * (Gx[k][l]));
sxg += ((copy[i - 1 + k][j - 1 + l].rgbtGreen) * (Gx[k][l]));
syr += ((copy[i - 1 + k][j - 1 + l].rgbtRed) * (Gy[k][l]));
syb += ((copy[i - 1 + k][j - 1 + l].rgbtBlue) * (Gy[k][l]));
syg += ((copy[i - 1 + k][j - 1 + l].rgbtGreen) * (Gy[k][l]));
}
}
int srtR = round(sqrt((sxr * sxr) + (syr * syr)));
int srtB = round(sqrt(sxb * sxb + syb * syb));
int srtG = round(sqrt(sxg * sxg + syg * syg));
//corner case of RGB values > 255
if (srtR > 255)
{
srtR = 255;
}
if (srtB > 255)
{
srtB = 255;
}
if (srtG > 255)
{
srtG = 255;
}
image[i][j].rgbtRed = srtR;
image[i][j].rgbtBlue = srtB;
image[i][j].rgbtGreen = srtG;
}
}
}
在上面的代码中,只有变量diference的声明.
有人能解释一下为什么声明变量的地方在代码中会产生如上的影响吗