Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R语言中实现Matlab专用函数_R_Matlab_2d_Blur_Gaussian - Fatal编程技术网

在R语言中实现Matlab专用函数

在R语言中实现Matlab专用函数,r,matlab,2d,blur,gaussian,R,Matlab,2d,Blur,Gaussian,我需要计算R中的结构相似性(SSIM)指数,并且只能找到在Matlab中实现的方法。除了两种Matlab方法“fspecial”和“filter2”,在R中重新编写该方法似乎非常简单 F特殊返回11x11矩阵中的二维高斯分布,标准偏差为1.5: h = fspecial('gaussian', 11, 1.5) 因此,我在R中实现了一个功能,该功能也可以在网上找到一些帮助: gaussian2D <- function(amplitude) { # Defining limits o

我需要计算R中的结构相似性(SSIM)指数,并且只能找到在Matlab中实现的方法。除了两种Matlab方法“fspecial”和“filter2”,在R中重新编写该方法似乎非常简单

F特殊返回11x11矩阵中的二维高斯分布,标准偏差为1.5:

h = fspecial('gaussian', 11, 1.5)
因此,我在R中实现了一个功能,该功能也可以在网上找到一些帮助:

gaussian2D <- function(amplitude) {
  # Defining limits of grid
  x_min <- 1
  x_max <- 11
  y_min <- x_min
  y_max <- x_max

  # Setting parameters of the two-dimensional Gaussian function 
  # The distribution is centred in [6,6]
  x_zero <- 6
  y_zero <- 6

  # Setting the spread of the filter
  sigma_x <- 1.5
  sigma_y <- sigma_x

  # Running through all x and y combinations applying the 2d-gaussian equation
  df <- NULL
  for (x_val in c(x_min:x_max)){
    for (y_val in c(y_min:y_max)){
      cell_value <- amplitude*exp(-( (((x_val-x_zero)^2)/(2*(sigma_x^2))) +     (((y_val-y_zero)^2)/(2*(sigma_y^2))) ))
      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
  gauss_matrix <- matrix(df[,3], nrow = 11, ncol = 11, dimnames = list(x_axis,     y_axis))

  return(gauss_matrix)
}

h2 = gaussian2D(1)

有人对我做错了什么有意见吗?

您缺少调整条件:1/(2*pi*sigma1*sigma2)


请注意,在MATLAB中,2*pi*1.5*1.5=14.13717

fspecial
规范化了内核,使内核中所有元素的总和等于1。这是因为当使用该内核执行卷积时,它避免产生任何超出与您试图过滤的信号相关联的数据类型动态范围的输出值

这也避免了使用@imo之前所述的任何调整术语。非常简单,您没有在代码中规范化内核。因此,在循环中有一个额外的求和项,它对每个高斯项求和,那么最终的矩阵应该将每个项除以这个量:

  df <- NULL
  s <- 0 # Added
  for (x_val in c(x_min:x_max)){
    for (y_val in c(y_min:y_max)){
      cell_value <- amplitude*exp(-( (((x_val-x_zero)^2)/(2*(sigma_x^2))) +     (((y_val-y_zero)^2)/(2*(sigma_y^2))) ))
      df = rbind(df,data.frame(x_val,y_val, cell_value))
      s <- s + cell_value # Added
    }
  }
要再次检查,在MATLAB中,这是调用
fspecial
时得到的结果:

>> format long g
>> h = fspecial('gaussian', 11, 1.5)

h =

  Columns 1 through 3

      1.05756559815326e-06       7.8144115330536e-06      3.70224770827489e-05
       7.8144115330536e-06      5.77411251978637e-05      0.000273561160085806
      3.70224770827489e-05      0.000273561160085806        0.0012960555938432
      0.000112464355116679      0.000831005429087199       0.00393706926284678
      0.000219050652866017       0.00161857756253439       0.00766836382523672
      0.000273561160085806       0.00202135875836257       0.00957662749024029
      0.000219050652866017       0.00161857756253439       0.00766836382523672
      0.000112464355116679      0.000831005429087199       0.00393706926284678
      3.70224770827489e-05      0.000273561160085806        0.0012960555938432
       7.8144115330536e-06      5.77411251978637e-05      0.000273561160085806
      1.05756559815326e-06       7.8144115330536e-06      3.70224770827489e-05

  Columns 4 through 6

      0.000112464355116679      0.000219050652866017      0.000273561160085806
      0.000831005429087199       0.00161857756253439       0.00202135875836257
       0.00393706926284678       0.00766836382523672       0.00957662749024029
         0.011959760410037        0.0232944324734871        0.0290912256485504
        0.0232944324734871        0.0453713590956603        0.0566619704916846
        0.0290912256485504        0.0566619704916846         0.070762237763947
        0.0232944324734871        0.0453713590956603        0.0566619704916846
         0.011959760410037        0.0232944324734871        0.0290912256485504
       0.00393706926284678       0.00766836382523672       0.00957662749024029
      0.000831005429087199       0.00161857756253439       0.00202135875836257
      0.000112464355116679      0.000219050652866017      0.000273561160085806

  Columns 7 through 9

      0.000219050652866017      0.000112464355116679      3.70224770827489e-05
       0.00161857756253439      0.000831005429087199      0.000273561160085806
       0.00766836382523672       0.00393706926284678        0.0012960555938432
        0.0232944324734871         0.011959760410037       0.00393706926284678
        0.0453713590956603        0.0232944324734871       0.00766836382523672
        0.0566619704916846        0.0290912256485504       0.00957662749024029
        0.0453713590956603        0.0232944324734871       0.00766836382523672
        0.0232944324734871         0.011959760410037       0.00393706926284678
       0.00766836382523672       0.00393706926284678        0.0012960555938432
       0.00161857756253439      0.000831005429087199      0.000273561160085806
      0.000219050652866017      0.000112464355116679      3.70224770827489e-05

  Columns 10 through 11

       7.8144115330536e-06      1.05756559815326e-06
      5.77411251978637e-05       7.8144115330536e-06
      0.000273561160085806      3.70224770827489e-05
      0.000831005429087199      0.000112464355116679
       0.00161857756253439      0.000219050652866017
       0.00202135875836257      0.000273561160085806
       0.00161857756253439      0.000219050652866017
      0.000831005429087199      0.000112464355116679
      0.000273561160085806      3.70224770827489e-05
      5.77411251978637e-05       7.8144115330536e-06
       7.8144115330536e-06      1.05756559815326e-06
。。。最后在R中:

> h2
              1            2            3            4            5            6            7
1  1.057566e-06 7.814412e-06 3.702248e-05 0.0001124644 0.0002190507 0.0002735612 0.0002190507
2  7.814412e-06 5.774113e-05 2.735612e-04 0.0008310054 0.0016185776 0.0020213588 0.0016185776
3  3.702248e-05 2.735612e-04 1.296056e-03 0.0039370693 0.0076683638 0.0095766275 0.0076683638
4  1.124644e-04 8.310054e-04 3.937069e-03 0.0119597604 0.0232944325 0.0290912256 0.0232944325
5  2.190507e-04 1.618578e-03 7.668364e-03 0.0232944325 0.0453713591 0.0566619705 0.0453713591
6  2.735612e-04 2.021359e-03 9.576627e-03 0.0290912256 0.0566619705 0.0707622378 0.0566619705
7  2.190507e-04 1.618578e-03 7.668364e-03 0.0232944325 0.0453713591 0.0566619705 0.0453713591
8  1.124644e-04 8.310054e-04 3.937069e-03 0.0119597604 0.0232944325 0.0290912256 0.0232944325
9  3.702248e-05 2.735612e-04 1.296056e-03 0.0039370693 0.0076683638 0.0095766275 0.0076683638
10 7.814412e-06 5.774113e-05 2.735612e-04 0.0008310054 0.0016185776 0.0020213588 0.0016185776
11 1.057566e-06 7.814412e-06 3.702248e-05 0.0001124644 0.0002190507 0.0002735612 0.0002190507
              8            9           10           11
1  0.0001124644 3.702248e-05 7.814412e-06 1.057566e-06
2  0.0008310054 2.735612e-04 5.774113e-05 7.814412e-06
3  0.0039370693 1.296056e-03 2.735612e-04 3.702248e-05
4  0.0119597604 3.937069e-03 8.310054e-04 1.124644e-04
5  0.0232944325 7.668364e-03 1.618578e-03 2.190507e-04
6  0.0290912256 9.576627e-03 2.021359e-03 2.735612e-04
7  0.0232944325 7.668364e-03 1.618578e-03 2.190507e-04
8  0.0119597604 3.937069e-03 8.310054e-04 1.124644e-04
9  0.0039370693 1.296056e-03 2.735612e-04 3.702248e-05
10 0.0008310054 2.735612e-04 5.774113e-05 7.814412e-06
11 0.0001124644 3.702248e-05 7.814412e-06 1.057566e-06

。。。看起来和我很相配

切题地说,您可以查看
outer
expand.grid
。它们可以用来减轻嵌套的
for
循环。谢谢,我现在生成了我想要的!谢谢,我想知道那个号码是从哪里来的!我用圆周率尝试了各种各样的操作,但这是有意义的。
>> format long g
>> h = fspecial('gaussian', 11, 1.5)

h =

  Columns 1 through 3

      1.05756559815326e-06       7.8144115330536e-06      3.70224770827489e-05
       7.8144115330536e-06      5.77411251978637e-05      0.000273561160085806
      3.70224770827489e-05      0.000273561160085806        0.0012960555938432
      0.000112464355116679      0.000831005429087199       0.00393706926284678
      0.000219050652866017       0.00161857756253439       0.00766836382523672
      0.000273561160085806       0.00202135875836257       0.00957662749024029
      0.000219050652866017       0.00161857756253439       0.00766836382523672
      0.000112464355116679      0.000831005429087199       0.00393706926284678
      3.70224770827489e-05      0.000273561160085806        0.0012960555938432
       7.8144115330536e-06      5.77411251978637e-05      0.000273561160085806
      1.05756559815326e-06       7.8144115330536e-06      3.70224770827489e-05

  Columns 4 through 6

      0.000112464355116679      0.000219050652866017      0.000273561160085806
      0.000831005429087199       0.00161857756253439       0.00202135875836257
       0.00393706926284678       0.00766836382523672       0.00957662749024029
         0.011959760410037        0.0232944324734871        0.0290912256485504
        0.0232944324734871        0.0453713590956603        0.0566619704916846
        0.0290912256485504        0.0566619704916846         0.070762237763947
        0.0232944324734871        0.0453713590956603        0.0566619704916846
         0.011959760410037        0.0232944324734871        0.0290912256485504
       0.00393706926284678       0.00766836382523672       0.00957662749024029
      0.000831005429087199       0.00161857756253439       0.00202135875836257
      0.000112464355116679      0.000219050652866017      0.000273561160085806

  Columns 7 through 9

      0.000219050652866017      0.000112464355116679      3.70224770827489e-05
       0.00161857756253439      0.000831005429087199      0.000273561160085806
       0.00766836382523672       0.00393706926284678        0.0012960555938432
        0.0232944324734871         0.011959760410037       0.00393706926284678
        0.0453713590956603        0.0232944324734871       0.00766836382523672
        0.0566619704916846        0.0290912256485504       0.00957662749024029
        0.0453713590956603        0.0232944324734871       0.00766836382523672
        0.0232944324734871         0.011959760410037       0.00393706926284678
       0.00766836382523672       0.00393706926284678        0.0012960555938432
       0.00161857756253439      0.000831005429087199      0.000273561160085806
      0.000219050652866017      0.000112464355116679      3.70224770827489e-05

  Columns 10 through 11

       7.8144115330536e-06      1.05756559815326e-06
      5.77411251978637e-05       7.8144115330536e-06
      0.000273561160085806      3.70224770827489e-05
      0.000831005429087199      0.000112464355116679
       0.00161857756253439      0.000219050652866017
       0.00202135875836257      0.000273561160085806
       0.00161857756253439      0.000219050652866017
      0.000831005429087199      0.000112464355116679
      0.000273561160085806      3.70224770827489e-05
      5.77411251978637e-05       7.8144115330536e-06
       7.8144115330536e-06      1.05756559815326e-06
> h2
              1            2            3            4            5            6            7
1  1.057566e-06 7.814412e-06 3.702248e-05 0.0001124644 0.0002190507 0.0002735612 0.0002190507
2  7.814412e-06 5.774113e-05 2.735612e-04 0.0008310054 0.0016185776 0.0020213588 0.0016185776
3  3.702248e-05 2.735612e-04 1.296056e-03 0.0039370693 0.0076683638 0.0095766275 0.0076683638
4  1.124644e-04 8.310054e-04 3.937069e-03 0.0119597604 0.0232944325 0.0290912256 0.0232944325
5  2.190507e-04 1.618578e-03 7.668364e-03 0.0232944325 0.0453713591 0.0566619705 0.0453713591
6  2.735612e-04 2.021359e-03 9.576627e-03 0.0290912256 0.0566619705 0.0707622378 0.0566619705
7  2.190507e-04 1.618578e-03 7.668364e-03 0.0232944325 0.0453713591 0.0566619705 0.0453713591
8  1.124644e-04 8.310054e-04 3.937069e-03 0.0119597604 0.0232944325 0.0290912256 0.0232944325
9  3.702248e-05 2.735612e-04 1.296056e-03 0.0039370693 0.0076683638 0.0095766275 0.0076683638
10 7.814412e-06 5.774113e-05 2.735612e-04 0.0008310054 0.0016185776 0.0020213588 0.0016185776
11 1.057566e-06 7.814412e-06 3.702248e-05 0.0001124644 0.0002190507 0.0002735612 0.0002190507
              8            9           10           11
1  0.0001124644 3.702248e-05 7.814412e-06 1.057566e-06
2  0.0008310054 2.735612e-04 5.774113e-05 7.814412e-06
3  0.0039370693 1.296056e-03 2.735612e-04 3.702248e-05
4  0.0119597604 3.937069e-03 8.310054e-04 1.124644e-04
5  0.0232944325 7.668364e-03 1.618578e-03 2.190507e-04
6  0.0290912256 9.576627e-03 2.021359e-03 2.735612e-04
7  0.0232944325 7.668364e-03 1.618578e-03 2.190507e-04
8  0.0119597604 3.937069e-03 8.310054e-04 1.124644e-04
9  0.0039370693 1.296056e-03 2.735612e-04 3.702248e-05
10 0.0008310054 2.735612e-04 5.774113e-05 7.814412e-06
11 0.0001124644 3.702248e-05 7.814412e-06 1.057566e-06