如何使用 writeJPEG 从 R 中嵌套数组中所有元素的平均值重现图像
How to reproduce image using writeJPEG from the mean of all elements in a nested array in R
我有一个结构复杂的嵌套数组。这个数组是15个人的集合,每人11张图片,描绘了眨眼、开心等11种面部表情
我想计算 15 个人拍摄的 11 张图像的平均图像。
all.faces
的结构是:
> str(all.faces)
List of 165
$ : int [1:159, 1:159] 48 50 51 50 50 53 57 55 50 47 ...
$ : int [1:159, 1:159] 159 156 131 130 117 115 122 119 123 129 ...
(... and 163 more...)
[list output truncated]
- attr(*, "dim")= int [1:2] 11 15
- attr(*, "dimnames")=List of 2
..$ : chr [1:11] "centerlight" "glasses" "happy" "leftlight" ...
..$ : NULL
当我尝试时
myarray <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))
mean.faces <- apply(myarray,1:2, mean)
writeJPEG(mean.faces,"MEAN_FACES.jpg")
我知道了:
怎么会?我会假设我会得到某种图像,而不是像这样一团糟的黑白配色方案。
可重现的例子
faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
faces.array <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))
mean.all.faces <- apply(faces.array,1:2, mean)
writeJPEG(mean.all.faces,"MEAN_FACES.jpg")
处理这种情况的最佳方法是什么?
提前致谢。
简短回答:writeJPEG()
需要一个介于 0 和 1 之间的值矩阵。您要为其提供值在 0-255 范围内的原始数据。
具体来说,您给出的示例 (yalefaces.mat
) 的尺寸为:
> faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
> dim(faces[[1]])
[1] 48 42 2414
所以它实际上是 2414 个大小为 48x42 的面。我们可以这样显示:
> image(faces[[1]][,,1])
如果对它们进行平均(当然不尝试调整数组大小):
> mean.all.faces <- apply(faces[[1]], 1:2, mean)
> image(mean.all.faces)
它似乎工作正常:
但是当你把它写成 JPEG 时,下面的内容会产生垃圾:
> writeJPEG(mean.all.faces, "BAD.jpg")
而以下工作(虽然有点暗):
> writeJPEG(mean.all.faces/255, "GOOD.jpg")
我有一个结构复杂的嵌套数组。这个数组是15个人的集合,每人11张图片,描绘了眨眼、开心等11种面部表情
我想计算 15 个人拍摄的 11 张图像的平均图像。
all.faces
的结构是:
> str(all.faces)
List of 165
$ : int [1:159, 1:159] 48 50 51 50 50 53 57 55 50 47 ...
$ : int [1:159, 1:159] 159 156 131 130 117 115 122 119 123 129 ...
(... and 163 more...)
[list output truncated]
- attr(*, "dim")= int [1:2] 11 15
- attr(*, "dimnames")=List of 2
..$ : chr [1:11] "centerlight" "glasses" "happy" "leftlight" ...
..$ : NULL
当我尝试时
myarray <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))
mean.faces <- apply(myarray,1:2, mean)
writeJPEG(mean.faces,"MEAN_FACES.jpg")
我知道了:
怎么会?我会假设我会得到某种图像,而不是像这样一团糟的黑白配色方案。
可重现的例子
faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
faces.array <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))
mean.all.faces <- apply(faces.array,1:2, mean)
writeJPEG(mean.all.faces,"MEAN_FACES.jpg")
处理这种情况的最佳方法是什么?
提前致谢。
简短回答:writeJPEG()
需要一个介于 0 和 1 之间的值矩阵。您要为其提供值在 0-255 范围内的原始数据。
具体来说,您给出的示例 (yalefaces.mat
) 的尺寸为:
> faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
> dim(faces[[1]])
[1] 48 42 2414
所以它实际上是 2414 个大小为 48x42 的面。我们可以这样显示:
> image(faces[[1]][,,1])
如果对它们进行平均(当然不尝试调整数组大小):
> mean.all.faces <- apply(faces[[1]], 1:2, mean)
> image(mean.all.faces)
它似乎工作正常:
但是当你把它写成 JPEG 时,下面的内容会产生垃圾:
> writeJPEG(mean.all.faces, "BAD.jpg")
而以下工作(虽然有点暗):
> writeJPEG(mean.all.faces/255, "GOOD.jpg")