-1

所以这里是执行 sobel 过程的函数:

void sobelOperator(int& x, int& y)
{
    ImageType image;

    for(int i = 0; i<x; i++)
    {
        for(int j = 0; j<y; j++)
        {

            //constants
            const int MATRIX_MAX = 3;
            const int PIXEL_MAX = 255;


            //value for the new cell calculation
            int GXpixel = 0, GYpixel = 0, Gpixel = 0;


            //Sobel Horizontal Mask or matrix for the Kx convolution
            const int sobelKxFilterMatrix[3][3] = {{1, 0, -1}, {2, 0, -2}, {1, 0, -1}};

            //Sobel Vertical Mask or matrix for the Ky convolution
            const int sobelKyFilterMatrix[3][3] = {{1, 2, 1}, {0, 0, 0}, {-1, -2, -1}};

            //for addressing into filter array and into picture
            int iFilter, jFilter;
            int iPic, jPic;

            //Loop to iterate over picture and filter window to perform sobel operation
            for(iFilter = 0, iPic =- 1; iFilter < MATRIX_MAX && iPic >= 1; iFilter++, iPic++)
            {
                for(jFilter = 0, jPic =-1; jFilter < MATRIX_MAX && jPic >= 1; jFilter++, jPic++)
                {
                    int val;
                    image.getPixelVal(x+iPic, y+jPic, val);

                    GXpixel += (val * sobelKxFilterMatrix[iFilter][jFilter]);

                    GYpixel += (val * sobelKyFilterMatrix[iFilter][jFilter]);
                }
            }

            //check for pixel saturation
            if(GXpixel > PIXEL_MAX){GXpixel = PIXEL_MAX;}
            if(GYpixel > PIXEL_MAX){GYpixel = PIXEL_MAX;}

            //normalize pixel
            Gpixel = static_cast<int>(sqrt(pow(static_cast<double>(GXpixel), 2.0) + pow(static_cast<double>(GYpixel), 2.0)));

            image.setPixelVal(i, j, Gpixel);
        }
    }
}

我在 pgm 图像上有一个用于 sobel 运算符的 c++ 代码,我的代码编译但未能给我所需的结果。谁能告诉我出了什么问题?

4

1 回答 1

1

这部分

for(iFilter = 0, iPic =- 1; iFilter < MATRIX_MAX && iPic >= 1; iFilter++, iPic++)

看起来不对。

您将 -1 分配给 iPic,然后测试 iPic >= 1。这将始终是错误的。

于 2015-11-30T14:22:45.900 回答