【发布时间】:2016-08-11 21:07:22
【问题描述】:
我需要计算R中的结构相似度(SSIM)指数,只能找到Matlab中实现的方法。除了两个 Matlab 方法“fspecial”和“filter2”之外,在 R 中重写该方法似乎非常简单。
fspecial 返回 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)
然而,奇怪的是,当我运行这两种方法时,我得到的结果并不相同,而是得到了一个按 14.13 缩放的矩阵:
h2/h
1 2 3 4 5 6 7 8 9 10 11
1 14.13137 14.13185 14.13201 14.13238 14.13187 14.13189 14.13187 14.13238 14.13201 14.13185 14.13137
2 14.13185 14.13186 14.13189 14.13175 14.13164 14.13154 14.13164 14.13175 14.13189 14.13186 14.13185
3 14.13201 14.13189 14.13135 14.13172 14.13176 14.13187 14.13176 14.13172 14.13135 14.13189 14.13201
4 14.13238 14.13175 14.13172 14.13155 14.13209 14.13194 14.13209 14.13155 14.13172 14.13175 14.13238
5 14.13187 14.13164 14.13176 14.13209 14.13194 14.13182 14.13194 14.13209 14.13176 14.13164 14.13187
6 14.13189 14.13154 14.13187 14.13194 14.13182 14.13188 14.13182 14.13194 14.13187 14.13154 14.13189
7 14.13187 14.13164 14.13176 14.13209 14.13194 14.13182 14.13194 14.13209 14.13176 14.13164 14.13187
8 14.13238 14.13175 14.13172 14.13155 14.13209 14.13194 14.13209 14.13155 14.13172 14.13175 14.13238
9 14.13201 14.13189 14.13135 14.13172 14.13176 14.13187 14.13176 14.13172 14.13135 14.13189 14.13201
10 14.13185 14.13186 14.13189 14.13175 14.13164 14.13154 14.13164 14.13175 14.13189 14.13186 14.13185
11 14.13137 14.13185 14.13201 14.13238 14.13187 14.13189 14.13187 14.13238 14.13201 14.13185 14.13137
有人对我做错了什么有建议吗?
【问题讨论】:
-
在切线说明中,您可以查看
outer和expand.grid。它们可用于缓解嵌套的for循环。