I upload an image to r:
我将图片上传到r:
img=readJPEG("18105_552.jpg", native = FALSE)
and the I wrote a for loop to average the pixels:
我写了一个for循环来平均像素:
options(digits=15)
m=matrix(data=NA, nrow=1, ncol=230400)
for(k in 1)
for (i in (1: 480)){
for (j in (1: 480)){
m[k, ]<-((img[i,j,1]+img[i,j,2]+img[i,j,3])/3)
}}
However, my output is all ones. If I go in and manually try to get values, they are all .8 or .9 something, so I think r is rounding everything up to 1, but I can't seem to figure out how to stop it from doing so. Any suggestions?
但是,我的输出是全部的。如果我进去并手动尝试获取值,它们都是.8或.9的东西,所以我认为r将所有内容都舍入到1,但我似乎无法弄清楚如何阻止它这样做。有什么建议么?
1 个解决方案
#1
1
Assuming that img is an array structure or can be converted to one, this can be done without explicit loops using rowMeans()
:
假设img是一个数组结构或者可以转换为一个,这可以在没有使用rowMeans()的显式循环的情况下完成:
img <- array(runif(30,0.6,1),dim=c(10,10,3))
rowMeans(img,dims=2)
Or, to get it in one row:
或者,将其排成一行:
matrix(rowMeans(img,dims=2),nrow=1)
This stores the numbers columnwise. Use byrow=TRUE
to fill up row by row. Next to a correct result, you get a considerable speedup.
这会按列存储数字。使用byrow = TRUE逐行填充。在正确的结果旁边,您可以获得相当快的速度。
By the way, your problem is not rounding. The reason your solution doesn't work, is the following step:
顺便说一句,你的问题不是四舍五入。您的解决方案不起作用的原因如下:
m[k, ]<-((img[i,j,1]+img[i,j,2]+img[i,j,3])/3)
Here you calculate a single number, but assign that to the k'th row of the matrix m
. R will recycle the single number, and fill the entire row with that single number. This one number will be the last one you calculated, and that might be the 1 in your case.
在这里计算单个数字,但将其分配给矩阵m的第k行。 R将回收单个数字,并用该单个数字填充整行。这一个数字将是您计算的最后一个数字,在您的情况下可能是1。
EDIT :
To do this for a set of pictures, store them in a list and use sapply to do the work for you:
要对一组图片执行此操作,请将它们存储在列表中并使用sapply为您完成工作:
Say:
img <- array(runif(30,0.6,1),dim=c(10,10,3))
img2 <- array(runif(30,0.6,1),dim=c(10,10,3))
Then make a list:
然后列出一个清单:
images <- list(img,img2)
names(images) <- c('img1','img2')
And :
m <- sapply(images,rowMeans,dims=2)
does what you want.
做你想要的。
#1
1
Assuming that img is an array structure or can be converted to one, this can be done without explicit loops using rowMeans()
:
假设img是一个数组结构或者可以转换为一个,这可以在没有使用rowMeans()的显式循环的情况下完成:
img <- array(runif(30,0.6,1),dim=c(10,10,3))
rowMeans(img,dims=2)
Or, to get it in one row:
或者,将其排成一行:
matrix(rowMeans(img,dims=2),nrow=1)
This stores the numbers columnwise. Use byrow=TRUE
to fill up row by row. Next to a correct result, you get a considerable speedup.
这会按列存储数字。使用byrow = TRUE逐行填充。在正确的结果旁边,您可以获得相当快的速度。
By the way, your problem is not rounding. The reason your solution doesn't work, is the following step:
顺便说一句,你的问题不是四舍五入。您的解决方案不起作用的原因如下:
m[k, ]<-((img[i,j,1]+img[i,j,2]+img[i,j,3])/3)
Here you calculate a single number, but assign that to the k'th row of the matrix m
. R will recycle the single number, and fill the entire row with that single number. This one number will be the last one you calculated, and that might be the 1 in your case.
在这里计算单个数字,但将其分配给矩阵m的第k行。 R将回收单个数字,并用该单个数字填充整行。这一个数字将是您计算的最后一个数字,在您的情况下可能是1。
EDIT :
To do this for a set of pictures, store them in a list and use sapply to do the work for you:
要对一组图片执行此操作,请将它们存储在列表中并使用sapply为您完成工作:
Say:
img <- array(runif(30,0.6,1),dim=c(10,10,3))
img2 <- array(runif(30,0.6,1),dim=c(10,10,3))
Then make a list:
然后列出一个清单:
images <- list(img,img2)
names(images) <- c('img1','img2')
And :
m <- sapply(images,rowMeans,dims=2)
does what you want.
做你想要的。