C &引用;“不连续性”;图像边缘的高斯模糊
我一直在尝试实现nxn图像的高斯模糊函数,其高斯核的具体半径为rs=((int)2.75*sigma+0.5)C &引用;“不连续性”;图像边缘的高斯模糊,c,C,我一直在尝试实现nxn图像的高斯模糊函数,其高斯核的具体半径为rs=((int)2.75*sigma+0.5) for(x=0;x让我们看一看应用于图像的典型过滤器内核,使用伪代码 # source[y][x] Old image (read-only) # target[y][x] New image (write-only) # image_height Image height (y = 0 .. image_height-1) # image_width Ima
for(x=0;x让我们看一看应用于图像的典型过滤器内核,使用伪代码
# source[y][x] Old image (read-only)
# target[y][x] New image (write-only)
# image_height Image height (y = 0 .. image_height-1)
# image_width Image width (x = 0 .. image_width-1)
# filter[y][x] Filter (weights) to be applied
# filter_height Filter height (y = 0 .. filter_height-1)
# filter_width Filter width (x = 0 .. filter_width-1)
# filter_y Target pixel y coordinate in filter (filter_height/2)
# filter_x Target pixel x coordinate in filter (filter_width/2)
其中filter\u y=floor(filter\u width/2)
和filter\u x=floor(filter\u height/2)
如果滤波器位于目标像素的中心(即对称),则伪码大致为
For base_y = 0 to image_height - 1:
# y range relative to base_y ...
min_y = -filter_y
max_y = filter_height - 1 - filter_y
# ... must not exceed the image boundaries.
If min_y + base_y < 0:
min_y = -base_y
End If
If max_y + base_y < 0:
max_y = -base_y
End If
If min_y + base_y >= image_height:
min_y = image_height - 1 - base_y
End If
If max_y + base_y >= image_height:
max_y = image_height - 1 - base_y
End If
For base_x = 0 to image_width - 1:
# x range relative to base_x ...
min_x = -filter_x
max_x = filter_width - 1 - filter_x
# ... must not exceed the image boundaries.
If min_x + base_x < 0:
min_x = -base_x
End If
If max_x + base_x < 0:
max_x = -base_x
End If
If min_x + base_x >= image_width:
min_x = image_width - 1 - base_x
End If
If max_x + base_x >= image_height:
max_x = image_width - 1 - base_x
End If
ValueSum = 0
WeightSum = 0
For y = min_y to max_y:
For x = min_x to max_x:
Value = source[y + base_y][x + base_x]
Weight = filter[y + filter_y][x + filter_x]
ValueSum = ValueSum + Value * Weight
WeightSum = WeightSum + Weight
End For
End For
If WeightSum != 0:
target[base_y][base_x] = ValueSum / WeightSum
End If
End For
End For
在外环内部(无需为每个base_x
!)打印它),以及
一旦进入最里面的循环(无需为每个base\u y
再次打印),例如if(y==0)printf(“…”)
。这将输出image\u width+image\u height
行,并让您验证定义的范围是否正确
在OP的情况下,图像边缘附近的范围不正确;即,与上述伪代码对应的一些if
子句计算/分配不正确的min\u x
,max\u x
,min\u y
和max\u y
值。我看不出您的计算有任何错误,但可能是舍入错误或者。你每一步都在转换为整数,如果你将所有数据存储为浮点或双精度进行计算,然后在最后将其四舍五入到最接近的整数,你会得到更好的结果。你每次转换为整数的方式都有很大的误差。我尝试将sum/wghtsum存储到单独的浮点矩阵中,然后将其再次写入int中的像素,但它的作用是相同的。我指的是您在几乎每个步骤中都执行的对int
的转换。确保所有类型为double的变量,并删除(int)的每个实例
,这会在小数点可能包含重要数据时截断小数点,特别是因为您执行了多次计算。您可能会被大量截断。简言之,这个答案的要点是偏移每个像素的内核加权和,因此有效内核和总是加在一起。他通过计算内核来实现这一点每次有效应用l单元格值,并将结果值除以它。整洁的解决方案,我喜欢!(如果我错了,请纠正我)
For base_y = 0 to image_height - 1:
# y range relative to base_y ...
min_y = -filter_y
max_y = filter_height - 1 - filter_y
# ... must not exceed the image boundaries.
If min_y + base_y < 0:
min_y = -base_y
End If
If max_y + base_y < 0:
max_y = -base_y
End If
If min_y + base_y >= image_height:
min_y = image_height - 1 - base_y
End If
If max_y + base_y >= image_height:
max_y = image_height - 1 - base_y
End If
For base_x = 0 to image_width - 1:
# x range relative to base_x ...
min_x = -filter_x
max_x = filter_width - 1 - filter_x
# ... must not exceed the image boundaries.
If min_x + base_x < 0:
min_x = -base_x
End If
If max_x + base_x < 0:
max_x = -base_x
End If
If min_x + base_x >= image_width:
min_x = image_width - 1 - base_x
End If
If max_x + base_x >= image_height:
max_x = image_width - 1 - base_x
End If
ValueSum = 0
WeightSum = 0
For y = min_y to max_y:
For x = min_x to max_x:
Value = source[y + base_y][x + base_x]
Weight = filter[y + filter_y][x + filter_x]
ValueSum = ValueSum + Value * Weight
WeightSum = WeightSum + Weight
End For
End For
If WeightSum != 0:
target[base_y][base_x] = ValueSum / WeightSum
End If
End For
End For
printf("y = %d, ymin = %d (%d), ymax = %d (%d)\n",
base_y, min_y, min_y + base_y, max_y, max_y + base_y);
printf("x = %d, xmin = %d (%d), xmax = %d (%d)\n",
base_x, min_x, min_x + base_x, max_x, max_x + base_x);