在R中实现MATLAB filter2函数

时间:2016-04-20 15:24:51

标签: r matlab filter 2d convolution

我目前正在R中实现filter2 MATLAB函数,这是一种2D卷积方法。我已经为2D卷积工作做了准备,但是有效的' filter2中的选项对我来说不太清楚。

这里描述MATLAB函数: http://se.mathworks.com/help/matlab/ref/filter2.html

我的实施:

filter2D <- function(img, window) {
  # Algoritm for 2D Convolution
  filter_center_index_y <- median(1:dim(window)[1])
  filter_max_index_y <- dim(window)[1]
  filter_center_index_x <- median(1:dim(window)[2])
  filter_max_index_x <- dim(window)[2]

  # For each position in the picture, 2D convolution is done by 
  # calculating a score for all overlapping values within the two matrices
  x_min <- 1
  x_max <- dim(img)[2]
  y_min <- 1
  y_max <- dim(img)[1]

  df <- NULL
  for (x_val in c(x_min:x_max)){
    for (y_val in c(y_min:y_max)){
      # Distanced from cell
      img_dist_left <- x_val-1
      img_dist_right <- x_max-x_val
      img_dist_up <- y_val-1
      img_dist_down <- y_max-y_val

      # Overlapping filter cells
      filter_x_start <- filter_center_index_x-img_dist_left
      if (filter_x_start < 1) {
        filter_x_start <- 1
      }
      filter_x_end <- filter_center_index_x+img_dist_right
      if (filter_x_end > filter_max_index_x) {
        filter_x_end <- filter_max_index_x
      }
      filter_y_start <- filter_center_index_y-img_dist_up
      if (filter_y_start < 1) {
        filter_y_start <- 1
      }
      filter_y_end <- filter_center_index_y+img_dist_down
      if (filter_y_end > filter_max_index_y) {
        filter_y_end <- filter_max_index_y
      }

      # Part of filter that overlaps
      filter_overlap_matrix <- filter[filter_y_start:filter_y_end, filter_x_start:filter_x_end]

      # Overlapped image cells
      image_x_start <- x_val-filter_center_index_x+1
      if (image_x_start < 1) {
        image_x_start <- 1
      }
      image_x_end <- x_val+filter_max_index_x-filter_center_index_x
      if (image_x_end > x_max) {
        image_x_end <- x_max
      }
      image_y_start <- y_val-filter_center_index_y+1
      if (image_y_start < 1) {
        image_y_start <- 1
      }
      image_y_end <- y_val+filter_max_index_y-filter_center_index_y
      if (image_y_end > y_max) {
        image_y_end <- y_max
      }

      # Part of image that is overlapped
      image_overlap_matrix <- img[image_y_start:image_y_end, image_x_start:image_x_end]

      # Calculating the cell value
      cell_value <- sum(filter_overlap_matrix*image_overlap_matrix)
      df = rbind(df,data.frame(x_val,y_val, cell_value))
    }
  }

  # Axis labels
  x_axis <- c(x_min:x_max)
  y_axis <- c(y_min:y_max)

  # Populating matrix
  filter_matrix <- matrix(df[,3], nrow = x_max, ncol = y_max, dimnames = list(x_axis, y_axis))

  return(filter_matrix)
}

运行方法:

> image
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    7    8    9   10   11   12
[3,]   13   14   15   16   17   18
[4,]   19   20   21   22   23   24
[5,]   25   26   27   28   29   30
[6,]   31   32   33   34   35   36

> filter
     [,1] [,2] [,3]
[1,]    1    2    1
[2,]    0    0    0
[3,]   -1   -2   -1

> filter2D(image, filter)
    1   2   3   4   5   6
1 -22 -32 -36 -40 -44 -35
2 -36 -48 -48 -48 -48 -36
3 -36 -48 -48 -48 -48 -36
4 -36 -48 -48 -48 -48 -36
5 -36 -48 -48 -48 -48 -36
6  76 104 108 112 116  89 

这与filter2(图像,过滤器)在matlab中生成的输出相同,但是,当选项“有效”时,添加以下输出:

-48  -48  -48  -48                                                                                                                                                                                                 
-48  -48  -48  -48                                                                                                                                                                                                 
-48  -48  -48  -48                                                                                                                                                                                                 
-48  -48  -48  -48  

如果filter2具有&#39;有效&#39;则不完全明显。选项生成此。它只是使用中心值吗?或者它正在做一些更复杂的事情?

1 个答案:

答案 0 :(得分:2)

在开始之前,您的代码实际上正在执行2D 关联。 2D卷积要求在进行加权求和之前在内核上执行180度旋转。如果内核是对称的(即内核的转置等于它自己),则相关和卷积实际上是相同的操作。我只是想在开始之前说清楚。此外,filter2的文档确实说明正在执行关联。

MATLAB中的'valid'选项仅意味着它应该仅返回内核在执行过滤时与2D信号完全重叠的输出。因为你有一个3 x 3内核,这意味着在2D信号中的位置(2,2),例如,内核不会超出信号边界。因此,返回的是滤波的2D信号的范围,其中内核完全在原始2D信号内。举个例子,如果你把内核放在位置(1,1),那么某些内核会超出范围。在过滤时处理越界条件可以通过多种方式完成,这可能会影响结果并在结果归结时解释这些结果。因此,当您使用形成最终结果的真实信息时,需要'valid'选项。您没有对超出2D信号边界的数据进行插值或执行任何估计。

简单地说,你返回一个删除边框元素的简化矩阵。过滤器形状奇特,这很容易。您只需删除第一个和最后一个floor(M/2)行以及floor(N/2)列,其中M x N是内核的大小。因此,因为你的内核是3 x 3,这意味着我们需要从顶部删除1行,从底部删除1行,从左边删除1列,从右边删除1列。从MATLAB的输出中可以看出,-48在4 x 4网格内。

因此,如果您想在代码中使用'valid'选项,只需确保删除结果中的边框元素即可。您可以在返回矩阵之前完成此操作:

# Place your code here...
# ...
# ...
# Now we're at the end of your code

# Populating matrix
filter_matrix <- matrix(df[,3], nrow = x_max, ncol = y_max, dimnames = list(x_axis, y_axis))

# New - Determine rows and columns of matrix as well as the filter kernel
nrow_window <- nrow(window)
ncol_window <- ncol(window)
nrows <- nrow(filter_matrix)
ncols <- ncol(filter_matrix)

# New - Figure out where to cut off
row_cutoff <- floor(nrow_window/2)
col_cutoff <- floor(ncol_window/2)

# New - Remove out borders
filter_matrix <- filter_matrix[((1+row_cutoff):(nrows-row_cutoff)), ((1+col_cutoff):(ncols-col_cutoff))]

# Finally return matrix
return(filter_matrix)    

运行示例

使用您的数据:

> image <- t(matrix(c(1:36), nrow=6, ncol=6))
> image
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    7    8    9   10   11   12
[3,]   13   14   15   16   17   18
[4,]   19   20   21   22   23   24
[5,]   25   26   27   28   29   30
[6,]   31   32   33   34   35   36
> filter <- matrix(c(1,0,-1,2,0,-2,1,0,-1), nrow=3, ncol=3)
> filter
     [,1] [,2] [,3]
[1,]    1    2    1
[2,]    0    0    0
[3,]   -1   -2   -1

我运行了这个功能,现在我得到了:

> filter2D(image,filter)
    2   3   4   5
2 -48 -48 -48 -48
3 -48 -48 -48 -48
4 -48 -48 -48 -48
5 -48 -48 -48 -48

我认为让水平和垂直标签保持原样可能很重要,这样你就可以明确地看到并没有返回所有信号,这就是代码当前正在做的事情......那是尽管如此。我会留给你决定。