请立即判断一下问题所在.请找到最后一个要实现的函数,即边缘检测函数.下面的代码不能正常工作,只检测第一个像素的边缘.

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的声明.

有人能解释一下为什么声明变量的地方在代码中会产生如上的影响吗

推荐答案

有人能解释一下为什么声明变量的地方在代码中会产生如上的影响吗

在您的例子中,工作代码在for( int j ...循环的范围内声明and initialises sxrsxbsxgsyrsybsyg.您的非工作代码在开始时只声明和初始化它们一次.这意味着工作代码每隔ij(每个像素?)重置它们,但非工作代码累加整个图像的值.

C++相关问答推荐

GCC不警告隐式指针到整数转换'

C语言中的strstr问题

有没有可能我不能打印?(C,流程)

如何将常量char*复制到char数组

如何捕捉只有换行符或空格字符缓冲区的边缘大小写

为什么双精度d=flt_max+flt_max;在c语言中得到inf的结果

如何用c语言修改shadow文件hash部分(编程)?

OpenSSL:如何将吊销列表与SSL_CTX_LOAD_VERIFY_LOCATIONS一起使用?

在C语言中,指针指向一个数组

GCC奇怪的行为,有fork 和印花,有换行符和不换行符

Wcstok导致分段故障

为四维数组中的Dim-1和Dim-3重新分配空间

为什么我的半数组测试和奇数组测试不起作用?(我使用Assert进行调试)

带有数组指针的 struct 在print_stack()函数中打印随机数

正在try 理解C++中的`正在释放的指针未被分配‘错误

在同一范围内对具有相同类型的变量执行的相同操作在同一C代码中花费的时间不同

GCC认为这是一个VLA是对的吗?

'printf("%s", user_input)' 危险吗?

malloc:损坏的顶部大小无法找出问题

C 中类型说明符的顺序重要吗?