使用gwidgets显示和操作图像

时间:2013-12-03 21:51:36

标签: r gwidgets

亲爱的stackexchange世界。我想问一个问题,我正面临一个问题,我正在用R写的一个小程序。 我编写了一个代码,可以导入图像并使用EBImage()库对其进行操作。我使用gWidgets()库,因此用户可以进行动态操作。代码就是这个:

library("EBImage")
library("gWidgets2")
setwd(choose.dir())
imageinput<-file.choose()
image<-readImage(imageinput)

##defininig the color mode
colorimage<-c(Greymode="gray",RGBmode="rgb")


updateImage <-function (h,...) {

image1<-((svalue(brightness)+image*svalue(contrast))^(svalue(gamma)))
image2<-channel(image1,colorimage[svalue(colormode)])
display(image2)
}

colormode <- gradio(names(colorimage), horizontal=FALSE,handler=updateImage)
brightness<-gslider(from=-1,to=1,by=.1, value=0,handler=updateImage)
contrast <- gslider(from=0,to=10,by=.1, value=1,handler=updateImage)
gamma <- gslider(from=0,to=10,by=0.1, value=1,handler=updateImage)


window <- gwindow("Image Editing")


BigGroup <- ggroup(cont=window)
group <- ggroup(horizontal=FALSE, container=BigGroup)
tmp <- gframe("Colormode", container=group)
add(tmp, colormode)
tmp <- gframe("Brightness", container=group)
add(tmp, brightness, expand=TRUE)
tmp <- gframe("Contrast", container=group)
add(tmp, contrast, expand=TRUE)
tmp <- gframe("Gamma", container=group)
add(tmp, gamma, expand=TRUE)

但我遇到了一个问题(正如我在上一个问题中发布的那样,但我解决了一些问题,我认为重新发布一个更好的代码并解决了许多问题会很好)。问题是我无法在gWidgets GUI中显示我构建的图像以及它是如何动态编辑的。虽然有一种方法可以使用EBImage包的display()功能查看图像,但它不是我想要的,因为它显示在Web浏览器中而不是GUI中。

如果有人知道如何解决这个问题,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

剧本完成后,感谢JohnVerzaniStéphaneLaurent的建议。这有点棘手,但最后它起作用了! 代码是这样的:

library("EBImage")
library("gWidgets2")
setwd(choose.dir())
imageinput<-file.choose()
image<-readImage(imageinput)

##defininig the color mode
colorimage<-c(RGBmode="rgb",Greymode="gray")


updateImage <-function (h,...) {
image1<-((svalue(brightness)+image*svalue(contrast))^(svalue(gamma)))
image2<-channel(image1,colorimage[svalue(colormode)])
imageout<-writeImage(image2,"imageout.jpeg")
svalue(img)<-"imageout.jpeg"
}

colormode <- gradio(names(colorimage), horizontal=FALSE,handler=updateImage)
brightness<-gslider(from=-1,to=1,by=.1, value=0,handler=updateImage)
contrast <- gslider(from=0,to=10,by=.1, value=1,handler=updateImage)
gamma <- gslider(from=0,to=10,by=0.1, value=1,handler=updateImage)


window <- gwindow("Image Editing")

BigGroup <- ggroup(cont=window)
group <- ggroup(horizontal=FALSE, container=BigGroup)
tmp <- gframe("Colormode", container=group)
add(tmp, colormode)
tmp <- gframe("Brightness", container=group)
add(tmp, brightness, expand=TRUE)
tmp <- gframe("Contrast", container=group)
add(tmp, contrast, expand=TRUE)
tmp <- gframe("Gamma", container=group)
add(tmp, gamma, expand=TRUE)
img <- gimage(imageinput,container=BigGroup)

应该创建一个包含图形(gimage)的新框架,但是每次编辑后都应该更新显示的图像的值。所以svalue应该在函数中。