-1

So here is the function that does the sobel process:

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);
        }
    }
}

I have a c++ code for sobel operator on pgm image, my code compiles but fails to give me the required results. Can anyone tell me what is wrong?

Yossarian
  • 5,226
  • 1
  • 37
  • 59

1 Answers1

1

This part

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

looks wrong.

You assign -1 to iPic and then test if iPic >= 1. That will always be false.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63