使用cimg在高斯均值滤波器上进行C ++ seg故障

时间:2022-09-01 23:55:22

I just tried implemting this filter using cimg but keep getting a seg fault. I am not sure why this is happening as the other filters that I have been using do not have that issue. Is there anything obvious that I am missing here?

我只是尝试使用cimg实现此过滤器,但不断遇到seg错误。我不确定为什么会发生这种情况,因为我一直在使用的其他过滤器没有这个问题。我有什么明显的遗失吗?

  CImg<float> gaussianBlur(CImg<float> source)
    {
        double frame[25];
        double mean = 0;
        int width = source.width;
        int height = source.height;
        CImg<float> destination;
        destination = source;

    for (int x = 1; x < int(width) - 3; x++)

{
    for (int y = 1; y < int(height) - 3; y++)
    {
        mean = 0.0;

        frame[0] = int(source(x - 2 ,y - 2)) * .003765;
        frame[1] = int(source(x - 1 ,y - 2)) * .015019;
        frame[2] = int(source(x - 0 ,y - 2)) * .023792;
        frame[3] = int(source(x + 1 ,y - 2)) * .015019;
        frame[4] = int(source(x + 2 ,y - 2)) * .003765;

        frame[5] = int(source(x - 2 ,y - 1)) * .015019;
        frame[6] = int(source(x - 1 ,y - 1)) * .059912;
        frame[7] = int(source(x - 0 ,y - 1)) * .094907;
        frame[8] = int(source(x + 1 ,y - 1)) * .059912;
        frame[9] = int(source(x + 2 ,y - 1)) * .015019;

        frame[10] = int(source(x - 2 ,y - 0)) * .023792;
        frame[11] = int(source(x - 1 ,y - 0)) * .094907;
        frame[12] = int(source(x - 0 ,y - 0)) * .150342;
        frame[13] = int(source(x + 1 ,y - 0)) * .094907;
        frame[14] = int(source(x + 2 ,y - 0)) * .023792;

        frame[15] = int(source(x - 2 ,y + 1)) * .015019;
        frame[16] = int(source(x - 1 ,y + 1)) * .059912;
        frame[17] = int(source(x - 0 ,y + 1)) * .094907;
        frame[18] = int(source(x + 1 ,y + 1)) * .059912;
        frame[19] = int(source(x + 2 ,y + 1)) * .015019;

        frame[20] = int(source(x - 2 ,y + 2)) * .003765;
        frame[21] = int(source(x - 1 ,y + 2)) * .015019;
        frame[22] = int(source(x - 0 ,y + 2)) * .023792;
        frame[23] = int(source(x + 1 ,y + 2)) * .015019;
        frame[24] = int(source(x + 2 ,y + 2)) * .003765;

        for (int z = 0; z < 25; z++)
        {
            mean += frame[z];
        }

        destination(x,y) = float(mean / 25);
    }
   }

    return destination;
}

1 个解决方案

#1


0  

for (int y = 1; y < int(height) - 3; y++)
{
    mean = 0.0;

    frame[0] = int(source(x - 2 ,y - 2)) * .003765;

When y == 1 you have y - 2 == -1, so you have an out-of-bounds access to the source image.

当y == 1时,你有y - 2 == -1,所以你对源图像有一个越界访问。

#1


0  

for (int y = 1; y < int(height) - 3; y++)
{
    mean = 0.0;

    frame[0] = int(source(x - 2 ,y - 2)) * .003765;

When y == 1 you have y - 2 == -1, so you have an out-of-bounds access to the source image.

当y == 1时,你有y - 2 == -1,所以你对源图像有一个越界访问。