所以这里是执行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++代码,我的代码进行了编译,但未能给出所需的结果。有人能告诉我怎么了吗?
本部分
for(iFilter = 0, iPic =- 1; iFilter < MATRIX_MAX && iPic >= 1; iFilter++, iPic++)
看起来不对。
将-1分配给iPic,然后测试iPic是否>=1。这将永远是错误的。