我的问题基于my earlier question。该解决方案适用于给定的示例(rlogo)。但是当我加载JPEG图像时,我得到一个错误,如下所示。
CGPointMake(self.view.bounds.width, self.view.bounds.height)
我看了str(x),我到了下面。我应该如何修改我的代码,使其运行如上所示? x @ data @ values为空:(
library(raster)
r1 <- brick("results_circle.jpg")#please load any jpeg image
width=ncol(r1)
height=nrow(r1)
x <- crop(r1, extent(0,width,0,height))
plotRGB(x)
str(x)
class(x)
#drawing a circle on the image
circlex=width/2
circley=height/2
radius=min(width,height)*0.3
draw.circle(circlex,circley,radius,border="blue")
#finding pixels that will lie outside the circle
mat=( ((1:(height*width) %/% (height) )-(height-circley)*width/height)^2 +((1:(height*width) %% width) -circlex )^2 ) >= radius^2
#converting pixels that are outside circle to black
x@data@values[mat, 3] <- 0
Error in `[<-`(`*tmp*`, mat, 3, value = 0) :
(subscript) logical subscript too long
x@data@values[mat, 1] <- 0
x@data@values[mat, 2] <- 0
答案 0 :(得分:1)
您可以对RasterBrick
进行子集化并更改值。
例如,使用随机图像和代码,您可以执行以下操作:
library(raster)
par(mfrow=c(1,2))
r1 <- brick("image.jpg")#please load any jpeg image
width=ncol(r1)
height=nrow(r1)
x <- crop(r1, extent(0,width,0,height))
plotRGB(x)
circlex=width/2
circley=height/2
radius=min(width,height)*0.3
draw.circle(circlex,circley,radius,border="blue")
mat=( ((1:(height*width) %/% (height) )-(height-circley)*width/height)^2 +((1:(height*width) %% width) -circlex )^2 ) >= radius^2
x[mat] <- 0
plotRGB(x)
得到这个结果:
查看mat
公式,转换后圆圈看起来更像椭圆。
我试过了:
mat =(rep(1:height,each=width)-(height-circley))^2+(rep(1:width,height) - circlex )^2 >=radius^2
给出: