我一直在摆弄R中的一个函数,其中,长话短说,我有一个for
-loop,并且在每一步,我使用保存一个图png
,然后立即readPNG
使我可以提取RGB信息.然后我制作第二个图,然后readPNG
这样我可以比较两个图像的RGB.问题是我png()
在一些循环之后不断收到关于无法启动设备或打开文件进行写入的错误消息(可能只有少数几个循环,或者多达几千个循环) .
这是真正简化的代码,但它有基本要素,并生成错误消息:
testfun<-function(beg,fini)
{
library(png)
setwd("D://mydirectory")
for (i in beg:fini)
{
png("test.png",width=277,height=277) #candidate image
par(mai=c(0,0,0,0))
plot(1,type="n",ann=FALSE,xlim=c(0,255),ylim=c(0,255),
xaxt="n",yaxt="n",frame.plot=F)
polygon(x=c(10,60,60),y=c(10,10,60),col="red")
graphics.off()
image<-readPNG("test.png")
#code where I get rgb values for original
png("test2.png",width=277,height=277) #candidate image with diferent params
par(mai=c(0,0,0,0))
plot(1,type="n",ann=FALSE,xlim=c(0,255),ylim=c(0,255),
xaxt="n",yaxt="n",frame.plot=F)
polygon(x=c(10,60,60),y=c(10,10,60),col="blue")
graphics.off()
image<-readPNG("test2.png")
#code where I get rgb values for second image, and compare
}
}
Run Code Online (Sandbox Code Playgroud)
并且错误消息:
Error in png("test.png", width = 277, height = 277) :
unable to start png() device …
Run Code Online (Sandbox Code Playgroud)