2016-11-04 121 views
1

我一直在尝试用特定半径rs =((int)2.75 * sigma + 0.5)的高斯核函数实现nxn图像的高斯模糊函数。围绕图像边缘的高斯模糊的“不连续”

for (x=0;x<n;x++){ 
    for (y=0;y<n;y++){ 

     sum=0.0,wsum=0.0; 

     //Position correction at the edges 

     if(x-rs<0){ 
      ix=0; 
     } 
     else ix=rs; 

     if(y-rs<0){ 
      iy=0; 
     } 
     else iy=rs; 

     if (x+rs>n-1){ 
      jx=n-1-x; 
     } 
     else jx=rs; 

     if (y+rs>n-1){ 
      jy=n-1-y; 
     } 
     else jy=rs; 
     //Kernel mean value correction at the edges 

     if (x-rs < 0){ 
      meanx=x+((int)rs/2); 
     } 
     else meanx=x; 

     if(y-rs<0){ 
      meany=y+((int)rs/2); 
     } 
     else meany=y; 

     if (x+rs>n-1){ 
      meanx=x-((int)rs/2); 
     } 
     else meanx=x; 

     if (y+rs>n-1){ 
      meany=y-((int)rs/2); 
     } 
     else meany=y; 


     for (i=x-ix;i<=x+jx;i++){ 
      for (j=y-iy;j<=y+jy;j++){ 

       weight=1/(2*M_PI*sigma*sigma)*exp(-((meanx-i)*(meanx-i)+(meany-j)*(meany-j))/(2*sigma*sigma)); 
       sum+=pic1.intenzity[i][j]*weight; 
       wsum+=weight; 
      } 
     } 

     pic2->intenzity[x][y]=((int)sum/wsum+0.5); 

     fprintf(fw,"%d\n",pic2->intenzity[x][y]); 
    } 

当我没有在边缘处使用平均值修正的结果是这样的:

without mean value correction

,当我试图转移内核的平均值它创建一个间断也是在图像的右下方边缘:

with shifting the mean value to rs/2

我不得不做出边缘positio n的修正,因为总和会溢出。现在看来,高斯卷积在某些原因下突然跳跃,当它位于rs和x和y的上边和左边时。我想让它的行为与它在图像“内部”中的行为相同,或者在位置接近边缘时使强度消失为0。

我可能会通过rs放大图像,但它会导致边缘位置的问题。

感谢您

+0

我没有看到你的计算有什么问题,但它可能是一个舍入误差。如果您将所有数据存储为浮点数或双精度执行计算,则您将转换为整数每一步的整数,然后在最后将其舍入到最接近的整数。每次你转换为int的方式都有很大的错误余量。 –

+0

我尝试将sum/wghtsum存储到单独的浮点矩阵中,然后再将它写入int中的像素,但它做了同样的事情。 – Martin

+0

我指的是你在几乎每一步都要做的“int”投射。确保所有类型为double的变量,并删除每个“(int)”实例,这会在包含重要数据时截断小数,尤其是因为您执行了多次计算。你可能会被大量消费。 –

回答

0

让我们来看看一个典型的滤波器内核应用于图像的任何有见地的帮助:),在伪代码。允许使用变量

# 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_y = floor(filter_width/2)filter_x = floor(filter_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_y][base_x]指目标像素,我们计算一个;而[y+base_y][x+base_x]指的是由[y+filter_y][x+filter_x]加权的源像素。 xy是相对值,分别从-filter_x-filter_yfilter_width-1-filter_xfilter_height-1-filter_y

只要ValueSumWeightSum有足够的范围,相同的代码工作,无论图像和过滤器数据是整数还是浮点数。

最棘手的部分,并导致OP看到文物的一部分,是如何正确计算min_ymax_ymin_xmax_x

要调试,删除这两个内部循环,而是打印出类似这样

printf("y = %d, ymin = %d (%d), ymax = %d (%d)\n", 
     base_y, min_y, min_y + base_y, max_y, max_y + base_y); 

外环(无需打印,每base_x!)里面,

printf("x = %d, xmin = %d (%d), xmax = %d (%d)\n", 
     base_x, min_x, min_x + base_x, max_x, max_x + base_x); 

一次在最里面的循环中(不需要再为每个base_y打印一遍),例如if (y == 0) printf("...");。这将输出image_width + image_height行,并让您验证您定义的范围是否正确。

在OP的情况下,范围在图像边缘附近是不正确的;即它们对应于上述伪代码的一些if子句计算/分配不正确的min_x,max_x,min_ymax_y值。