[英]How do I make a random walk step read the background image?
I'm trying to get my random walk to invoke function z on each step. 我试图让我随机行走以在每个步骤上调用函数z。 Whenever the step ends on a red pixel in the background image variable CS=1. 每当步长在背景图像变量CS = 1中的红色像素处结束时。 If the step ends on a black pixel then CS=0. 如果该步骤以黑色像素结束,则CS = 0。 Right now it's just a randomly generated value. 现在,它只是一个随机生成的值。 Don't worry about the other stuff, I provide it for you to reproduce the issue, but those parts work fine. 不用担心其他问题,我为您提供了重现此问题的方法,但是这些部分都可以正常工作。 The critical part is variable CS in function step.prob. 关键部分是功能step.prob中的变量CS。
Main question: How do I make the walk function read the background image?<-- solved 主要问题:如何使步行功能读取背景图像?
New issue: I'm receiving a "subscript out of bounds" error in the step.prob() function. 新问题:在step.prob()函数中收到“下标超出范围”错误。 Why and how do I fix it? 为什么以及如何解决?
so far it looks like this: 到目前为止,它看起来像这样:
You will need package EBImage, and this image saved as testmap2.png in your working directory to run this code. 您将需要EBImage软件包,并且将此图像另存为工作目录中的testmap2.png才能运行此代码。 The code is currently: 该代码当前为:
library(tiff)
library("EBImage")
pic<-readImage("testmap2.png",all=TRUE,package="EBImage")
display(pic, method="raster")
dim(pic)
pic[,,1]
par(bg="black",col="black",col.axis="white",new=T)
P<-30
step.max=125
s<-step.max
pic<-readImage("testmap2.png",all=TRUE,package="EBImage")
walkW <- function(n.times=125,
xlim=c(524058,542800),
ylim=c(2799758,2818500),
start=c(542800,2815550),
stepsize=c(4000,4000)) {
display(pic, method="raster")
par(bg="black",col="black",col.axis="white",new=T)
plot(c(0,0),type="n",xlim=xlim,ylim=ylim,
xlab="Easting",ylab="Northing",col="white",col.lab="white")#arena
x <- start[1]
y <- start[2]
steps <- 1/c(1,2,4,8,12,16)
steps.y <- c(steps,-steps,0)
steps.x <- c(steps[c(1,5,6)],-steps,0)
points(x,y,pch=16,col="green",cex=1)
for (i in 1:n.times) {
repeat {
xi <- stepsize[1]*sample(steps.x,1)
yi <- stepsize[2]*sample(steps.y,1)
newx <- x+xi
newy <- y+yi
if (newx>xlim[1] && newx<xlim[2] &&
newy>ylim[1] && newy<ylim[2]) break
}
lines(c(x,newx),c(y,newy),col="white")
x <- newx
y <- newy
####the following function returns an "subscript out of bounds" error
step.prob<-function(n.times=step.max){
CS<-pic[x,y,1]
CS.max<-1
step.num<-15
SP<-(((CS/CS.max)*(1-(step.num/step.max))+(step.num/step.max))*100)
}
z<-step.prob(1)
##end the function that returns an error
if(z>P)break
else
if(step.max){points(newx,newy,pch=16,col="yellow",cex=1)
}
}
}
set.seed(101)
walkW(s)
A color png
or jpg
is just a multidimensional array for position and RGB color. png
或jpg
颜色只是位置和RGB颜色的多维数组。
library(png) # same idea for EBImage
pic<-readPNG("testmap2.png")
dim(pic) # 615 683 3 (3 for the RGB levels)
pic[1:5,1:5,1] # where is it red?
Returns: 返回:
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 1 1
[2,] 0 0 0 1 1
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
So to test a point on the walk for red: 因此,要在步行中测试一个点是否为红色:
x <- 2; y <- 2
pic[x,y,1] # 0, not red
x <- 2; y <- 5
pic[x,y,1] # 1 , red
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.