Sobel 梯度运算符:未获得所需的输出
Sobel gradient operator: Not getting desired output
我在维基百科 (Bikesgray) 的图像上应用了 sobel 运算符。但是我没有得到想要的输出。我的输出比维基百科中显示的输出更亮。我的代码在 .pgm 文件上运行。
这是我的代码的输出和维基百科中显示的输出
这是我的 C 代码:
//image[][] is input image. temp_image[][] is output image. temp_image[][] and image[][] are global.
// 3 x 3 region of an image will be [z1 z2 z3]
[z4 z5 z6]
[z7 z8 z9]
//x direction mask [-1 -2 -1]
[0 0 0]
[1 2 1]
//y direction mask [-1 0 1]
[-2 0 2]
[-1 0 1]
void find_sobel_gradient_image()
{
int i,j;
int gx,gy;
double m;
int padded_image[700][700]={0};
//create padded image of 1pixel zero padding and copy data from image[][]
for(i=1;i<=y_size;i++)
{
for(j=1;j<=x_size;j++)
{
padded_image[i][j]=image[i-1][j-1];
}
}
//resulting image
for(i=1;i<=y_size;i++)
{
for(j=1;j<=x_size;j++)
{
//gx = (z7 + 2*z8 + z9)-(z1 + 2*z2+ z3)
gx=(padded_image[i+1][j-1]+2*padded_image[i+1][j]+padded_image[i+1][j+1])-(padded_image[i-1][j-1]+2*padded_image[i-1][j]+padded_image[i-1][j+1]);
//gy = (z3 + 2*z6 +z9) - (z1+ 2*z4 +z7)
gy=(padded_image[i-1][j+1]+2*padded_image[i][j+1]+padded_image[i+1][j+1])-(padded_image[i-1][j-1]+2*padded_image[i][j-1]+padded_image[i+1][j-1]);
m = sqrt(gx*gx+gy*gy);
temp_image[i][j]=round(m);
}
}
}
注:每个temp[ i ][ j ] > 255 ==> temp[ i ][ j ] = 255
维基百科的描述是normalized magnitude, yet you've clamped的量级。要标准化大小,请找到最大值和将此值映射到 255 的除数,然后将每个像素值除以该除数。
我在维基百科 (Bikesgray) 的图像上应用了 sobel 运算符。但是我没有得到想要的输出。我的输出比维基百科中显示的输出更亮。我的代码在 .pgm 文件上运行。
这是我的代码的输出和维基百科中显示的输出
这是我的 C 代码:
//image[][] is input image. temp_image[][] is output image. temp_image[][] and image[][] are global.
// 3 x 3 region of an image will be [z1 z2 z3]
[z4 z5 z6]
[z7 z8 z9]
//x direction mask [-1 -2 -1]
[0 0 0]
[1 2 1]
//y direction mask [-1 0 1]
[-2 0 2]
[-1 0 1]
void find_sobel_gradient_image()
{
int i,j;
int gx,gy;
double m;
int padded_image[700][700]={0};
//create padded image of 1pixel zero padding and copy data from image[][]
for(i=1;i<=y_size;i++)
{
for(j=1;j<=x_size;j++)
{
padded_image[i][j]=image[i-1][j-1];
}
}
//resulting image
for(i=1;i<=y_size;i++)
{
for(j=1;j<=x_size;j++)
{
//gx = (z7 + 2*z8 + z9)-(z1 + 2*z2+ z3)
gx=(padded_image[i+1][j-1]+2*padded_image[i+1][j]+padded_image[i+1][j+1])-(padded_image[i-1][j-1]+2*padded_image[i-1][j]+padded_image[i-1][j+1]);
//gy = (z3 + 2*z6 +z9) - (z1+ 2*z4 +z7)
gy=(padded_image[i-1][j+1]+2*padded_image[i][j+1]+padded_image[i+1][j+1])-(padded_image[i-1][j-1]+2*padded_image[i][j-1]+padded_image[i+1][j-1]);
m = sqrt(gx*gx+gy*gy);
temp_image[i][j]=round(m);
}
}
}
注:每个temp[ i ][ j ] > 255 ==> temp[ i ][ j ] = 255
维基百科的描述是normalized magnitude, yet you've clamped的量级。要标准化大小,请找到最大值和将此值映射到 255 的除数,然后将每个像素值除以该除数。